Advanced Locator Strategies
Appium is built to implement the Selenium WebDriver JSON Wire Protocol specification, extended to enable the automation of mobile devices. By and large, the extensions to the WebDriver spec are encoded within the draft Mobile JSON Wire Protocol spec.
In order to find elements in a mobile environment, Appium implements a number of locator strategies that are specific to, or adaptations for, the particulars of a mobile device. Three are available for both Android and iOS:
class name strategy is a
string representing a UI element on the current view.
- For iOS it is the full name of a UIAutomation class, and will begin with
UIA-, such as
UIATextFieldfor a text field. A full reference can be found here.
- For Android it is the fully qualified name of a UI Automator class, such
android.widget.EditTextfor a text field. A full reference can be found here.
The client libraries for Appium support getting a single element, or multiple elements, based on the
class name. This functionality is in the Selenium clients (e.g., Python).
accessibility id locator strategy is designed to read a unique identifier for a UI element. This has the benefit of not changing during localization or any other process that might change text. In addition, it can be an aid in creating cross-platform tests, if elements that are functionally the same have the same accessibility id.
- For iOS this is the
accessibility identifierlaid out by Apple here.
- For Android the
accessibility idmaps to the
content-descriptionfor the element, as described here.
For both platforms getting an element, or multiple elements, by their
accessibility id is usually the best method. It is also the preferred way, in replacement of the deprecated
The client libraries specific to Appium support getting elements by
accessibility id. The methods are not yet implemented in the standard Selenium clients.
xpath locator strategy is also available in the WebDriver protocol, and exposes the functionality of XPath language to locate elements within a mobile view. An XML representation of the view is created in Appium, and searches are made against that image.
The Selenium clients have methods for retrieving elements using the
xpath locator strategy.
In the mobile environment,
ids are not, as in WebDriver, CSS ids, but rather some form of native identifier.
- For iOS the situation is complicated. Appium will first search for an
accessibility idthat matches. If there is none found, a string match will be attempted on the element labels. Finally, if the id passed in is a localization key, it will search the localized string.
- For Android, the
idis the element’s
Accessing the underlying automation tools
Finally, Appium exposes two methods of using the underlying device automation technology directly, in order to find elements. This is especially useful when a test needs some fine-grained control over finding elements.
Android’s UI Automator framework provides a number of ways to find elements. You can use the UI Automator API, in particular the UiSelector class to locate elements. In Appium you send the Java code, as a string, to the server, which executes it in the application’s environment, returning the element or elements.
To find, for instance, all elements that are clickable in a view, you would use the following Java code (adapted from here):
UiDevice device = UiDevice.getInstance(getInstrumentation());
UiObject cancelButton = device
In Appium, this is reduced to the
UiSelector code itself, which is sent to the appropriate
command = ‘new UiSelector().text(“Cancel”)).className(“android.widget.Button”)’
var textField = UIATarget.localTarget().frontMostApp().mainWindow().textFields()[“User Text”];
UIATarget.localTarget().frontMostApp().mainWindow() call. That is to say,
command = ‘.textFields()[“User Text”]’
You can also use predicate searching within iOS UI Automation in Appium, to control element finding even further. See here for details.
Using these strategies, it is possible to have control over finding elements, whether single or multiple, in a mobile application. This control forms the foundation of any automation, as once elements are found they can be queried for information as well as interacted with. By giving tests access to all elements in a variety of ways, Appium allows tests and testers to choose their preferred methodologies.
- Accessibility Testing
- Appium Resources
- Best Practices
- Continuous Delivery
- Continuous Integration
- Continuous Testing
- Cross Browser Testing
- Guest Blog Posts
- Load Testing
- Machine Learning
- Mobile Development & Testing
- News & Product Updates
- Open Sauce
- Open Source
- Performance Testing
- Product Updates
- Quality Assurance
- Quality Engineering
- Sauce Product Info
- Security Testing
- Selenium Resources
- Software Development & Testing
- The Story of Sauce