Use xPath in conditional statement

Ask general questions here.
Northers75
Posts: 5
Joined: Fri Aug 19, 2016 10:49 am

Use xPath in conditional statement

Post by Northers75 » Wed Aug 31, 2016 3:42 pm

Hi,

I have some variables that I need to set in my UI. Unfortunately I cannot tell whether they are set until I start running and the steps used to enable them also disable them so I have to know whether they are enabled or not before I try and enable them. This is an old UI so I have to use an xPath to the RawText element as follows: -

"/form[@title='SmartSEM Status']//list[@controlid='10063']/listitem[@text>'Beam State = ']"

The RawText contains a variable at the end (Beam State = Off, Beam State = Running, etc.) hence the use of > which I hope will look for any string starting with ‘Beam State =’.

Is there any way I can incorporate this into a conditional statement in some user code such as: -

if ("/form[@title='SmartSEM Status']//list[@controlid='10063']/listitem[@text>'Beam State = ']")
{
//Do nothing, it is already selected
}
else
{
Report.Log(ReportLevel.Info, "Mouse", "Mouse Left Click item 'SmartSEMStatus.Tree10062.BeamState' at 14;10.", repo.SmartSEMStatus.Tree10062.BeamStateInfo, new RecordItemIndex(7));
repo.SmartSEMStatus.Tree10062.BeamState.Click("14;10");
Delay.Milliseconds(200);
}


I don’t need to validate the existence of the xPath and do not want execution to stop if the xPath is not found, I just need to check whether it exists or not then act accordingly. This is not for an actual test, but for the initial setup to be run prior to each test.

Regards,

asdf
Posts: 174
Joined: Mon Mar 21, 2016 3:16 pm

Re: Use xPath in conditional statement

Post by asdf » Fri Sep 02, 2016 1:35 pm

Hi Northers,

I would recommend using the GetValue action in Ranorex Studio and store this attribute to a variable.
Afterwards, create a usercode module where you check this variable in an if statement.

You can use GetValue also in code. I hope the code snippet below helps you to understand my approach.

Code: Select all

var attrib = repo.YourPath.YourElement.Element.GetAttributeValue("InnerText");
if(attrib == "YourText")
{
       //Do some things
}
I hope this helps.

Kind regards,
asdf

Northers75
Posts: 5
Joined: Fri Aug 19, 2016 10:49 am

Re: Use xPath in conditional statement

Post by Northers75 » Fri Sep 02, 2016 2:20 pm

Hi Asdf,

Thanks for the reply.

I have tried using GetAttributeValue in code, but I can't get it to work. I'm using: -

var attrib = repo.SmartSEMStatus.List10063.BeamStateEqShutdown.GetAttributeValue("InnerText");
if (attrib == "Beam State = ")
{
// Do nothing
}
else
{
// Do something
}

All I keep getting are errors: -

The type arguments for method 'Ranorex.Adapter.GetAttributeValue<T>(string)' cannot be inferred from the usage. Try specifying the type arguments explicitly. (CS0411)

and

Operator '==' cannot be applied to operands of type 'T' and 'string' (CS0019)

Any ideas?

One of the main problems I have is that the text can be any one of many variations, but they all start with 'Beam state =', that is why I was only capturing the rawText up to the equals sign. I can set up a bool variable to check whether each possible string exists on screen and use them in the conditional statement if (string1 || string2 || string3 || string4), etc. but this results in Ranorex timing out when it tries to find each string so if there are four strings and my default timeout is one minute it will take at least four minutes to perform this very simple operation.

All I'm after is a way of looking on the screen for a text field starting with 'Beam state =' then doing something if it exists and doing something else if it does not...

johnzer
Posts: 16
Joined: Thu Aug 18, 2016 9:27 pm

Re: Use xPath in conditional statement

Post by johnzer » Fri Sep 02, 2016 3:26 pm

I think you might be using the function incorrectly.

Are you trying to get a String? If so, try:

Code: Select all

var attrib = repo.SmartSEMStatus.List10063.BeamStateEqShutdown.GetAttributeValue<String>("InnerText");
If not, replace <String> with <[Whatever type you want]>.

Northers75
Posts: 5
Joined: Fri Aug 19, 2016 10:49 am

Re: Use xPath in conditional statement

Post by Northers75 » Fri Sep 02, 2016 3:45 pm

Yep, it's a string and the addition of <String> has stopped the errors, but now attrib only ever returns null. :(

Any ideas?

krstcs
Ranorex Guru
Posts: 2683
Joined: Tue Feb 07, 2012 4:14 pm
Location: Austin, Texas, USA

Re: Use xPath in conditional statement

Post by krstcs » Fri Sep 02, 2016 3:51 pm

You can find the Ranorex API here: http://www.ranorex.com/Documentation/Ranorex/

It should help with issues like this as it pretty clearly details the requirements and usage for all of the Ranorex PUBLIC functionality.

Are you sure that the InnerText attribute of that element is the one you want? Have you looked at it with Ranorex Spy to make sure?
Shortcuts usually aren't...

johnzer
Posts: 16
Joined: Thu Aug 18, 2016 9:27 pm

Re: Use xPath in conditional statement

Post by johnzer » Fri Sep 02, 2016 3:59 pm

Northers75 wrote:Yep, it's a string and the addition of <String> has stopped the errors, but now attrib only ever returns null. :(

Any ideas?
If it's returning null, then either the attribute is not defined for the element you chose, or the element hasn't loaded by the time you call GetAttribute. As suggested, double check in Ranorex Spy to ensure that you're fetching something that's actually set. If that's all working, try adding a delay before the GetAttribute statement and see if it's an issue with the element loading.

If neither of these work, there's also the possibility that Ranorex is having trouble identifying the element at runtime. This might be an entirely separate issue, that you can being reading about it here:

http://www.ranorex.com/support/user-gui ... forms.html

Northers75
Posts: 5
Joined: Fri Aug 19, 2016 10:49 am

Re: Use xPath in conditional statement

Post by Northers75 » Mon Sep 05, 2016 9:28 am

Thanks for all the help, I've sorted this now.

In the end all I did was get the rawtext from the element, convert it to a string and take a substring of the portion that I need. I put all that in a try catch so execution did not stop if the element was not found by Ranorex then based my condition on the substring.

It's probably horrendous code, but now my variables are being set correctly if they are not currently set and are ignored if they are set regardless of what the latter part of the string contains.

Outside of this usercode I have added validations to ensure the variables display the state that I expected during the particular test.

asdf
Posts: 174
Joined: Mon Mar 21, 2016 3:16 pm

Re: Use xPath in conditional statement

Post by asdf » Mon Sep 05, 2016 1:51 pm

Hi Northers,

The problem in your code is that you forgot the "Element" after your repository item.
In order to use GetAttributeValue for your approach please try the following code snippet.

Code: Select all

var attrib = repo.SmartSEMStatus.List10063.BeamStateEqShutdown.Element.GetAttributeValue("InnerText");
if (attrib == "Beam State = ")
{
        Report.Info("Success");
}
else
{
        Report.Info("Something went wrong!");
}
Hope this helps!

Kind regards,
asdf