Usage of Regex.Escape for the variables in Recorded Modules

Technology specific object identification, supported applications, web technologies, and 3rd party controls.
Karzone
Posts: 8
Joined: Thu Feb 06, 2014 6:16 pm

Usage of Regex.Escape for the variables in Recorded Modules

Post by Karzone » Thu Feb 06, 2014 10:16 pm

Hi,

I am using a regular expression, to identify an element, based on a variable value.
Eg: \\form[@Title~$country]

Where the country value might have some special characters like '(, [, ), ]' etc.
But this regular expression is used to identify an object in a recorded module.

I am not sure how to accommodate this regex.escape here in this situation in the recorded module.

Could you advice me, what is the best way to implement this escape sequence for the variable in the regular expression.

If there is no provision available, could you also advice me how to do it through user code.
I wanted to use this object with Click() event.

Please help.

Regards,
Karthick K

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

Re: Usage of Regex.Escape for the variables in Recorded Modules

Post by krstcs » Fri Feb 07, 2014 3:11 pm

My understanding is that Ranorex automatically escapes all variables passed in to RegEx paths.

Have you tried it just the way it is? Is it finding the element? If not, what is the error?

Can you post a snapshot of your application and a small sample of the code/test you are running?
Shortcuts usually aren't...

Karzone
Posts: 8
Joined: Thu Feb 06, 2014 6:16 pm

Re: Usage of Regex.Escape for the variables in Recorded Modules

Post by Karzone » Fri Feb 07, 2014 6:44 pm

Thank you for your response.

I am trying to maximize the browser, which has the browser name which is dynamic with respect to the country of execution.

I am not using any user code to achieve it, and I'm only using the recorded module to perform it.

And in the repository, I'm using the following regular expression to identify the browser, which has the following country name[Dynamic] displayed on the title bar.
"Shell Malaysia Trading Sdn Bhd (6087-M)", which has the special characters like (, - etc. But these are not escaped by default in the regular expression.

/form[@title~$Country], where country is a variable, which will take values with special characters,
when we try to pass this through the regex, it's not escaping, and I do not find any provisions to escape in the variables in the regular expression, if used in the recorded module.

Please let me know, if you require any other information on this.
Please help on this.
Last edited by Karzone on Thu Feb 20, 2014 11:28 pm, edited 1 time in total.

User avatar
Support Team
Site Admin
Site Admin
Posts: 11709
Joined: Fri Jul 07, 2006 4:30 pm
Location: Graz, Austria

Re: Usage of Regex.Escape for the variables in Recorded Modules

Post by Support Team » Tue Feb 11, 2014 11:32 am

Hi Karzone,

You can escape the characters for instance in the RxPath of the specific repository item.
You could for instance use the following RxPath: /form[@title~'Shell\ '+$NewVariable+'\ Trading\ Sdn\ Bhd\ \(6087\-M\)' and you can set the RepoVariable to "Malaysia".

Regards,
Markus
.
Image

Karzone
Posts: 8
Joined: Thu Feb 06, 2014 6:16 pm

Re: Usage of Regex.Escape for the variables in Recorded Modules

Post by Karzone » Thu Feb 13, 2014 4:18 pm

Hi,

Thank you for your response.

Is there a way to escape the complete variable, instead of making it for every word. As the complete string will vary with respect to country. I am looking for something like Regex.escape(variable), which could be used in the recorded module.

Thanks.

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

Re: Usage of Regex.Escape for the variables in Recorded Modules

Post by krstcs » Thu Feb 13, 2014 4:30 pm

You can do anything you like in UserCode, it's just .NET. Just add a UserCode action in your test and put the logic you want to use inside the method that is created.
Shortcuts usually aren't...

mzperix
Posts: 137
Joined: Fri Apr 06, 2012 12:19 pm

Re: Usage of Regex.Escape for the variables in Recorded Modules

Post by mzperix » Thu Mar 13, 2014 10:34 am

A common way to 'escape' special characters is to shoot for less. Instead of the whole expression 'Shell Malaysia Trading Sdn Bhd (6087-M)' just use 'Malaysia', or even 6087 (if the number is a unique identifier) in your $Country variable.