Home Guide Locators in Appium

Locators in Appium

By Jash Unadkat, Technical Content Writer at BrowserStack -

Locating specific elements is the most important step prior to automating test scenarios for both web and mobile apps. Knowing how to use different locators correctly is key to building efficient automation scripts. After all, if the test script is not able to 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 of Selenium testing relate better with the Appium framework.

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 this article on Appium Desktop.

1. ID

Finding elements using the ID is, by far the simplest technique. Each element has a unique ID assigned to it that helps in identifying and interacting 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

It is a highly preferred locator strategy, especially in the case of 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 and this creates a problem in finding one particular element. Thus, one needs to use a combination of multiple 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


Read More: Getting Started with XCUITest Framework


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 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 needs to send the Java code as a string to the server that is executed in the application’s environment which in turn returns the particular elements.

Code

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

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

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

Run Appium Tests on Real Devices for Free

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 right elements for successful testing is mandatory. Consequently, it is imperative for testing teams to be well-versed with all possible locator strategies in Appium that help identify web elements accurately.

BrowserStack Logo Run Appium tests on real Android & iOS devices Get Started Free