-LIST
--Listitem
---Headline
---Status
Step1: I get all the headlines in a list via the Find<Text>('text[@automationid='headline']"') method. I now have a IList<Text> headlines.
Step2: I find the element with a specific headline via a Linq Query like myHeadlines.First(x=>x.TextValue == "Hadline1");. I now have the element Text specialHeadline.
Step3: I now want know the status of that listitem with my special headline. But how can I find the closest element with the automationid status without messy code?
I might be able to say myHeadline.Parent.Find but what if there is an extra container inbetween etc. I am looking for an equivalent to https://api.jquery.com/closest/ in Ranorex.
Find Closest Element by Id
-
- Posts: 254
- Joined: Tue Mar 24, 2015 5:05 pm
- Location: Des Moines, Iowa, USA
Re: Find Closest Element by Id
Could you provide an example of the DOM?
The little outline you've provided seems like you've got the "Headline" object and you could just grab the status by using the "following-sibling" axis.
this will get the following sibling that is a status tag.
Something like this should work for either
-LIST
--Listitem
---Headline
---Status
OR
-LIST
--Listitem
---Headline
---ExtraContainer
---Status
Let me know if this helps or if I misunderstood the situation.
The little outline you've provided seems like you've got the "Headline" object and you could just grab the status by using the "following-sibling" axis.
Code: Select all
objHeadline.findsingle("following-sibling::status")
Something like this should work for either
-LIST
--Listitem
---Headline
---Status
OR
-LIST
--Listitem
---Headline
---ExtraContainer
---Status
Let me know if this helps or if I misunderstood the situation.
Doug Vaughan
Re: Find Closest Element by Id
Thanks following-sibling is a good hint but not the real solution. Sometimes the text is in an extra label field etc. and sibling takes only elements on the same tree-level (as far as I understood). The closest method would scan for the nearest element in the tree that matches the criteria. Is there no such thing in Ranorex?
-
- Posts: 254
- Joined: Tue Mar 24, 2015 5:05 pm
- Location: Des Moines, Iowa, USA
Re: Find Closest Element by Id
Ranorex has the concept of optional location.
Example: /form/container?/button
This is a pattern I use on a regular basis. I'll use something like this:
to describe this:
So back to your sample DOM
-LIST
--Listitem
---Headline
---Status
or
-LIST
--Listitem
---Headline
---ExtraContainer
----Status
Could be achieved as such
The following-sibling::* will grab ALL of the siblings that follow Headline regardless of tag.
The descendant-or-self::status will scan each of those siblings to grab the Status tag(s) that are either a direct sibling of Headline or a descendant of a direct sibling or Headline.
To my knowledge there is no method that will scan for the closest element in all directions at once. One of the various find methods are your best bet, but you still need to provide some kind of relationship between the two elements.
If the text you're looking for is sometimes in the Status element and sometimes a descendant of that Status element, I would go about getting the status element as above then search using a regular expression or something like descendant-or-self::*[@innertext~[A-Za-z]+]
or this:
If using a combination of following-sibling and descendant-or-self doesn't get you what you need then I have a fundamental misunderstanding of your situation and I really would need to see the DOM to help.
Example: /form/container?/button
I prefer doing this using regular old Xpath:optional location step fitting a container adapter between form and button; identifies both, '/form/container/button' and '/form/button'
Code: Select all
'/form/descendant-or-self::button
Code: Select all
descendant::label[@innertext~'(Last Name|Paternal Last)']/parent::td/following-sibling::td/descendant::input
So back to your sample DOM
-LIST
--Listitem
---Headline
---Status
or
-LIST
--Listitem
---Headline
---ExtraContainer
----Status
Could be achieved as such
Code: Select all
objHeadline.findsingle("following-sibling::*/descendant-or-self::status")
The descendant-or-self::status will scan each of those siblings to grab the Status tag(s) that are either a direct sibling of Headline or a descendant of a direct sibling or Headline.
To my knowledge there is no method that will scan for the closest element in all directions at once. One of the various find methods are your best bet, but you still need to provide some kind of relationship between the two elements.
If the text you're looking for is sometimes in the Status element and sometimes a descendant of that Status element, I would go about getting the status element as above then search using a regular expression or something like descendant-or-self::*[@innertext~[A-Za-z]+]
or this:
Code: Select all
objHeadline.findsingle("following-sibling::*/descendant-or-self::status/descendant-or-self::*[@innertext~[A-Za-z]+]")
You do not have the required permissions to view the files attached to this post.
Doug Vaughan