Using variables in a repository

Best practices, code snippets for common functionality, examples, and guidelines.
jack
Posts: 3
Joined: Sun Oct 16, 2016 9:23 pm

Using variables in a repository

Post by jack » Sun Oct 16, 2016 9:48 pm

Hi,

I have a bunch of text fields whose repository items follow this pattern:
.//input[#'code1']
.//input[#'code2']
.//input[#'code3']
and so on...

I would like to be able to do something like:
.//input[#'code'+$num]
where $num is a declared variable.

Is there a way to just tack on a specific number to the end using variables?
I don't want to include the word 'code' in the variable.

Thanks

asdf
Posts: 174
Joined: Mon Mar 21, 2016 3:16 pm

Re: Using variables in a repository

Post by asdf » Tue Oct 18, 2016 11:10 am

Hi Jack,

Since Ranorex is based on SharpDevelop, you can use the whole .NET Framework to realize your approach.
You could combine your variable and the string "code" within a user code method.
In order to clarify my intention, I uploaded a small sample solution.

Feel free to ask me, if you have any questions regarding my sample solution.
I hope I could help you.

Kind regards,
asdf
Attachments
RepoVariables.zip
(18.06 KiB) Downloaded 69 times

User avatar
odklizec
Ranorex Guru
Ranorex Guru
Posts: 3914
Joined: Mon Aug 13, 2012 9:54 am
Location: Zilina, Slovakia

Re: Using variables in a repository

Post by odklizec » Tue Oct 18, 2016 11:49 am

Hi,

To be able to use variables in repository xpaths, you must replace unique ID attribute with "normal" "ID" attribute and use regular expression, like this:

Code: Select all

.//input[@id~'code'+$num]

Unique ID attribute cannot be combined with regexes and variables. The only way, how to combine unique ID attributes with other attributes, is like this:

Code: Select all

.//input[#'code1'][@enabled='True']

But this will not help you in this particular case.
Pavel Kudrys
Ranorex explorer at Descartes Systems

Please add these details to your questions:
  • Ranorex Snapshot. Learn how to create one >here<
  • Ranorex xPath of problematic element(s)
  • Ranorex version
  • OS version
  • HW configuration

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

Re: Using variables in a repository

Post by krstcs » Tue Oct 18, 2016 2:14 pm

While Pavel's approach is the correct and appropriate one for your case, note that you do not have (and might not want) to use the Regex operator ("~"). You can use the equality operator ("=") if you know for sure that the evaluated expression is going to match exactly.

If $num = 1:
.//input[@id~'code'+$num] <- Will find the first input with an id that has "code1" anywhere IN it.
.//input[@id='code'+$num] <- Will find only the first input that whose id equals exactly "code1"

If you have instances where you have the following IDs, you might not get what you want with the Regex operator looking for "code1", for example:

code1
code12
Xcode1

All of these would potentially be found with the regex version, depending on where they are in the app's document model, but the equality version would only find the first one ("code1").
Shortcuts usually aren't...

Hondo
Posts: 43
Joined: Thu Nov 03, 2016 12:03 am

Re: Using variables in a repository

Post by Hondo » Wed Dec 28, 2016 1:18 am

Hello,
Thanks for the little demo .sln, that helped.

I've successfully setup a Repository variable, applied it to a test module and bound it to a .csv data source in the TestCase.
Log file shows it the .csv values are being propagated through the repository variable. => Success!

However, getting the following error: "Invalid location string format: 'input1'
Here is the XPath: .//text[@text=$varBM]

Have tried a number of RegEx edits to the XPath, but no difference.

Help.
Thanks!

asdf
Posts: 174
Joined: Mon Mar 21, 2016 3:16 pm

Re: Using variables in a repository

Post by asdf » Wed Dec 28, 2016 11:10 am

Hi Hondo,

May I ask you to upload a Ranorex Snapshot of your application under test to the forum? This will allow us to check you RxPath.

Best regards,
asdf

Hondo
Posts: 43
Joined: Thu Nov 03, 2016 12:03 am

Re: Using variables in a repository

Post by Hondo » Wed Dec 28, 2016 5:33 pm

No sorry I cannot.. Proprietary concerns etc..

Hopefully this'll help.

Note: x64 WPF app.

Rooted Folder: .//container/container[3]/?/element[@automationid='Content']/?/container[@automationid='BookmarkersCtrlPanel']
=>Child Rooted Folder: .//container[@automationid='LayoutRoot']/?/list[@automationid='listBox_Bookmarks']/element[@automationid='Bd']
==>Child Rooted Folder: .//container/container/container?/element[@automationid='PART_ScrollContentPresenter']/list/container

===>List Item: listitem?/element?/?/text[@text='input1'] (this resolves just fine)
===>List Item: listitem?/element?/?/text[@text=$varBM] (passing in 'input1' generates "Invalid location string format: 'input1'" )

p.s. edit:
I can also go to the Action line in the module (Mouse>DoubleClick) and then select "HighLight Element" and it successfully resolves to 'input1'.. more proof the Repository variable is hooked up properly & the XPath definition is correct.

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

Re: Using variables in a repository

Post by Support Team » Mon Jan 02, 2017 3:41 pm

Hi Hondo,

May I ask you to send an email to [email protected] with a detailed description of your issue?
We could arrange a remote session, if it is necessary. Then we would be able to analyze your issue directly on your machine.

Thank you in advance.

Sincerely,
Matthias
.
Image

Hondo
Posts: 43
Joined: Thu Nov 03, 2016 12:03 am

Re: Using variables in a repository

Post by Hondo » Tue Jan 31, 2017 10:16 pm

Just a follow up..

In an entirely different and new script, I made Repository variables work.
Even using two different data sources (Simple and CSV).
Worked well. 8)

Not sure what the problem was in the original script. Will revisit it soon enough.