Locators in Appium
Locating specific elements is the most important step prior to automating test scenarios for both web and mobile apps. Therefore it’s imperative for developers or QAs to know how to locate the exact elements using particular locator strategies in Appium. 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.
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.
This article aims to point a few locator strategies in Appium that are highly used.
Locator strategies supported by Appium:
- ID
- Class Name
- Xpath
- Accessibility ID
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 iOSdr.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 the 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
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.
Code
MobileElement computeSumButton = driver.findElementByXPath("(//XCUIElementTypeButton)[1]");
While performing automated app testing, test scripts need to interact with extensive elements. Thus, locating the right elements for successful testing is mandatory.