Create Appium Tests Using Object Spy & XPath

This tutorial is a part of our hands-on Appium Studio Training series.

Create robust Appium testing projects by Identifying objects in your mobile application by their native/web properties.

Watch the following tutorials to learn about Appium Studio object spy and XPath identifiers. 

Create Appium Tests Using Object Spy & XPath

Video Tutorials

”Object Spy” is a unique tool in Appium Studio that allows for the generation of XML dumps containing all elements that are present on-screen at any time. XPath queries can then be made using this information. A full set of properties for each element is also produced by the Object Spy.

If Native or Web elements of a specific page are being inspected using the Object Spy, an XML file is generated representatively using the element tree, wherein each node of the tree represents an element with its different properties. XPath 1.0  language syntax can be used through SeeTest to identify the tree elements.

A useful query language for use with XML trees, XPath allows users to access the properties of elements and the elements around them (relative to their location in the tree).This makes for much easier and more efficient identification in automation scripts.

You can visit w3schools to learn more about XPath, its syntax, and its implementation.

The following pages will go into detail on how to use the Object Spy tool and its features within Appium Studio.

Object Spy On-screen testing

The Object Spy is a new powerful tool to Appium Studio that allows users to access the properties of all objects and elements on-screen for testing. The properties can be implemented into all created tests. Native, Web, and Native Non-instrumented elements can all have their properties identified and accessed by the Object Spy.

Each of these cases is discussed below:

Appium Testing for Native Apps

Appium testing for Native apps

If a foreground instance of an instrumented application is running, the Object Spy will highlight all the elements using red rectangles, and the elements’ native properties are listed in red. A complete list of properties, including those specified for a particular element by the developer, is generated.

In the picture above, we see that all properties of the “Login” button can be seen simply by clicking on the “Object Spy” icon.

The Object Spy has multiple options available within the Appium Studio.

Extract Object

”Extract Object” provides a user with the ability to add a given object to Appium Studio’s object repository.

Extract Object

First, right-click the element you wish to extract.

Add to Object Repository

Then, click “Create a New Object” and fill out the details. This new object will be placed into your repository.

New Element

Fill in the details, such as the name and identifying properties, and then click “OK.”

If you’d rather create a reference to an object that already exists in your repository, you can click “Add to Existing.”

A list of available elements in your repository will come up, and you can select one to create a reference to it.

Add to Existing

Extract Multiple Elements

The “Extract Multiple Elements” option gives you the ability to add more than one object to your repository at any one time.

Extract Multiple Elements

Choosing this option will give you a list of all available elements on-screen.

Elements available on-screen

Select the ones you wish to add to your repository, then click “Add” and they will all be sent there.

Ignore Elements

Choosing “Ignore”, tells the Object Spy to ignore the element you are currently inspecting. This allows you to inspect elements that may be underneath or overlapping with the ignored element more easily.

Ignore Elements

In the above Example, we again use the “Login” element and ignore it. Clicking again lets us investigate the “LinearLayout” class since the Login button is being ignored.

Linear Layout

Copy Unique XPath

Copying XPaths is very useful because you will likely retrieve them for repeated use later in your testing protocol.

Copy Unique XPath

The XPath value for the Login button is as follows: XPath=//*[@text=’Login’]

Copy Unique XPath (SkipText)

Since the text properties may be dynamic and change often, the SkipText function allows a user to create or copy an XPath without any text properties.

Copy Unique XPath Skip Text

In this case, the Login buttons value will be: XPath=//*[@id=’loginButton’].

Native Non-Instrumented Mode

Objects in the device springboard, such as gallery, settings, and camera, can be identified by SeeTest. All elements of this type will be highlighted by orange rectangles, with native properties read in non-instrumented mode.

An Example of the home screen under Object Spy in this mode is given below:

Native Non-instrumented mode

All properties seen in the above example can have XPaths constructed and be used in tests.

Appium Testing for Web Application

Automating web applications is when the Object Spy tool becomes most useful. It can be used to view all developer-defined properties on a given webpage.

Appium Test for Web app

Dynamic XPaths can be created for each of these properties, and these XPaths can, in turn, be used in the application tests.

The options for web elements are the same as for native elements, with additional options as well as outlined ahead.

Elements Zoom in Web Dump

Viewport zoom is taken into account by Object Spy when inspecting web elements, in order to appropriately generate a web dump. CSS-zoom attributes are inherently ignored, however, (i.e style = “zoom: 4;” ) and those of HTML elements or media queries. This can impact the web dump and make it uncoordinated.

To address this issue, enable support.css.zoom in the app.properties file using the following line of text:

Elements Zoom in Web dump

Show in Browser

