Help selecting xpath that contains elements

Technology specific object identification, supported applications, web technologies, and 3rd party controls.
puntapret
Posts: 34
Joined: Fri Sep 14, 2012 11:05 am

Help selecting xpath that contains elements

Post by puntapret » Tue Apr 22, 2014 2:50 pm

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

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

Re: Help selecting xpath that contains elements

Post by krstcs » Tue Apr 22, 2014 3:09 pm

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']
Shortcuts usually aren't...

puntapret
Posts: 34
Joined: Fri Sep 14, 2012 11:05 am

Re: Help selecting xpath that contains elements

Post by puntapret » Tue Apr 22, 2014 3:16 pm

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 :|

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

Re: Help selecting xpath that contains elements

Post by krstcs » Tue Apr 22, 2014 3:25 pm

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.
Shortcuts usually aren't...

puntapret
Posts: 34
Joined: Fri Sep 14, 2012 11:05 am

Re: Help selecting xpath that contains elements

Post by puntapret » Wed Apr 23, 2014 8:31 am

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 ?

mzperix
Posts: 137
Joined: Fri Apr 06, 2012 12:19 pm

Re: Help selecting xpath that contains elements

Post by mzperix » Wed Apr 23, 2014 9:47 am

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

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

Re: Help selecting xpath that contains elements

Post by krstcs » Wed Apr 23, 2014 1:46 pm

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...).
Shortcuts usually aren't...

mzperix
Posts: 137
Joined: Fri Apr 06, 2012 12:19 pm

Re: Help selecting xpath that contains elements

Post by mzperix » Thu Apr 24, 2014 8:09 am

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

puntapret
Posts: 34
Joined: Fri Sep 14, 2012 11:05 am

Re: Help selecting xpath that contains elements

Post by puntapret » Thu Apr 24, 2014 10:10 am

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.

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

Re: Help selecting xpath that contains elements

Post by krstcs » Thu Apr 24, 2014 3:15 pm

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.
Shortcuts usually aren't...

puntapret
Posts: 34
Joined: Fri Sep 14, 2012 11:05 am

Re: Help selecting xpath that contains elements

Post by puntapret » Tue May 13, 2014 12:43 pm

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.

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

Re: Help selecting xpath that contains elements

Post by krstcs » Tue May 13, 2014 2:58 pm

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?)
Shortcuts usually aren't...

puntapret
Posts: 34
Joined: Fri Sep 14, 2012 11:05 am

Re: Help selecting xpath that contains elements

Post by puntapret » Wed May 14, 2014 11:11 am

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

User avatar
Support Team
Site Admin
Site Admin
Posts: 11709
Joined: Fri Jul 07, 2006 4:30 pm
Location: Graz, Austria

Re: Help selecting xpath that contains elements

Post by Support Team » Wed May 21, 2014 1:24 pm

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
.
Image