Variables in Repository not getting right value

Ask general questions here.
zoework
Posts: 38
Joined: Tue Jan 27, 2015 10:44 pm
Location: Charlotte, NC, USA

Variables in Repository not getting right value

Post by zoework » Tue May 26, 2015 5:02 pm

I am using variables in the repository that will 'randomly' select an option on the web page. I have this in the repository li[2]/span[$Question]/?/?/span , but it is replaced by this li[2]/span['1']/?/?/span during runtime. The original definition was listed as li[2]/span[1]/?/?/span. How do I get it to replace the variable without single quotes?
Thanks,
Zoë (aka zoework)

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

Re: Variables in Repository not getting right value

Post by krstcs » Tue May 26, 2015 5:50 pm

The problem is that Ranorex treats all variables in the XPath of a repo item as a single-quoted string, so it will always put the ticks around the value to make sure that the final XPath is correctly formatted. XPath requires all strings to have ticks.

The way to handle this type of thing is to add the attribute to the XPath, in your case the attribute would be "[@childIndex=$Question]". This would then be evaluated by XPath at runtime as "[@childIndex='1']".

I would also suggest that you make sure to have a well-defined parent for this item. Instead of 'li[2]', try to find an attribute of the li tag that is more consistent.



My final suggestion would be to find something besides the index to identify any element, if possible. Indexes are one of the most volatile ways to identify elements because they change whenever the layout changes, while other attributes might not.
Shortcuts usually aren't...

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

Re: Variables in Repository not getting right value

Post by odklizec » Tue May 26, 2015 6:20 pm

Hi,

Although I fully agree with krstcs' point about indexes and his explanation of variables being strings, there is something weird about this problem. Ranorex user guide mentions that it should be possible to use variables instead of hardcoded indexes...
/form/button[$var]
identifies the button with index equals the value stored in the variable $var
Source:
http://www.ranorex.com/support/user-gui ... xpath.html
So either the user guide is wrong, or something has got broken?
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

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

Re: Variables in Repository not getting right value

Post by Support Team » Wed May 27, 2015 2:24 pm

Hello guys,
The problem is that Ranorex treats all variables in the XPath of a repo item as a single-quoted string, so it will always put the ticks around the value to make sure that the final XPath is correctly formatted. XPath requires all strings to have ticks.
Unfortunately, this is not correct for variables, which are used as a wildcard for an index. As stated within our UserGuide (mentioned by odklizec):
VariablesInRxPath.png
VariablesInRxPath.png (3.09 KiB) Viewed 643 times
.. it should be possible to use a variable as an index within the RxPath.

Regards,
Robert
.
Image

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

Re: Variables in Repository not getting right value

Post by odklizec » Wed May 27, 2015 4:07 pm

I tried to use variable instead of hardcoded index and the element was found OK. So I guess there must be something else wrong?

Zoë, what version of Ranorex do you use? Could you please post a sample solution?
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: Variables in Repository not getting right value

Post by krstcs » Wed May 27, 2015 4:15 pm

Ah, my bad! Thanks for the correction Robert! I'm still learning stuff... :D
Shortcuts usually aren't...

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

Re: Variables in Repository not getting right value

Post by Support Team » Thu May 28, 2015 8:52 am

Hi,

@krstcs

Don't worry :) I'm glad that you share your experience with other users.

Regards,
Robert
.
Image