Selecting this will open the web page being inspected using your machine’s default browser, allowing you to investigate object properties on your own.

Show in Browser

An Example of this is shown below for Google:

Example of Show in browser

Open JavaScript Console

If you wish to develop or test JavaScript on web elements, you can open a JavaScript console in Appium Studio. This console is easy to use.

Simply open the Object Spy tool, right-click on any web element, and click “Open JavaScript Console.”

Open Javascript console

After that, a console dialog box will be opened. You can write JavaScript and execute it using the Run button. Once you are finished writing your script in the JavaScript console, you can simply copy-paste it into the Script parameters of your HybridRunJavaScript command. The result will be returned as a string.

Example: The following script extracts the text color of a given web element.

Javascript Console Example

Non-Instrumented-Dump

Non-instrumented XML dumps can be retrieved in an instrumented application, either in Chrome or Safari Mode.

See the following Example:

Non instrumented dump

There are even more options available for the Object Spy in Appium Studio.

XPath Filter

User-generated dynamic XPaths can be created with SeeTest’s XPath filter option. For a given XPath query, the filter specifies all the elements which meet its definition. A combination of one or more attributes or properties can constitute this XPath.

See the below Example in which all elements meeting a specific query are highlighted:

XPath Filter

Free Text Filter

If you are uncertain of a particular text property and its corresponding value, the Text Filter can help you. It filters elements by exactly these properties and values. For instance, in the below Example, all the elements with “English” in their property or value assignments have been highlighted with the Text Filter.

Free Text Filter

Regular Expression Filter

For customization of filtering, you can use the regular expression filter, which filters according to any user-defined regular expression. For instance, all elements that match the regular expression pattern “G.*” are filtered below.

Regular Expression Filter

If Native or Web elements of a specific page are being inspected using the Object Spy, an XML file is generated representatively using the element tree, wherein each node of the tree represents an element with its different properties. XPath 1.0 language syntax can be used through SeeTest to identify the tree elements.

A useful query language for use with XML trees, XPath allows users to access the properties of elements and the elements around them (relative to their location in the tree). This makes it much easier and more efficient identification in automation scripts.

As stated before, w3schools can be a great place to learn more about XPath.

How to Improve Appium Testing with XPath Feature

Use the following guide to prepare, install, and launch Appium testing with XPath:

See Android instrumentation for implementation on Android. XPath support is included for non-instrumented native applications.

To retrieve the native/web properties of all elements on-screen, click the “Spy” icon. Right-click the desired properties of a given element, one or multiple, and click Copy XPath. This will copy a query to the clipboard which will allow you to search for all elements that have the inspected properties and values.

For instance, in the below example we copy a query that searches for the id property “IMDB_branding” and the class property “android.widget.ImageView”.

Android Widget image view

A query can be dynamically created or edited using the XPath search box at the bottom of the tree window. Inserting a copied query into this box will check all the elements on-screen, and those that satisfy the query will then be highlighted in the device reflection.

XPath bottom Checkbox

In our Example, we see that the highlighted element is the one from which we extracted the properties in the first place. This is a strong validation that our query will seek the desired element and can be used in our automation scripts.

As shown in the below Example, there are times when this will not be the case:

XPath Filter Unchecked

Here, the id property of an element (with value label) has been retrieved, but after putting this query in the search box, 11 elements are highlighted.  This is why using these queries in an automation script without understanding the exact specifications can be dangerous. We recommend making sure your queries are specific enough to uniquely identify any element you wish to isolate.

A unique XPath query can be generated in SeeTest automatically for a specific element.

If SeeTest’s automatic query generation fails, the index parameter of the action commands can be used to distinguish between non-uniquely identified elements.

Advanced XPath Options

Integration with relative elements

../* – This command integrates a sibling’s properties with each element in the query. Every time the ../ command is encounter, the tool will ascend another level in the hierarchy, so ../../* can be used to integrate a property from an element’s father.

Example: Here we see the use of a label’s text properties in XPath to uniquely identify an arrow button to the right of the text.

XPath=//*[@id=’accessory’]

Advanced XPath Option Example 1

If only the id property with an accessory label is specified, then all 4 arrow buttons will meet the query. The arrow button itself has no other properties that will help us differentiate it, so instead, we use the text property of the label in order to uniquely identify the arrow.

We use the ../* command in our query, which eliminates all elements that don’t possess a sibling in the tree with a text property and a US TV Recaps value. This uniquely identifies the arrow button we are looking for.

XPath=//*[@id=’accessory’ and ../*[@text=’US TV Recaps’]]

Advanced XPath Option Command 2

Check our upcoming tutorial to know more about integrating IDE and running Appium tests.

PREVIOUS Tutorial #7 | NEXT Tutorial #9