Understanding Locators in Appium

Use Locators in Appium to locate elements on Real Android & iOS Devices

Get Started free
Home Guide Effective Locator Strategies in Appium

Effective Locator Strategies in Appium

By Jash Unadkat, Community Contributor -

Locating specific elements is essential before automating test scenarios for web and mobile apps. Knowing how to use different locators correctly is key to building efficient automation scripts. After all, if the test script cannot identify which element it needs to interact with, the test will fail before it can begin.

A previous article on locators in Selenium explores the different locators that help uniquely identify elements on an HTML page. On similar lines, this article will point out different locator strategies used in Appium for automated app testing.

The official documentation on Appium.io states that Appium provides compatibility with a subset of the WebDriver locator strategies. For example: find by Xpath, find by class. This will help QAs with prior experience in Selenium testing relate better to the Appium framework.

What are Appium Locators?

Locators are references or identifiers used in automation testing to interact with elements in a user interface (UI). They act as the bridge between the automation script and the application’s visual elements, enabling testers to find and manipulate specific UI components like buttons, text fields, or checkboxes.

In Appium, which is a popular open-source tool for automating mobile apps (both Android and iOS), locators are used to identify elements within the mobile application’s UI so that automation scripts can interact with them. Appium uses various locator strategies to find elements across different views and platforms.

Appium supports different locator strategies like ID, Accessibility ID, Class Name, XPath, Android UI Automator, Android View Tag, and iOS UI automation.

7 Locator Strategies Supported by Appium

Below are the 7 Appium Locator Strategies:

  1. ID
  2. Class Name
  3. Xpath
  4. Accessibility ID
  5. Android UI Automator
  6. Android View Tag (Espresso Only)
  7. iOS UI Automation

1. ID

By far, finding elements using the ID is the most straightforward technique. Each element has a unique ID assigned to it that helps identify and interact with it. Appium has a Native element identifier for Android and iOS.

resource-id is used as an element identifier for Android and name is used for iOS.

Code

driver.findElementById("IntegerA"); // for iOS

dr.findElement(By.id("android:id/text1")).click(); //for Android

2. Accessibility ID in Appium

Accessibility ID in Appium is a highly preferred locator strategy, especially when automating Android and iOS test cases. Developers can explicitly set the Accessibility ID during development.

As Accessibility ID can be used for cross-platform automation, the code becomes reusable.

For iOS, the default Accessibility ID is set to the name of the UI element. For Android, the value of Accessibility is same as the value of the attribute “content-desc”.

Code

dr.findElementByAccessibilityId("Accessibility").click();

3. Class Name

Using Class Name for searching an element is a very generic method. This is because multiple elements may have the same class name, creating a problem finding a particular component. Thus, one needs to use a combination of various attributes, for example, combining text with the class name to identify the element.

For iOS, Class Name is represented as the full name of the XCUI element and begins with XCUIElementType. For example – UIAButton, UIARadioButton

In the case of Android, the Class Name is called out as the full name of the UIAutomator2 class. For example – android.widget.TextView

Code

List<WebElement> buttons = driver.findElementsByClassName("android.widget.TextView");

for(WebElement button : buttons){
System.out.println(button.getText());

if(button.getText().equals("Animation")){
button.click();
}
}

4. XPath

Xpath in Appium analyzes the XML structure of the app and then locates the element. Xpath should only be used when there is no ID, Name, or accessibility ID assigned to a specific UI element. Although XPath allows for the formulation of complex queries, using XPath is not recommended because it has stability and performance issues (as mentioned in the official documentation).

One can easily find the Xpath using the Appium Desktop Inspector while inspecting the XML structure of the application.

Appium locator
Image source

Code

MobileElement computeSumButton = driver.findElementByXPath("(//XCUIElementTypeButton)[1]");

5. Android UI Automator (UI Automator 2)

Naturally, as the name suggests, this locator is Android-specific. One needs to use the UI Automator API, in particular, the UISelector Class to search for specific elements. Naturally, this makes it a pre-requisite for QAs to have prior knowledge of UISelector API. In Appium, one must send the Java code as a string to the server executed in the application’s environment, which returns the particular elements.

Code

String selector = "new UiSelector().text(“Cancel”))

.className(“android.widget.Button”))";

MobileElement element = (MobileElement) driver.findElement(MobileBy.AndroidUIAutomator(selector));

6. Android View Tag (Espresso Only)

Similar to Android UI Automator, this is also an Android platform-specific locator. It allows QAs to locate elements using its view tag.

7. iOS UIAutomation

This is an iOS platform-specific locator. It enables QAs or developers to use Apple’s Instruments framework to locate elements while automating tests for iOS apps.

Code

String selector = "**/XCUIElementTypeCell[`name BEGINSWITH "P"`]/XCUIElementTypeButton[4]";

MobileElement element = (MobileElement)

Note: The iOS UIAutomation is deprecated, and now the primary support for automating iOS apps is using the XCUITest Driver.

Use this guide to migrate iOS tests from UIAutomation (iOS 9.3 and below) to XCUITest (iOS 9.3 and up)

While performing automated app testing, test scripts need to interact with extensive elements. Thus, locating the correct elements for successful testing is mandatory. Consequently, testing teams must be well-versed with all possible locator strategies in Appium that help identify web elements accurately.

Run Appium Tests on Real Devices

Which Locator is fastest in Appium?

In Appium, the fastest locator strategy is typically the ID locator (referred to as resource ID on Android and accessibility ID on iOS). This is because IDs are unique and directly reference an element, allowing Appium to locate it quickly without traversing the entire UI hierarchy.

