Problem Modifying xPath

Ask general questions here.
costamesakid
Posts: 94
Joined: Tue Jun 16, 2009 10:27 pm

Problem Modifying xPath

Post by costamesakid » Mon Feb 15, 2010 10:18 pm

I have elements on my application that have long xPaths such as:

element[@class='QWidget' and @instance='3']/element[@class='QWidget' and @instance='0']/element[@class='QWidget' and @instance='1']/element[@class='QWidget' and @instance='0']/element[@class='QWidget' and @instance='2']/element[@class='QWidget' and @instance='0']/element[@class='QWidget' and @instance='0']/element/element[@class='QWidget' and @instance='1']/element[@class='QWidget' and @instance='4']/button[@accessiblename='Save Image']

In the above case the path is to a button named 'Save Image'. The problem is that the instance numbers can change dynamically depending on how many elements are configured in the application for that form. So often times if I cannot reuse scripts that I record if I restart the application.

Since in the above example 'accessiblename' is a unique identifier for this button element I tried to modify the xPath to "element[@class='QWidget']/button[@accessiblename='Save Image']" but Ranorex then could no longer identify the object.

Since the accessiblename for this element is unique shouldn't I be able to just use class and accessiblename in the xPath? Thanks

User avatar
Support Team
Site Admin
Site Admin
Posts: 12145
Joined: Fri Jul 07, 2006 4:30 pm
Location: Houston, Texas, USA
Contact:

Re: Problem Modifying xPath

Post by Support Team » Tue Feb 16, 2010 8:47 am

Hi costamesakid!

Use // instead to check all descendants from an given object path.
So element[@class='QWidget']//button[@accessiblename='Save Image'] should work.

Also see the userguide for more information how to use RxPath: http://www.ranorex.com/support/user-gui ... xpath.html

Best regards,
Christian
Ranorex Team

costamesakid
Posts: 94
Joined: Tue Jun 16, 2009 10:27 pm

Re: Problem Modifying xPath

Post by costamesakid » Tue Feb 16, 2010 11:02 pm

That worked great! Thanks

Hit another snag though and I cannot find the solution in the Users Guide. How do I reference an object via xPath that has the same accessiblename as another object on a form. For instance I have 2 links with the same name:

element[@class='QWidget']//text[@accessiblename='Start']
element[@class='QWidget']//text[@accessiblename='Start']

User avatar
Ciege
Posts: 1336
Joined: Thu Oct 16, 2008 6:46 pm
Location: Arizona, USA

Re: Problem Modifying xPath

Post by Ciege » Wed Feb 17, 2010 12:20 am

Are the objects differentiable by another property other than accessiblename?
If both objects are truly children of the same element then they are probably differentiated by index, which is determined at runtime of the AUT.
If that is the case you can work out some custom code that has logic in it to determine which one you want to click. Or you should add a property differentiator in the xPath string.
If this or any response has helped you, please reply to the thread stating that it worked so other people with a similar issue will know how you fixed your issue!

Ciege...

User avatar
Support Team
Site Admin
Site Admin
Posts: 12145
Joined: Fri Jul 07, 2006 4:30 pm
Location: Houston, Texas, USA
Contact:

Re: Problem Modifying xPath

Post by Support Team » Tue Feb 23, 2010 3:15 pm

As ciege said, you can use the index to differentiate the two elements. You can do that using custom code or in the RxPath (note that indexes in XPath are one-based):
First element: element[@class='QWidget']//text[@accessiblename='Start'][1]
Second element: element[@class='QWidget']//text[@accessiblename='Start'][2]

Regards,
Alex
Ranorex Support Team

costamesakid
Posts: 94
Joined: Tue Jun 16, 2009 10:27 pm

Re: Problem Modifying xPath

Post by costamesakid » Fri Feb 26, 2010 12:00 am

This is interesting. I modified my xPaths to:

First element: element[@class='QWidget']//text[@accessiblename='Start'][1]
Second element: element[@class='QWidget']//text[@accessiblename='Start'][2]

In the repository I clicked 'Highlight Element' for each entry. As expected the 1st entry highlighted the 1st occurrence of the 'Start' link and the 2nd entry highlighted the 2nd occurrence of the 'Start' link. However, when I played back the script Ranorex clicked on the 1st occurrence of the 'Start' link 2 times.

Any ideas on why this would happen? Thanks

User avatar
Support Team
Site Admin
Site Admin
Posts: 12145
Joined: Fri Jul 07, 2006 4:30 pm
Location: Houston, Texas, USA
Contact:

Re: Problem Modifying xPath

Post by Support Team » Fri Feb 26, 2010 6:15 pm

costamesakid wrote:Any ideas on why this would happen?
Actually, no, if it works in Ranorex Spy, it should work in code, too - both use the same Ranorex library methods. Please, check that you use the RanoreXPaths in your code or in your repository, respectively!

Regards,
Alex
Ranorex Support Team

costamesakid
Posts: 94
Joined: Tue Jun 16, 2009 10:27 pm

Re: Problem Modifying xPath

Post by costamesakid » Wed Mar 31, 2010 8:49 am

Actually, there have been a few incidents of this happening from programmers/tester here and we have it well documented now so we have scheduled a Web Cast with Support/Development. I will post the results of said meeting. Thanks