Ways to identify UI elements in mobile apps

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.

Screenshot of calculator app using uiautomatorviewer
Figure 1. Inspecting a mobile application

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']

With Python and Appium, your corresponding call would be:

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")

With Python and Appium, your corresponding call would be:

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')

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")

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")

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)

If you have read so far, you may be interested in our open-sourced Python-based mobile automation framework.Happy inspecting! Happy locating!


Subscribe to our weekly Newsletter


View a sample



44 thoughts on “Ways to identify UI elements in mobile apps

  1. Hello,
    Thank you for helpful post. But I am having a trouble with the question below:
    How can I press on a text field?
    I hope I receive your help.
    Thanks

    1. Hi Thanh,
      Once you find the text element you can use send_keys() to send any keys to the text field.
      In case you want to click on the element use click() command.

  2. Hello,

    Your explanation is quite good. Could you please help to find elements apart from UI Automator. I have the scenario that, if I open page in android device, in UI automator there is no Webelements found. I am not sure do we have webelements for that page or not. But in that page we have so many things are there need to operate. So i am searching for alternative for UI Automator can u please guide me ?

  3. Nice article, Could you explain the difference between clickable and enabled in the node details? It’s a bit confusing when clickable is false and enabled is tue.

    1. Hi Arya,
      I got some information from this StackOverflow answer.
      In Android, a widget that is not clickable will not respond to click events. A disabled widget not only is not clickable but also visually indicates that it is disabled. Hope this clarifies your question.

  4. Hey hi,
    Nice work, i have started working on mobile app automation testing using java but i am facing some issue, like at one page of mobile application i can able to locate elements and able to perform my action but when i go to next page i was not able to locate any element using xpath i tried all the possible ways.
    Can you please guide me what actually the problem is?i am clueless

    1. Hi Abhinav,
      Can you please provide more information about the issue, so I can help. Do you get any error messages? Are you sure your automation script taking you successfully to next expected page? Check your script pointing to correct page object or not if you were using POM framework. Sometimes, adding wait time in the script (after doing the action which takes you to next page) will help to solve this kind of issues.

Leave a Reply

Your email address will not be published.