Page 1 of 2

Add Regular Expression when using a Variable

Posted: Tue Dec 06, 2011 2:07 pm
by Nadeem
Hello,

Somebody might have touched on this topic already but i failed to find it in my search so decided to post my question.

I am validating to check the innertext of en element; so my XPath is as follows:

".............\div/div/div/h2[@innertext~'(?i:06 December 2011)']"

Now, obviously the date will change and I want to pass that through as a variable, whereby it gets picked from an Excel spreadsheet using the formula "=TEXT(TODAY(),"dd mmmm yyyy")".

I am using the regular expression of ~'(?i:06 December 2011)'] because Excel displays the date as "06 December 2011" and the innertext value equals "06 december 2011". Hence, me using this expression matches the text regardless of case sensitivity.

Now my problem is this, how can i keep this regular expression but pass the value through as a variable. If i do the below, Ranorex does not identify it as a variable. :?

".............\div/div/div/h2[@innertext~'(?i:$Var_CurrentDate)']"

Clearly i am not doing it correctly, more to the point, can we?

I do not want to use code as we are trying to refrain from any code. Reason being, we want our solution accessible to all Testers (who lack C# experience) so that they can modify the scripts if required.

Regards
Nadeem

Re: Add Regular Expression when using a Variable

Posted: Tue Dec 06, 2011 3:41 pm
by Support Team
Hi,
Nadeem wrote:Clearly i am not doing it correctly, more to the point, can we?
You could use following RxPath to solve your issue.
div/div/div/h2[@innertext~'(?i:'+$Var_CurrentDate+')']
You are able to concatenate variables in the RxPath with the char + as you know it from a string in code.

Regards,
Peter
Ranorex team

Re: Add Regular Expression when using a Variable

Posted: Tue Jul 10, 2012 10:05 pm
by SegmentationFault
Hello,

I am trying to use a combination of a RegEx and a variable to determine which virtual machines to delete. The end of the path is ....../container/table/row/cell[@text~'$PartialMachineNameVariable'].

I have tried many combinations to get it to work, and if I use the one previously recommended for Nadeem, it will try to delete all of my machines. I have had no luck with any of the user guides either. $PartialMachineNameVariable has a partial name shared by a group of machines I want to delete, and the regular expression would be something like .*($PartialMachineNameVariable).*, so it can be at any point in the name. How do I do this?

Thanks,
Andrew

Re: Add Regular Expression when using a Variable

Posted: Wed Jul 11, 2012 4:19 pm
by Support Team
Hello,

You missed the second "'" and the "+", as Peter wrote you have to concatenate it:
[@text~'.*('+$PartialMachineNameVariable+').*']
Regards,
Markus
Ranorex Support Team

Re: Add Regular Expression when using a Variable

Posted: Wed Jul 11, 2012 4:27 pm
by SegmentationFault
I am doing user code for flexibility of moving between machines and creating classes to with functions based on input (IP Address, etc.). Is there a way to use variables in the RanoreXPath without adding them using the "Variables" button with the different code modules?

Re: Add Regular Expression when using a Variable

Posted: Thu Jul 12, 2012 3:40 pm
by Support Team
Hi,

It depends on where and how you are using the RxPath.
Do you want to add a variable to a RxPath of an element of your repository, or do you want to use a variable for your in user code created adapters?

Regards,
Markus
Ranorex Support Team

Re: Add Regular Expression when using a Variable

Posted: Thu Jul 12, 2012 3:52 pm
by SegmentationFault
I'm doing everything from user code adapters. The variables are created in the code and I don't use the repository at all, instead I use the UI Adapters that Ranorex has. For example, let's say I have these machines:
{CloneMaker,
Discard-0,
Discard-1,
Mycard178,
Keep}.
I want to pass a function a string variable, containing "card", and delete every machine that matches the regular expression (.*card.*). However I want to be able to change what string the variable looks for. Does that help clear things up?

-Andrew

Re: Add Regular Expression when using a Variable

Posted: Fri Jul 13, 2012 1:38 pm
by Support Team
Hi,

I hope I got it ;).
Do you mean the following?
String variable = "lcula";
RxPath path = "/form[@title~'.*"+variable+".*']";
Form form = Host.Local.FindSingle(path);
Report.Info(form.Title);
Form form1= "/form[@title~'.*"+variable+".*']";
Report.Info(form1.Title);
Regards,
Markus
Ranorex Support Team

