Page 1 of 1

Help selecting xpath that contains elements

Posted: Tue Apr 22, 2014 2:50 pm
by puntapret
Hello,

I'm having trouble on how to do identifying an xpath element, maybe someone with strong xpath experience can help me out.

I'm using Silverlight and Ranorex 4.1.6.

Basically in my silverlight application I have 2 links all with the same AutomationId, and inside i have either two texts or one texts with the same captions. All texts are clickable.

Code: Select all

     Link 'HpButton'
      - Text 'ADMIN'
      - Text 'BBBB'
   Link 'HpButton'
      - Text 'BBBB'
   
How i can choose using XPath, the Text 'BBBB' that contains 'ADMIN' (the first one) and the Text 'BBBB' without 'ADMIN' (the second one)

I managed with my limited xpath experience choosing the Link 'HpButton' (the first one) that contains ADMIN and BBBB, but i really don't know how to choose only the inside BBBB text

Code: Select all

link[@automationid='HpButton']/text[@name='BBBB' and @name='Admin']/
Thank you

Re: Help selecting xpath that contains elements

Posted: Tue Apr 22, 2014 3:09 pm
by krstcs
It would be helpful if you could post a snapshot of the application so we can see the structure. http://www.ranorex.com/support/user-gui ... files.html

Also, the user guide goes into good detail about relative paths, which is what I'm using below. http://www.ranorex.com/blog/ranorexpath-tips-and-tricks

Try this:

Code: Select all

Link_ADMIN      -->  link[@automationid='HpButton']/text[@name='Admin']/following-sibling::text[@name='BBBB']
Link_NON_ADMIN  -->  link[@automationid='HpButton']/text[@name='Admin']/parent::link/following-sibling::link[@automationid='HpButton']/text[@name='BBBB']

Re: Help selecting xpath that contains elements

Posted: Tue Apr 22, 2014 3:16 pm
by puntapret
Thank you krstcs. It's exactly what i want.

I didn't post the snapshot, because i thinks it's just a lack of my XPath skills :|

Re: Help selecting xpath that contains elements

Posted: Tue Apr 22, 2014 3:25 pm
by krstcs
OH, one warning: The following/preceding-sibling elements are dependent on the structure of the dom. If the dom changes, the paths may not work.

Re: Help selecting xpath that contains elements

Posted: Wed Apr 23, 2014 8:31 am
by puntapret
krstcs wrote:OH, one warning: The following/preceding-sibling elements are dependent on the structure of the dom. If the dom changes, the paths may not work.
Is there any elegant way to avoid dom changes breaking paths ?

Re: Help selecting xpath that contains elements

Posted: Wed Apr 23, 2014 9:47 am
by mzperix
Hi puntapret,

Did you try for NON_ADMIN:

Code: Select all

link[@automationid='HpButton']/text[@name~'BBBB' and @name!~'Admin']/
So @name!~'Admin' means: name does not contain the string 'Admin'

Regards,
Zoltan

Re: Help selecting xpath that contains elements

Posted: Wed Apr 23, 2014 1:46 pm
by krstcs
Zoltan, that will still find the first instance of BBBB, not the second. All that says is "Find me the text object that is BBBB AND not ADMIN." The first BBBB matches that (it is BBBB and is NOT ADMIN), so Ranorex will stop there. There is no way to say "Find me an object that has a child of BBBB and doesn't have a child of ADMIN", unfortunately, as that is what we are looking for here. (As far as I know! :D Maybe the support team can enlighten us if there is a way. I would love to hear it!)



puntapret, there is no way to do it the way your application is structured, from what I can see. If I were in your position, I would ask the developers to add a unique way to identify the elements that wouldn't change if the dom structure changes. For instance, add unique ids or class attributes to the two link objects. The first could be @id='Link1' and the second could be @id='Link2', for all we care, as long as those ids don't change if the structure changes.

Without doing something along those lines, you are going to continue to run into issues. Remember, Ranorex is software and runs on a computer, which means it can't think or reason the way you and I can, so we have to be able to tell it EXACTLY what we want it to do. It can't just look at two items that match and pick the right one, it will always pick the first one. In this case we can't get by without using an index or other structure-based mechanism unless we have some unique identifier, because of the limits of XPath and Ranorex's logic (mainly XPath...).

Re: Help selecting xpath that contains elements

Posted: Thu Apr 24, 2014 8:09 am
by mzperix
Hi all,

krstcs is right, I misinterpreted the situation. I thought the text BBB and Admin are not separate elements :)

