Mastering Locator Strategies in Appium: Effective Techniques for Identifying and Testing Elements

Morris MMorris M
8 min read

When it comes to testing mobile apps, it’s important to know exactly how to search and test different features in those apps. Understanding these techniques in the world of mobile app testing with Appium sounds very powerful. This guide is here to help you successfully browse and test features in Appium.

We will go in-depth on the various ways you can discover and interact with objects in mobile apps using Appium From learning effective ways to discover buttons, text fields, and other parts of an app to you’ll make sure your testing is robust and reliable, we’ll cover it all. This guide provides you with the skills and knowledge needed to tackle the challenges of testing mobile apps with Appium. So, explore these techniques and best practices that will improve your ability in mobile app testing!

Common Locator Strategies in Appium

In Appium, there are different methods used to find and work with different elements within a mobile app’s interface during automated testing. These methods include looking for specific IDs given to elements, using XPath expressions to move around the app’s structure, finding elements based on accessibility IDs which make things easier to use, searching for elements with similar attributes, finding elements by their names, and using techniques specific to each platform like Android UI Automator and iOS UI Automation. Each of these methods offers a unique way to accurately locate elements, ensuring that the app is tested thoroughly across various mobile platforms. Deciding which method to use depends on factors like how unique the elements are, how reliable the method is, and how efficient it is within the app’s structure.

By using ID

The “ID” locator strategy in Appium is one of the primary and most efficient methods for finding elements within mobile applications. An “ID” is a unique identifier assigned to an element in the app’s source code. This ID serves as a distinctive label, making it relatively quick for Appium to locate and interact with the specific element during automation.

When using the “ID” locator strategy, Appium searches for elements by their unique identification tag. This strategy is advantageous because IDs are typically unique for each element on a page, reducing the chances of ambiguity or mistakenly identifying the wrong element. As a result, finding elements by their IDs often leads to faster and more stable automation scripts. Cross-platform automation with Appium is facilitated by dedicated Native element identifiers for Android and iOS.

  • Android: resource-id serves as a unique identifier for elements.

  • iOS: name is commonly used to identify elements uniquely.

from appium import webdriver

from selenium.webdriver.common.by import By

import time

# Desired Capabilities for your specific mobile app

desired_capabilities = {

'platformName': 'Android', # Replace with your platform

'deviceName': 'Your_Device_Name',

'appPackage': 'com.example.app', # Replace with your app's package name

'appActivity': 'com.example.app.MainActivity' # Replace with your app's main activity

Add other desired capabilities as needed

}

# Appium server URL

appium_server = 'http://localhost:4723/wd/hub' # Replace with your Appium server URL

# Initialize the driver

driver = webdriver.Remote(appium_server, desired_capabilities)

# Wait for the elements to load (replace 'username_field_id' with your actual ID)

username_field = driver.find_element(By.ID, 'username_field_id')

# Enter username into the field

username_field.send_keys('YourUsername')

# Wait for a few seconds (optional)

time.sleep(2)

# Close the app session

driver.quit()

Using Accessibility ID

The Accessibility ID locator option in Appium is particularly useful when testing mobile applications, especially for iOS, because it helps identify objects with their accessibility identifiers or labels explicitly configured for accessibility purposes. Appium inspector has an internal option for Accessibility ID, which helps build more flexible mobile application locators and makes it easier to test less efficiently.

Note: iOS uses the element’s name as its default Accessibility ID, while Android uses the “content-desc” value.

from appium import webdriver

from selenium.webdriver.common.by import By

import time

# Desired Capabilities for your specific mobile app

desired_capabilities = {

'platformName': 'iOS', # Replace with your platform

'platformVersion': '14.5', # Replace with your iOS version

'deviceName': 'Your_iOS_Device_Name',

'app': '/path/to/your/app.ipa', # Replace with the path to your app

Add other desired capabilities as needed

}

# Appium server URL

appium_server = 'http://localhost:4723/wd/hub' # Replace with your Appium server URL

# Initialize the driver

driver = webdriver.Remote(appium_server, desired_capabilities)

# Wait for the elements to load (replace 'login_button_accessibility_id' with your actual ID)

login_button = driver.find_element(By.ACCESSIBILITY_ID, 'login_button_accessibility_id')

# Perform an action, e.g., click on the login button

login_button.click()

# Wait for a few seconds (optional)

time.sleep(2)

# Close the app session

driver.quit()

Using Class Name

Using the Class Name locator strategy in Appium involves locating elements based on their class names, which are part of the HTML structure in web applications or UI components in mobile apps. While it’s a useful method, it’s important to note that not all elements have unique class names, and some elements might share the same class name, making this strategy less precise than others like ID or Accessibility ID. Using the Class Name strategy in Appium can help locate groups of similar elements, but it’s essential to handle cases where elements share the same class name or where class names might change dynamically.

  • For iOS: The Class Name represents the full name of the XCUI element and typically starts with “XCUIElementType,” such as XCUIElementTypeButton, XCUIElementTypeRadioButton, etc.
  • For Android: The Class Name corresponds to the full name of the UIAutomator2 class, like android.widget.TextView. It’s used to identify elements uniquely in Android apps when using Appium for test automation.