Re: Add Regular Expression when using a Variable

Posted: Fri Jul 13, 2012 2:14 pm
by SegmentationFault
Perfect, that is exactly what I'm looking for. Thanks!

Re: Add Regular Expression when using a Variable

Posted: Wed Dec 12, 2012 12:46 pm
by AccidentReport
This post is down the right line for what I want but I can't figure it out.

I currently have an object as follows:

...//a[@innertext='Var(DOW - RANOREX_DOW_001 {IN})']

Now, I am trying to alter it so that I can match part of the text and also a variable. Basically i want it to match something like this:

'Var('+$varTypeofNode+' - '+$varNameofNode+' {IN})'

That doesn't work. I know what I want and could do it in another langues but can't work out the magic here. Anyone want to show me my stupidity and fix it in 30 seconds?! ;)

Re: Add Regular Expression when using a Variable

Posted: Wed Dec 12, 2012 1:10 pm
by Support Team
Hi,

Is this the RxPath of a repo item or of an adapter created in UserCode?
If this is the RxPath of a repo item it looks good to me. Are you sure you created two repository variables with the right name and set them to the right value?
Are you getting any error messages?
Which Ranorex version are you using?

Regards,
Markus

Re: Add Regular Expression when using a Variable

Posted: Wed Dec 12, 2012 2:24 pm
by AccidentReport
I'm trying to do it in the xpath bit. I have a variable called $varDOWVarName created in the same test/recording. I have tried the following:

...//a[@innertext='Var(DOW - '+$varDOWVarName+' {IN})']

and this gives the error:

No elements were found for: a[@innertext='Var(DOW - '+$varDOWVarName+' {IN})']

Basically the "Var(DOW - " is plain text, then the variable and then " {IN})", another piece of plain text at the end. I'm probably just using the regex stuff incorrectly but not sure what!

Re: Add Regular Expression when using a Variable

Posted: Thu Dec 13, 2012 1:37 pm
by Support Team
Hi,

Sorry but I am a bit confused, what do you mean with "I'm trying to do it in the xpath bit"?
You can use the xpath in a recording and in the repository.
If you are changing the RxPath of a repository item and if you want to use an variable in the RxPath of the item you have to make sure you created a repository variable and not a module/recording variable as shown below:
RepoVariable.jpg
If you use the RxPath in a recording you must use double quotes "" and not just the single quotes '' and the "$" is also not needed.
Please read my previous postings there you can see the differences.

Regards,
Markus

Re: Add Regular Expression when using a Variable

Posted: Thu Dec 13, 2012 2:30 pm
by AccidentReport
I am trying to modify the xpath of the repository item.

Re: Add Regular Expression when using a Variable

Posted: Thu Dec 13, 2012 4:24 pm
by Support Team
Hi,

I hope the following step by step description of how you could use repository variables in the RxPath of a repository item will help you to solve the issue.
If you just want to use variables in your RxPath there is no need for regular expressions.
If you are using regular expressions in your RxPath you have to use the "~" instead of the "=" as shown in the following link: RanoreXPath.

Step 1:
Create two repository variables, one for the word "DOW" and one for "RANOREX_DOW_001".
Don't forget to set the "Default Value" for each newly created variable.
Step1.jpg
Step 2:
Add the two variables to the RxPath of the repository item.
This is the original RxPath: "option[@innertext='DOW - RANOREX_DOW_001 {IN}']" and this is the RxPath with the two variables: "option[@innertext=''+$FirstVariable+' - '+$SecondVariable+' {IN}']"
Step2.jpg
By the way do you know how to properly select an option tag in a select tag?

Regards,
Markus