Identifying two similar elements with dynamic IDs

Technology specific object identification, supported applications, web technologies, and 3rd party controls.
rmcgsi
Posts: 2
Joined: Tue Nov 11, 2014 4:09 pm

Identifying two similar elements with dynamic IDs

Post by rmcgsi » Tue Nov 11, 2014 4:38 pm

On the web page, there are two very similar elements, but I am having trouble identifying them separately.

For example, given the following two elements:
  • /dom[@domain='my.domain.com']//ul[#'id-4676']/li[@innertext='This is incorrect.']
  • /dom[@domain='my.domain.com']//ul[#'id-9852']/li[@innertext='This is incorrect.']
The dynamic part of the path would be //ul[#'id-????']. I've changed the XPath to use //ul[@id~'id-[0-9]+'], but I am only able to identify one of them when tracking with Ranorex Spy.

What would be the best way to handle this situation? I don't believe there are any other attributes I can find to differentiate them.

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

Re: Identifying two similar elements with dynamic IDs

Post by krstcs » Wed Nov 12, 2014 3:37 pm

If there truly are no other attributes that are different then you will have trouble identifying the second object (Ranorex always stops searching when it finds the first object that matches the XPath).


If you could post a snapshot (not a screenshot) of the application under test, we might be able to see something else. See this page for information on creating a snapshot.

For example, if there are other elements that have titles or other text that IS different, you might be able to use those to identify the object using relational operators in the XPath.

Say you have this structure:

Code: Select all

<html>
  <body>
    <div>
      <h>Header 1</h>
      <ul>
        <li>This is incorrect.</li>
      </ul>
    </div>
    <div>
      <h>Header 2</h>
      <ul>
        <li>This is incorrect.</li>
      </ul>
    </div>
  </body>
</html>
You could make your XPath like this:
//div/h[@innertext='Header 1']/following-sibling:ul/li[@innertext='This is incorrect.']
//div/h[@innertext='Header 2']/following-sibling:ul/li[@innertext='This is incorrect.']

See this page for information on relationship operators.
Shortcuts usually aren't...

rmcgsi
Posts: 2
Joined: Tue Nov 11, 2014 4:09 pm

Re: Identifying two similar elements with dynamic IDs

Post by rmcgsi » Wed Nov 12, 2014 5:50 pm

You could make your XPath like this:
//div/h[@innertext='Header 1']/following-sibling:ul/li[@innertext='This is incorrect.']
//div/h[@innertext='Header 2']/following-sibling:ul/li[@innertext='This is incorrect.']
Thank you for the suggestion, krstcs.

This was the solution I was looking for. After recording, I did not take into account that the XPath expression can be made explicit even further. In other words, I modified the paths and made the differentiation on a div group.

Modified original example:
/dom[@domain='my.domain.com']//div[1]/ul[#'id-4676']/li[@innertext='This is incorrect.']
/dom[@domain='my.domain.com']//div[2]/ul[#'id-9852']/li[@innertext='This is incorrect.']

Based on both our examples, we can differentiate the elements by property or by element index.

Again, thanks for your help. Your solution works for me :D

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

Re: Identifying two similar elements with dynamic IDs

Post by krstcs » Wed Nov 12, 2014 9:20 pm

A couple of things:

1. Indexes should be used as a last resort. If your developers change the layout of the page, even just changing the position of those two divs, your test could false-positive. It is almost always better to find another way to identify the element path, one that will not be in-correct if the structure changes. (I understand that there are times we, as testers, can't get around having to redo our object structure, but we should try to minimize it.)

2. If you using the index, you probably don't want to use the unique id ("#'id-4676'"). If the id is actually unique then you should use it instead of the index, but if it isn't, you want to get rid of it all-together.

/dom[@domain='my.domain.com']//div[1]/ul/li[@innertext='This is incorrect.']
Shortcuts usually aren't...