Add Regular Expression when using a Variable

Ask general questions here.
Nadeem
Posts: 10
Joined: Fri Jul 01, 2011 9:26 am

Add Regular Expression when using a Variable

Post by Nadeem » Tue Dec 06, 2011 2:07 pm

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

User avatar
Support Team
Site Admin
Site Admin
Posts: 12145
Joined: Fri Jul 07, 2006 4:30 pm
Location: Houston, Texas, USA
Contact:

Re: Add Regular Expression when using a Variable

Post by Support Team » Tue Dec 06, 2011 3:41 pm

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

SegmentationFault
Posts: 4
Joined: Tue Jul 10, 2012 9:57 pm

Re: Add Regular Expression when using a Variable

Post by SegmentationFault » Tue Jul 10, 2012 10:05 pm

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

User avatar
Support Team
Site Admin
Site Admin
Posts: 12145
Joined: Fri Jul 07, 2006 4:30 pm
Location: Houston, Texas, USA
Contact:

Re: Add Regular Expression when using a Variable

Post by Support Team » Wed Jul 11, 2012 4:19 pm

Hello,

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

SegmentationFault
Posts: 4
Joined: Tue Jul 10, 2012 9:57 pm

Re: Add Regular Expression when using a Variable

Post by SegmentationFault » Wed Jul 11, 2012 4:27 pm

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?

User avatar
Support Team
Site Admin
Site Admin
Posts: 12145
Joined: Fri Jul 07, 2006 4:30 pm
Location: Houston, Texas, USA
Contact:

Re: Add Regular Expression when using a Variable

Post by Support Team » Thu Jul 12, 2012 3:40 pm

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

SegmentationFault
Posts: 4
Joined: Tue Jul 10, 2012 9:57 pm

Re: Add Regular Expression when using a Variable

Post by SegmentationFault » Thu Jul 12, 2012 3:52 pm

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

User avatar
Support Team
Site Admin
Site Admin
Posts: 12145
Joined: Fri Jul 07, 2006 4:30 pm
Location: Houston, Texas, USA
Contact:

Re: Add Regular Expression when using a Variable

Post by Support Team » Fri Jul 13, 2012 1:38 pm

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

SegmentationFault
Posts: 4
Joined: Tue Jul 10, 2012 9:57 pm

Re: Add Regular Expression when using a Variable

Post by SegmentationFault » Fri Jul 13, 2012 2:14 pm

Perfect, that is exactly what I'm looking for. Thanks!

AccidentReport
Posts: 78
Joined: Tue Dec 04, 2012 2:30 pm

Re: Add Regular Expression when using a Variable

Post by AccidentReport » Wed Dec 12, 2012 12:46 pm

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?! ;)

User avatar
Support Team
Site Admin
Site Admin
Posts: 12145
Joined: Fri Jul 07, 2006 4:30 pm
Location: Houston, Texas, USA
Contact:

Re: Add Regular Expression when using a Variable

Post by Support Team » Wed Dec 12, 2012 1:10 pm

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

AccidentReport
Posts: 78
Joined: Tue Dec 04, 2012 2:30 pm

Re: Add Regular Expression when using a Variable

Post by AccidentReport » Wed Dec 12, 2012 2:24 pm

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!

User avatar
Support Team
Site Admin
Site Admin
Posts: 12145
Joined: Fri Jul 07, 2006 4:30 pm
Location: Houston, Texas, USA
Contact:

Re: Add Regular Expression when using a Variable

Post by Support Team » Thu Dec 13, 2012 1:37 pm

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
You do not have the required permissions to view the files attached to this post.

AccidentReport
Posts: 78
Joined: Tue Dec 04, 2012 2:30 pm

Re: Add Regular Expression when using a Variable

Post by AccidentReport » Thu Dec 13, 2012 2:30 pm

I am trying to modify the xpath of the repository item.
You do not have the required permissions to view the files attached to this post.

User avatar
Support Team
Site Admin
Site Admin
Posts: 12145
Joined: Fri Jul 07, 2006 4:30 pm
Location: Houston, Texas, USA
Contact:

Re: Add Regular Expression when using a Variable

Post by Support Team » Thu Dec 13, 2012 4:24 pm

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
You do not have the required permissions to view the files attached to this post.