Index Identification

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

Index Identification

Post by Fergal » Fri Sep 05, 2014 11:30 am

Ranorex can use an index value for identifying elements in the repository. E.g. the example in the user guide, uses a column index and a row index to identify a cell containing KeePass version number.

When index values are used to identify an element on the web page, where are those index values taken from?

Are index values the best way to identify context menu items on a webpage built with HTML and JavaScript?

Thanks!

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

Re: Index Identification

Post by krstcs » Fri Sep 05, 2014 2:22 pm

I recommend that you not use indexes at all, unless there is no other way to identify an element.

The index is based on the item's CURRENT (at the time that you create the element) position relative to it's parent and siblings. This means that if the AUT changes the order of elements, your index will no longer be pointing at the intended element.

Also, XPath indexes are 1-based, not 0-based like .NET (and most other languages).

For example, let's say you create an indexed XPath to an element:

<html>
<body>
<div id='MyIndexedDiv' />
</body>
</html>


MyIndexedDiv -> /body/div[1]

Now, let's say the devs add another div in body, but put it before your div, like this:
<html>
<body>
<div id='NewDiv' />
<div id='MyIndexedDiv' />
</body>
</html>


Now, instead of pointing at MyIndexDiv, your xpath is pointing at NewDiv.

If you don't have IDs to use, you might ask your devs to add them. It usually doesn't take long and will help testing return results more quickly, helping them make better code.

Rule of thumb: Use id attributes if there is one, and indexes ONLY after every other option has been exhausted.
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: Index Identification

Post by Fergal » Fri Sep 05, 2014 3:21 pm

krstcs wrote:...If you don't have IDs to use, you might ask your devs to add them. It usually doesn't take long and will help testing return results more quickly, helping them make better code....
Thanks a lot for your reply krstcs. Are you referring to IDs in the html, such as the "demo" ID in the code below;

<p id="paraOne">Text</p>

If IDs are present, will they show up in the WebElement section of the Spy?

Thanks again!

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

Re: Index Identification

Post by krstcs » Fri Sep 05, 2014 4:26 pm

Yes, that is exactly the ID I'm referring to, in your example 'id="paraOne"' is what I would use.

Ranorex should automatically attempt to use those IDs to identify elements. ID and AutomationID are usually the two highest weighted attributes that Ranorex will attempt to use. And, yes, it will be in the WebElement section in spy.



Also, the IDs should be unique inside the containing element, and preferably unique across the entire page. This makes it even better.
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: Index Identification

Post by Fergal » Mon Sep 08, 2014 9:14 am

Thanks krstcs, that's very helpful.