When using the object spy to inspect the Native or Web elements of a specific page, the element tree is a representation of an XML file, with each element representing a node with different properties in the tree module. SeeTest gives the user the option of using XPath 1.0language syntax in order to identify elements in the tree.
XPath is a powerful query language for XML trees, which gives the ability to use multiple properties of both the element you are looking for and the elements around it (along with their relative location in the tree) in order to be able to always get the identification you are looking for throughout the automation scripts.
To learn more about the XPath language and how to use its syntax, please visit w3schools.
How to use the XPath feature?
- Prepare, Install & Launch the Application using the following instructions:
For iOS: See iOS instrumentation
Use the Spy icon button in order to get the Native/Web properties of all the objects on the screen. The easiest way to create a simple XPath query is by marking the wanted properties of an element (one or multiple properties can be used), right clicking on them and then clicking on Copy XPath. This will copy to the clipboard a query that will look for elements that have the properties and values that are looked for. In this example, we will create a query that will look for elements that have both id property with IMDB_branding as the value and class property with android.widget.ImageView as the value.
- The XPath search box on the bottom of the tree window gives the ability to dynamically create and edit a query. The best way to check the elements a query identifies is by inserting the query into that search box. Only elements that meet the query will still be highlighted on the device's reflection once a query is inserted in the search box.
- In the above example, the only element that is left highlighted is the one we extracted the properties from and wanted to identify. That means that the query gives unique identification of the desired element, and can be safely used in the automation scripts. But that is not always the case, as demonstrated below.
- In the above query, we extracted just the id property of one of the elements (with value label), and when checking the query in the search box, we can see that there are 11 other elements on the screen that meet the query and are identified by it. That means that using this query in the automation script can be dangerous since any one of the elements can be identified when this query is called during an action command. That is why it is always recommended to try and create a query that will give unique identification of the element you are looking for.
- SeeTest also has a feature that will automatically generate a unique XPath query for a specific element.
If a unique query still cannot be generated, it is possible to use the index parameter of the action commands to differentiate between the elements identified by the query.
Advanced XPath Options
integrate with Relative elements
- ../* - Can be used to integrate the properties of a sibling of an element in the query. Each instance of ../ will go up the hierarchy another level, so use ../../* to integrate a property of the father of an element.
Example: As mentioned above, the XPath language gives the ability to use properties of other elements in order to identify the element we are looking for. In the following example, we will use the text property of one of the labels in order to uniquely identify the arrow button to the right of it.
- Using just the id property with an accessory label is not a unique identifier, as all 4 arrow buttons meet this query. Notice that there are no other properties for the arrow button element itself that can help to separate it from the others, so we will use the text property of the label itself in order to get the unique identification. In order to do that, we will add to the query another part using ../*, that will also eliminate all elements that don't have a sibling in the tree with a text property and US TV Recaps value. The arrow button will now be uniquely identified.
xpath=//*[@id='accessory' and ../*[@text='US TV Recaps']]