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,
Use xPath in conditional statement
Re: Use xPath in conditional statement
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.
I hope this helps.
Kind regards,
asdf
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
}
Kind regards,
asdf
-
- Posts: 5
- Joined: Fri Aug 19, 2016 10:49 am
Re: Use xPath in conditional statement
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...
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...
Re: Use xPath in conditional statement
I think you might be using the function incorrectly.
Are you trying to get a String? If so, try:
If not, replace <String> with <[Whatever type you want]>.
Are you trying to get a String? If so, try:
Code: Select all
var attrib = repo.SmartSEMStatus.List10063.BeamStateEqShutdown.GetAttributeValue<String>("InnerText");
-
- Posts: 5
- Joined: Fri Aug 19, 2016 10:49 am
Re: Use xPath in conditional statement
Yep, it's a string and the addition of <String> has stopped the errors, but now attrib only ever returns null.
Any ideas?
Any ideas?
Re: Use xPath in conditional statement
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?
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...
Re: Use xPath in conditional statement
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.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 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
-
- Posts: 5
- Joined: Fri Aug 19, 2016 10:49 am
Re: Use xPath in conditional statement
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.
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.
Re: Use xPath in conditional statement
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.
Hope this helps!
Kind regards,
asdf
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!");
}
Kind regards,
asdf