And I would go along with krstcs on having a conversation with the developers. It is always a good idea to have an active kommunication with them.

Regards,
Zoltan

Re: Help selecting xpath that contains elements

Posted: Thu Apr 24, 2014 10:10 am
by puntapret
krstcs wrote: puntapret, there is no way to do it the way your application is structured, from what I can see. If I were in your position, I would ask the developers to add a unique way to identify the elements that wouldn't change if the dom structure changes. For instance, add unique ids or class attributes to the two link objects. The first could be @id='Link1' and the second could be @id='Link2', for all we care, as long as those ids don't change if the structure changes.

Without doing something along those lines, you are going to continue to run into issues. Remember, Ranorex is software and runs on a computer, which means it can't think or reason the way you and I can, so we have to be able to tell it EXACTLY what we want it to do. It can't just look at two items that match and pick the right one, it will always pick the first one. In this case we can't get by without using an index or other structure-based mechanism unless we have some unique identifier, because of the limits of XPath and Ranorex's logic (mainly XPath...).
Krstcs, I think i've no choice other than crossing my finger for each tests. I believe it will be too hard to ask the UI developpers to follow some rules. Since the Quality department is basically only me, and for this time it's just for the title, so that we can show to the client that we have a quality controls. I hope it will changes in the future though.

Re: Help selecting xpath that contains elements

Posted: Thu Apr 24, 2014 3:15 pm
by krstcs
I understand. I've been in that situation myself.

I would suggest that you keep at it though. Try to make the devs see what value you bring to them by being able to test and return results more quickly and effectively if they follow some standard practices. Microsoft best practices for Silverlight and all WPF-based applications is to use UIAutomation framework. If they are not doing that, then they aren't doing it the way MS designed the systems.

Most developers want to write good code, so if you let them see how it will help, they may be open to it.

Re: Help selecting xpath that contains elements

Posted: Tue May 13, 2014 12:43 pm
by puntapret
Well, i'm still having a problem with this one, the link finally has more than one :

Code: Select all

 Link 'HpButton'
      - Text 'ADMIN'
      - Text 'BBBB'  
 Link 'HpButton'
      - Text 'ADMIN'
      - Text 'CCCC'
 Link 'HpButton'
      - Text 'ADMIN'
      - Text 'DDDD'
Link 'HpButton'
      - Text 'BBBB'
   Link 'HpButton'
      - Text 'CCCC'
   Link 'HpButton'
      - Text 'DDDD'
The links was range in 2 columns, sorry i can not post the snapshot (company policy) :

Code: Select all

-------------------------------------------------
|                              |                          |
|     BBBB - Admin     |            BBBB        |
|                              |                          |
-------------------------------------------------
|                              |                          |
|     CCCC- Admin     |            CCCC       |
|                              |                          |
-------------------------------------------------
|                              |                          |
|     DDDD - Admin     |            DDDD      |
|                              |                          |
-------------------------------------------------
When i used the code given by krstcs for BBBB and BBBB Admin it worked well, but when i started using to find the CCCC and DDDD, it keeps finding the CCCC-Admin and DDDD-Admin.

Re: Help selecting xpath that contains elements

Posted: Tue May 13, 2014 2:58 pm
by krstcs
Can you post the XPath that you are using for the CCCC and DDDD items (both ADMIN and non-ADMIN)?

Are you data-driving the path? (I.e., are you using variables in the XPath?)

Re: Help selecting xpath that contains elements

Posted: Wed May 14, 2014 11:11 am
by puntapret
I just copied what you gave me above for CCCC and DDDD (admin and non admin)

Code: Select all

Admin : 
link[@automationid='HpButton']/text[@name='Admin']/following-sibling::text[@name='CCCC']
link[@automationid='HpButton']/text[@name='Admin']/following-sibling::text[@name='DDDD']

Non Admin

link[@automationid='HpButton']/text[@name='Admin']/parent::link/following-sibling::link[@automationid='HpButton']/text[@name='CCCC']
link[@automationid='HpButton']/text[@name='Admin']/parent::link/following-sibling::link[@automationid='HpButton']/text[@name='DDDD']

Nope, no data driven, since i've got only 6 links and it's quite static

Re: Help selecting xpath that contains elements

Posted: Wed May 21, 2014 1:24 pm
by Support Team
Hello puntapret,

Unfortunately it is really hard to analyze your issue due to the lack of information. May I ask you to contact [email protected] if you are allowed to provide more information to us?

Thank you for your understanding.

Regards,
Robert