App & Browser Testing Made Easy

Give your users a seamless experience by testing on 3000+ real devices and browsers. Don't compromise with emulators and simulators

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.

7 Locator Strategies Supported by Appium:

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

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.

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

Tags
Appium Mobile App Testing

Featured Articles

Appium Best Practices Every Developer Must Know

Desired Capabilities in Appium

Curated for all your Testing Needs

Actionable Insights, Tips, & Tutorials delivered in your Inbox
By subscribing , you agree to our Privacy Policy.
thank you illustration

Thank you for Subscribing!

Expect a curated list of guides shortly.