Question re xPath for cell in Ranorex Web Testing Example

Ask general questions here.
Fergal
Certified Professional
Certified Professional
Posts: 298
Joined: Tue Feb 18, 2014 2:14 pm
Location: Co Louth, Ireland
Contact:

Question re xPath for cell in Ranorex Web Testing Example

Post by Fergal » Fri Nov 14, 2014 11:38 am

On http://www.ranorex.com/web-testing-examples/ there is a value of "green" in the third cell of "rows 2".

I can successfully locate that value using the following xPath:

/dom[@caption='Ranorex Test Page']//table[#'simpletable']//td[@innertext='green']

However, I cannot identify that value with the following xPath:

/dom[@caption='Ranorex Test Page']//table[#'simpletable']/tr/td[@innertext='green']

The <td> is a child of a <tr>, so what is wrong with my second xPath?

Thanks!

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

Re: Question re xPath for cell in Ranorex Web Testing Example

Post by krstcs » Fri Nov 14, 2014 4:35 pm

You're missing the /tbody tag in between the /table and /tr tags.

The structure is:

Code: Select all

<table>
  <tbody>
    <tr>
      <td/>
    </tr>
  </tbody>
</table>
So you need to either add the /tbody to the path, or put a wildcard, or an extra slash, like one of these:

Code: Select all

/dom[@caption='Ranorex Test Page']//table[#'simpletable']/tbody/tr/td[@innertext='green']
/dom[@caption='Ranorex Test Page']//table[#'simpletable']/?/tr/td[@innertext='green']
/dom[@caption='Ranorex Test Page']//table[#'simpletable']//tr/td[@innertext='green']
The "?" means, any element can go here.
The "//" means, find the next element ANYWHERE under the previous element and it's children.
Shortcuts usually aren't...

Fergal
Certified Professional
Certified Professional
Posts: 298
Joined: Tue Feb 18, 2014 2:14 pm
Location: Co Louth, Ireland
Contact:

Re: Question re xPath for cell in Ranorex Web Testing Example

Post by Fergal » Wed Nov 19, 2014 1:00 pm

Thanks very much for your most helpful reply krstcs. The three paths you give, work perfectly and based on your explanation I can understand why they work.

Given that your paths work, why does my original path (shown below) also work? It looks to me, as if it is missing a "/", but it still works? Your paths have three "/"s between the table and td, but mine only has two.

Code: Select all

/dom[@caption='Ranorex Test Page']//table[#'simpletable']//td[@innertext='green']
Also when I track the "green" cell with Ranorex Spy, it doesn't include "tbody" in the path, it gives me the path shown above. Why is it not picking up "tbody"?

Thanks again!

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

Re: Question re xPath for cell in Ranorex Web Testing Example

Post by krstcs » Wed Nov 19, 2014 2:44 pm

I'll answer your last question first: Ranorex has 3 different methods/algorithms it uses to generate the XPath. The reason your path looks like it does is because of the method you are using ("StepCostReduce"). You can change the XPath generation mode by going to: Global Settings -> Advanced Tab. There you will find, next to the bottom, "RanoreXPath generation mode". You can choose "Simple" (path will contain EVERY element), "Reduce" (what I use, path will contain most elements as long as they are meaningful), and "StepCostReduce" (the default, path will ONLY contain the most meaningful elements). See this page for more specific info.


Your original path works because the "//" means "Any element under this one that matches the next part of the XPath". So your path reads like this:

1. Find the WebDocument (/dom) with a caption of 'Ranorex Test Page'.
2. Find a table ANYWHERE under the WebDocument in (1.) that has an id = 'simpletable'.
3. Find a tdtag ANYWHERE under the table in (2.) that has an innertext = 'green'.

When using unique ids (the '#' sign) you can get away with this because the unique id is hashed in a way that makes it very fast for Ranorex to find it. If you are using unique ids, you should use the "//" so the path is shorter.

When NOT using unique ids, having the '//' makes Ranorex search EVERY path until it finds the first matching element. So with (3.) above, Ranorex will search every child of the table (id = simpletable) and every child of every child, until it finds the FIRST innertext that has 'green' as the value. This can take a while if there are a lot of elements and could cause the test to fail due to timeouts.

This is why I use Reduce instead of StepCostReduce. It usually gives a path that is closer to what I want. But, your experience might be different.
Shortcuts usually aren't...

Fergal
Certified Professional
Certified Professional
Posts: 298
Joined: Tue Feb 18, 2014 2:14 pm
Location: Co Louth, Ireland
Contact:

Re: Question re xPath for cell in Ranorex Web Testing Example

Post by Fergal » Wed Nov 19, 2014 4:54 pm

Thanks very much for your in-depth reply krstcs. That really is super helpful and it gives me a much better understanding of how XPath works. I will try using "Reduce" to see if that improves my tests.

Thanks again!