from appium import webdriver

from selenium.webdriver.common.by import By

import time

# Desired Capabilities for your specific mobile app

desired_capabilities = {

'platformName': 'Android', # Replace with your platform

'deviceName': 'Your_Device_Name',

'appPackage': 'com.example.app', # Replace with your app's package name

'appActivity': 'com.example.app.MainActivity', # Replace with your app's main activity

Add other desired capabilities as needed

}

# Appium server URL

appium_server = 'http://localhost:4723/wd/hub' # Replace with your Appium server URL

# Initialize the driver

driver = webdriver.Remote(appium_server, desired_capabilities)

# Wait for the elements to load (replace 'button_class_name' with your actual class name)

button_elements = driver.find_elements(By.CLASS_NAME, 'button_class_name')

# Perform actions on the found elements (for demonstration, let's print the text of each button)

for button in button_elements:

print(button.text)

# Wait for a few seconds (optional)

time.sleep(2)

# Close the app session

driver.quit()

Using XPath

XPath is a powerful locator strategy used in Appium to precisely identify elements in mobile apps based on their XML path. It provides a way to navigate through the XML structure of the app’s UI elements, allowing for specific and flexible element identification. XPath is powerful as it allows for precise element targeting, but it’s crucial to create XPath expressions that are stable and reliable, considering changes in app structure or dynamic content. XPath expressions can be used to access elements based on various attributes, positions, or relationships within the XML structure of the app.

from appium import webdriver

from selenium.webdriver.common.by import By

import time

# Desired Capabilities for your specific mobile app

desired_capabilities = {

'platformName': 'Android', # Replace with your platform

'deviceName': 'Your_Device_Name',

'appPackage': 'com.example.app', # Replace with your app's package name

'appActivity': 'com.example.app.MainActivity', # Replace with your app's main activity

Add other desired capabilities as needed

}

# Appium server URL

appium_server = 'http://localhost:4723/wd/hub' # Replace with your Appium server URL

# Initialize the driver

driver = webdriver.Remote(appium_server, desired_capabilities)

# Wait for the elements to load (replace 'xpath_expression' with your actual XPath)

element = driver.find_element(By.XPATH, 'xpath_expression')

# Perform actions on the found element (for demonstration, let's click the element)

element.click()

# Wait for a few seconds (optional)

time.sleep(2)

# Close the app session

driver.quit()

Using Android UI Automator

Android UI Automator is an inspection and testing framework provided by Android that Appium leverages to locate and interact with elements in Android apps. It allows for the identification of UI elements by their properties, texts, and other attributes visible on the Android user interface. Android UI Automator allows testers to create complex and flexible selectors using various attributes, texts, and other properties of UI elements to precisely locate them within the app’s interface. This strategy is powerful for handling dynamic elements or finding elements based on specific criteria visible to the user on the Android UI.

from ui automator import device as d

# Press the home button to ensure we're at the home screen

d.press.home()

# Open the Settings app

d.app_start("com.android.settings")

# Click on the 'Display' option

d(text="Display").click()

# Scroll down in the Display settings

d(scrollable=True).scroll.toEnd()

# Click on the 'Sleep' option under Display settings

d(text="Sleep").click()

# Change the sleep time to 30 seconds

d(text="30 seconds").click(

# Go back to the home screen

d.press.home()

Android View Tag (Espresso Only)

The Android View Tag is a concept specifically used with Espresso, a testing framework for Android, and not directly accessible in Appium, which is primarily used for cross-platform mobile app testing. Espresso interacts with Android View Tags to access and manipulate UI components within Android applications.

Espresso’s usage of the Android View Tag involves accessing elements based on the tag assigned to views in the Android UI hierarchy. The View Tag acts as a marker or identifier attached to a particular UI component that developers can set and testers can use for test automation.

// Example Espresso Test in Java using Android View Tags

onView(withTagValue(Matchers.equalTo("tag_value"))).perform(click());

iOS UI Automation

iOS UI Automation has been deprecated by Apple and replaced by XCTest, which is the current and recommended framework for UI testing in iOS applications. XCTest offers more modern and robust features for UI testing, providing native support within Xcode. In the context of Appium, which is widely used for cross-platform mobile app testing (including iOS), it typically doesn’t directly employ the deprecated iOS UI Automation framework. Instead, Appium leverages various other locator strategies like XPath, ID, Accessibility ID, Class Name, and others to identify and interact with elements within iOS apps.

For iOS app testing using Appium, testers commonly use these locator strategies that are compatible with both Android and iOS platforms. This allows them to create unified test scripts that work across different platforms without specifically relying on the deprecated iOS UI Automation framework.

Conclusion

In this article, you will get an overview of the locator strategy for Appium which you can manage or identify by using Appium Inspector and, you can also use the TestGrid.IO tool for identifying locator as well as for automating the test cases in Appium. TestGrid.io also provides different real device access where you can run our test cases or directly automate using it.

This blog is originally published at TestGrid

0
Subscribe to my newsletter

Read articles from Morris M directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Morris M
Morris M

QA Leader with 7+ yrs experience. Expert in team empowerment, collaboration, & automation. Boosted testing efficiency & defect detection. Active in QA community.