Problem: Many testers do not know how to identify UI elements in mobile apps.

In this post, I explain about different ways to find UI elements in Android’s calculator app. I also show you examples of using Appium’s API with the different locator strategies.

Why this post?

Identifying UI elements is a key component of writing UI automation. Most testers know how to inspect a web application and uniquely identify the UI elements that they want. However, inspecting and identifying UI elements in a mobile application is still somewhat of a dark art for most testers. Case in point – when I was learning Appium, it was hard to find a single article that showed me the various options I have to identify UI elements. This post aims to rectify this problem. In this post you will learn different ways to identify and interact with UI elements in a mobile application.

uiautomatorviewer: Mobile’s Firebug

If you automate web application testing, you are sure to have used either developer tools or Firebug. uiautomatorviewer is a GUI tool to scan and analyze the UI components of an Android app. uiautomatorviewer comes with the Android SDK. Please make sure to install Android SDK to follow along.

To launch uiautomatorviewer, pull up your command prompt, navigate to your $android-sdk/tools directory and issue the command uiautomatorviewer. Launch uiautomatorviewer and connect your Android device to your machine. Launch the calculator app. In uiautomatorviewer, click on the Device Screenshot button (top left) to take the snapshot of the calculator app. You can now inspect the layout hierarchy and view the properties of the individual UI components that are displayed on the device.

NOTE 1: For more details on connecting your Android device to your computer, please refer to the Step 2 of our previous blog.

Different ways of finding UI elements

Figure 1 is your path to happiness. You can use the properties of the elements found in the Node Detail table like text, class, resource-id, content-desc etc to find the UI components of the app. Here are a few locator strategies that map well with Appium’s API:

1. Finding element by xpath in mobile application

2. Finding mobile element by Android UIAutomator

1. Finding element by xpath in mobile applications

To write your xpath:

a. start with the class //class

b. [OPTIONAL] add attributes @attribute="blah"

E.g.: XPath to identify the number 7 in the calculator app is

//android. widget . Button [ @ text = '7' ] //android.widget.Button[@text='7']

With Python and Appium, your corresponding call would be:

driver. find_element_by_xpath ( "//android.widget.Button[@text='7']" ) driver.find_element_by_xpath("//android.widget.Button[@text='7']")

2. Finding mobile elements by Android UIAutomator

Another way to identify UI elements in a mobile application is via UiSelector. To do so,

a. start with a new UiSelector object new UiSelector()

b. dot-add any of the other properties of the element .text()

E.g.: To identify the number 7 in the calculator app:

new UiSelector ( ) . text ( "7" ) new UiSelector().text("7")

With Python and Appium, your corresponding call would be:

driver. find_element_by_android_uiautomator ( 'new UiSelector().text("7")' ) driver.find_element_by_android_uiautomator('new UiSelector().text("7")')

NOTE: This method uses properties such as text, content-desc, resorurce id etc to find elements. Please refer to this link for more details.

Here are a few shortcuts to identifying elements that are specific to Appium:

1. Via accessibility ID: On Android, you can use the string in content-desc.

E.g.: To identify the delete button in the calculator,

driver. find_element_by_accessibility_id ( 'delete' ) driver.find_element_by_accessibility_id('delete')

2. Finding element by Id: Corresponds to the resource-id field

E.g.: To identify the number 7 in the calculator app

driver. find_element_by_id ( "com.android.calculator2:id/digit7" ) driver.find_element_by_id("com.android.calculator2:id/digit7")

3. Finding element by class name

E.g.: To find the text/results box in the calculator app

driver. find_element_by_class_name ( "android.widget.EditText" ) driver.find_element_by_class_name("android.widget.EditText")

An example Appium + Python script with different locator strategies

I am providing a fully functioning Python script for you to practice your locator strategies. Challenge yourself to identify different elements in the calculator app and make corresponding changes to this script.

