Page 1 of 1

Usage of Regex.Escape for the variables in Recorded Modules

Posted: Thu Feb 06, 2014 10:16 pm
by Karzone
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

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

Posted: Fri Feb 07, 2014 3:11 pm
by krstcs
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?

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

Posted: Fri Feb 07, 2014 6:44 pm
by Karzone
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.

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

Posted: Tue Feb 11, 2014 11:32 am
by Support Team
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

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

Posted: Thu Feb 13, 2014 4:18 pm
by Karzone
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.

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

Posted: Thu Feb 13, 2014 4:30 pm
by krstcs
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.

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

Posted: Thu Mar 13, 2014 10:34 am
by mzperix
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.