Unlike XPath, which can be slow as it requires navigating through the document tree to find an element, ID locators offer a direct and efficient path. Using IDs is particularly beneficial for improving test performance, as they minimize the processing time required to find elements within complex mobile interfaces.

Additionally, they tend to be more stable, as IDs are often less likely to change during app updates compared to attributes like class names or text values.

When designing your Appium tests, prioritizing ID-based locators can lead to faster execution times and more reliable automation.

How do you Inspect Locators in Appium?

Inspecting locators in Appium involves using various tools and techniques to identify and validate the attributes of UI elements in a mobile app.

1. Appium Inspector: Appium includes a built-in tool known as the “Appium Inspector” that enables testers to visually inspect elements within the app. It displays the app’s interface and provides details like IDs, Accessibility IDs, XPath, and other relevant attributes for identifying elements.

2. UI Automator Viewer (for Android) / Accessibility Inspector (for iOS): Android and iOS have their own official tools for element inspection. For Android, the UI Automator Viewer provides information such as resource IDs and other attributes, while iOS users rely on Accessibility Inspector (available in Xcode) to access details like Accessibility IDs and element properties.

3. Element Inspection Libraries: Tools such as Appium Desktop, UIAutomatorViewer, and Appium Desktop Inspector offer enhanced element inspection features. These libraries provide information about the element attributes and hierarchy within the app’s UI.

4. Mobile Emulators/Simulators: Emulators and simulators allow testers to interact with the app just like on a real device. Paired with Appium, testers can observe and inspect element properties while using the app, making it easier to identify and work with UI elements.

Note: An Appium Inspector Tool allows users to locate elements using all the above locator strategies. To learn more about Appium Inspector, refer to the Tutorial on Understanding Appium Desktop.

Best Practices when using Locators in Appium

By following these best practices, you can improve the efficiency, stability, and maintainability of your Appium test scripts.

  1. Use Unique Locators: Always prefer unique locators like ID or Accessibility ID over less reliable ones like XPath. This ensures that tests are stable and less prone to breakage as the app evolves.
  2. Avoid Absolute XPath: Avoid using absolute XPath, which is based on the full path of the element in the UI hierarchy. Instead, use relative XPath to make your locators more robust and less dependent on changes in UI structure.
  3. Leverage Accessibility IDs: When possible, use Accessibility IDs for locating elements. They are consistent across platforms, easy to maintain, and improve accessibility for users with disabilities.
  4. Minimize Use of XPath: XPath can be slow and unreliable, especially on mobile platforms. Use it only when no other locator strategy works effectively. Prioritize faster strategies like ID or Class Name.
  5. Use Descriptive Locators: Choose locators that accurately describe the function of the element. This improves code readability and makes it easier for other testers to understand and maintain the test scripts.
  6. Ensure Locator Stability: Locators should remain consistent across different versions of the app. Avoid dynamic attributes that are likely to change, such as text or index-based locators.
  7. Optimize Locator Strategy for Performance: Locating elements using ID or Class Name is generally faster than XPath. Optimize for performance by choosing the most efficient locator for each element.
  8. Use Multiple Locator Strategies for Complex UIs: For complex and dynamic UIs, use a combination of locators (like XPath with conditions) to handle scenarios where elements might change based on context.
  9. Test Locators in Different Environments: Ensure that locators work across various devices, screen sizes, and platforms (iOS/Android) to avoid platform-specific failures.
  10. Maintain Locator Reusability: Use consistent naming conventions and store commonly used locators in a centralized location (like a separate class or object repository) to enhance reusability and maintainability.

Talk to an Expert

Advantages of Using Locators in Appium

Here are the core advantages of using Locators in Appium:

  1. Cross-Platform Support: Appium allows for the same locator strategies to be used across Android and iOS platforms, which means the same test scripts can be applied to different mobile OSs with minimal modification.
  2. Flexibility: Appium supports a wide variety of locator strategies (ID, XPath, Class Name, etc.), giving testers flexibility to choose the most suitable one based on the app’s structure and requirements.
  3. Improved Test Coverage: Locator strategies like XPath and UIAutomator allow testers to handle complex and dynamic UI elements, enhancing the robustness and coverage of test cases.
  4. Test Maintainability: Using strategies like Accessibility ID not only improves the app’s accessibility for users but also creates stable locators that are less likely to break during app updates, reducing maintenance effort.
  5. Support for Real Devices and Emulators: Locators in Appium work seamlessly on both physical devices and emulators, allowing testers to validate UI components in real-world scenarios as well as simulated environments.

BrowserStack App Automate Banner

Conclusion

Locators in Appium play a crucial role in identifying and interacting with UI elements during mobile app automation. Popular locators such as ID, XPath, Class Name, and Accessibility ID offer flexibility in element selection, with ID and Accessibility ID being the fastest and most stable options for cross-platform testing. XPath, while powerful, should be used sparingly due to its slower performance, especially in complex UIs.

Appium’s locator strategies bring significant advantages, such as cross-platform compatibility, better test coverage, and maintainability. Following best practices, like prioritizing unique locators, avoiding absolute XPath, and ensuring locator stability, can significantly enhance test performance and reliability. By choosing the right locator strategy and adhering to these practices, testers can create robust, efficient, and scalable automation scripts for mobile apps.

Tags
Appium Mobile App Testing

Featured Articles

Appium Best Practices Every Developer Must Know

Desired Capabilities in Appium

Automation Tests on Real Android & iOS Devices

Seamlessly Run Mobile App Automation Tests on real Android & iOS Devices