""" Qxf2: Example script to run one test against calculator app The test will show you how you can find UI elements by various methods like xpath, id, accessibility_id and android UIautomator on a android calculator app """ import os import unittest , time from appium import webdriver from time import sleep class Android_Calculator ( unittest . TestCase ) : "Class to run tests for android calculator" def setUp ( self ) : "Setup for the test" desired_caps = { } desired_caps [ 'platformName' ] = 'Android' desired_caps [ 'platformVersion' ] = '4.4' desired_caps [ 'deviceName' ] = 'Moto E' # Get the package and activity name of the calculator to launch the calculator app desired_caps [ 'appPackage' ] = 'com.android.calculator2' desired_caps [ 'appActivity' ] = 'com.android.calculator2.Calculator' self . driver = webdriver. Remote ( 'http://localhost:4723/wd/hub' , desired_caps ) def tearDown ( self ) : "Tear down the test" self . driver . quit ( ) def test_calculator ( self ) : "Testing android calculator" self . driver . implicitly_wait ( 10 ) # Find the UI element using xpath self . driver . find_element_by_xpath ( "//android.widget.Button[@text='7']" ) . click ( ) self . driver . find_element_by_xpath ( "//android.widget.Button[@resource-id='com.android.calculator2:id/mul']" ) . click ( ) # Find UI element using id self . driver . find_element_by_id ( "com.android.calculator2:id/digit7" ) . click ( ) # Find UI element using accessibility_id self . driver . find_element_by_accessibility_id ( 'delete' ) . click ( ) # Find UI element using android UIautomator self . driver . find_element_by_android_uiautomator ( 'new UiSelector().text("3")' ) . click ( ) self . driver . find_element_by_android_uiautomator ( 'new UiSelector().resourceId("com.android.calculator2:id/equal")' ) . click ( ) # Find UI element using class name result = self . driver . find_element_by_class_name ( "android.widget.EditText" ) print result. get_attribute ( 'text' ) #---START OF SCRIPT if __name__ == '__main__' : suite = unittest . TestLoader ( ) . loadTestsFromTestCase ( Android_Calculator ) unittest . TextTestRunner ( verbosity = 2 ) . run ( suite ) """ Qxf2: Example script to run one test against calculator app The test will show you how you can find UI elements by various methods like xpath, id, accessibility_id and android UIautomator on a android calculator app """ import os import unittest, time from appium import webdriver from time import sleep class Android_Calculator(unittest.TestCase): "Class to run tests for android calculator" def setUp(self): "Setup for the test" desired_caps = {} desired_caps['platformName'] = 'Android' desired_caps['platformVersion'] = '4.4' desired_caps['deviceName'] = 'Moto E' # Get the package and activity name of the calculator to launch the calculator app desired_caps['appPackage'] = 'com.android.calculator2' desired_caps['appActivity'] = 'com.android.calculator2.Calculator' self.driver = webdriver.Remote('http://localhost:4723/wd/hub', desired_caps) def tearDown(self): "Tear down the test" self.driver.quit() def test_calculator(self): "Testing android calculator" self.driver.implicitly_wait(10) # Find the UI element using xpath self.driver.find_element_by_xpath("//android.widget.Button[@text='7']").click() self.driver.find_element_by_xpath("//android.widget.Button[@resource-id='com.android.calculator2:id/mul']").click() # Find UI element using id self.driver.find_element_by_id("com.android.calculator2:id/digit7").click() # Find UI element using accessibility_id self.driver.find_element_by_accessibility_id('delete').click() # Find UI element using android UIautomator self.driver.find_element_by_android_uiautomator('new UiSelector().text("3")').click() self.driver.find_element_by_android_uiautomator('new UiSelector().resourceId("com.android.calculator2:id/equal")').click() # Find UI element using class name result=self.driver.find_element_by_class_name("android.widget.EditText") print result.get_attribute('text') #---START OF SCRIPT if __name__ == '__main__': suite = unittest.TestLoader().loadTestsFromTestCase(Android_Calculator) unittest.TextTestRunner(verbosity=2).run(suite)

Subscribe to our weekly Newsletter

View a sample

Leave this field empty if you're human: