xPath preceding-sibling last() not excepted by Ranorex

Technology specific object identification, supported applications, web technologies, and 3rd party controls.
bewiss
Posts: 16
Joined: Mon Feb 24, 2014 3:11 pm

xPath preceding-sibling last() not excepted by Ranorex

Post by bewiss » Tue Mar 25, 2014 4:37 pm

<text>
<text>
<picture>
<text>
<button[@name='myButton']
<text>
<text>


Hi,
I'm trying to identify a certain text element (highlighted in red) with Ranorex xPath:
/button[@name='myButton']/preceding-sibling::text[last()]

Running this example in any xPath on the fly onliner generater works fine but Ranorex refuses to allow the last() command. Any ideas? Due to xaml the architecture I'm not able to set an automation id directly to the text element. I have to use the button as a marker.

Thx
bewiss

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

Re: xPath preceding-sibling last() not excepted by Ranorex

Post by krstcs » Tue Mar 25, 2014 5:37 pm

You cannot use function calls inside the RanoreXPath.

Just use the "/button[@name='myButton']/preceding-sibling::text" without any attributes and it should return the text element just before the button. Note that this depends on the DOM structure, so if that changes (the elements are moved) then this will fail.

I would recommend talking to your developers and having them add a unique ID to the text element so it won't matter where it is.
Shortcuts usually aren't...

bewiss
Posts: 16
Joined: Mon Feb 24, 2014 3:11 pm

Re: xPath preceding-sibling last() not excepted by Ranorex

Post by bewiss » Thu Mar 27, 2014 7:12 pm

Hi krstcs,

Without any axis given, you'll get a list of all text elements containing in the calling node. So, your suggestion will not work. Even without function calls, using an index, e.g. /following-sibling::text[1], does not work within Ranorex.

Somtimes I just want to do a quick check on elements instead of implementing an aID.

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

Re: xPath preceding-sibling last() not excepted by Ranorex

Post by krstcs » Thu Mar 27, 2014 8:01 pm

I'm sorry, but that is incorrect.

Preceding/following-sibling returns the IMMEDIATE preceding/following element from the parent path, of the type specified after the "::". It works every time, and I use it often.

So, the path:

//div[#'mydiv']/following-sibling::buttontag

will return the buttontag element that is just after the div tag identified by 'mydiv' in an HTML document (for example).

It IS dependent on the order of the DOM that Ranorex reads, but it does work.

Also, I'm not sure what you mean by "axis".
Shortcuts usually aren't...

User avatar
testautomator
Posts: 67
Joined: Fri Oct 25, 2013 6:37 am
Location: Bangalore, India

Re: xPath preceding-sibling last() not excepted by Ranorex

Post by testautomator » Thu Apr 03, 2014 7:10 am

I second with krstcs,
It will return the immediate text after the button. Try using other attributes if you want to make it unique. use 'and' operator in the attributes ex: text[@name='name1' and @visible='true']

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

Re: xPath preceding-sibling last() not excepted by Ranorex

Post by Support Team » Thu Apr 03, 2014 2:45 pm

Hello,

Preceding- as well as following-sibling returns a list of all elements with the specified node name either preceding or following the current node.

Let’s assume that we have a form which holds five text fields:
SUT1.png
SUT1.png (3.01 KiB) Viewed 7927 times

I want to select the first sibling before ‘textBox2’, which is ‘textBox3’ in our sample.
SUT2.png
SUT2.png (3.79 KiB) Viewed 7927 times

In order to start our selection at node ‘textBox2”, we specify the RxPath as shown below:

Code: Select all

/form[@controlname='Form1']/text[@controlname='textBox2']
If we add “/preceding-sibling::text” to our specified RxPath we retrieve a list of all text boxes preceding ‘textBox2’.

Code: Select all

/form[@controlname='Form1']/text[@controlname='textBox2']/preceding-sibling::text
SUT3.png
SUT3.png (3.03 KiB) Viewed 7927 times
Now it’s time to add the index to our RxPath to retrieve the desired node ‘textBox3’.

Code: Select all

/form[@controlname='Form1']/text[@controlname='textBox2']/preceding-sibling::text[][1]
This RxPath selects the node ‘textBox3’.
SUT4.png
SUT4.png (2.52 KiB) Viewed 7927 times

The first index of preceding sibling (preceding-sibling::text[]) always refers to the parent node and filters accordingly to the specified node name, in our case “text”.

The second index (preceding-sibling::text[][]) selects an item from our result list.
In our case node ‘textBox3’ can be selected by using index one (preceding-sibling::text[][1]).

Regards,
Robert
.
Image

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

Re: xPath preceding-sibling last() not excepted by Ranorex

Post by krstcs » Thu Apr 03, 2014 3:06 pm

Robert, thank you for the clarification.

However, that has not been my experience. In my use, it returns only 1 element, not all. RXPath only returns the FIRST MATCHING element for anything it retrieves, so technically it cannot return more than 1 anyway.


On top of that, if what you say is correct, then the function is not named correctly, and should be "siblings" (plural). Naming something singular implies it will only return 1 item.
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: xPath preceding-sibling last() not excepted by Ranorex

Post by Support Team » Mon Apr 07, 2014 10:07 am

Hello,

Thank you for your input.

Yes you are right. The RxPath returns one single element, but following-sibling returns a list of all siblings of the current node as stated here:
• the following-sibling axis contains all the following siblings of the context node; if the context node is an attribute node or namespace node, the following-sibling axis is empty
http://www.w3.org/TR/xpath/#axes
The so-called axes are specified within the XML Path Language (XPath). Ranorex is not accountable for the naming of the axes even though it’s sometimes misleading.

Regards,
Robert
.
Image

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

Re: xPath preceding-sibling last() not excepted by Ranorex

Post by krstcs » Mon Apr 07, 2014 2:04 pm

Thanks Robert!

That makes sense (well the functionality anyway, the XPath spec is a mess... :D ).
Shortcuts usually aren't...