How to locate android app elements using UI Automator?

By Ranjith VG on May 25, 2018

What is UI Automator?

We know it is easy to locate web elements using locators in selenium. Likewise,  it is easy to locate android app’s elements too. UI automator is the GUI tool used to scan and analyze UI components of an android application. In this article i will describe the step by step process to locate each android app element.

Prerequisite for Running UI Automator.

  1. Install JDK
  2. Configure environment variable for JAVA_HOME
  3. Go to http://developer.android.com/sdk/index.html.
  4. Scroll down to “SDK tools only” under other download options section.
  5. Download the “android-sdk_r(Version number)-windows.zip”.
  6. Unzip the file and install ‘SDK Manager.exe’ by selecting the required packages.
  7. Set ANDROID_HOME and Path Environment Variables For SDK In Windows(Explained below).

How to set android home variable?

  • Right click on  My Computer >> Select properties >> Advanced system settings >> Environment Variables.
  • Under user variable table, select new button and set Variable Name = ANDROID_HOME and Variable value =  (Path of SDK folder).
  • Copy path of ‘tools’ and ‘platform-tools’ under SDK folder.
  • Edit path variable under environment variable and append the copied paths of ‘tools’ and ‘platform-tools’.
  • Click the ‘Ok’ button and close the window.
  • Verify whether android is installed properly by executing the command ‘android’ in command prompt(This will open the android SDK Manager).

 

How to use android SDK with Eclipse?

  • Open Eclipse IDE and go to Help >> Install New Software.
  • Click ‘Add’ Button in the Install window.
  • Set Location as https://dl-ssl.google.com/android/eclipse/ and click ‘ok’ button. This will open up developer tools with checkbox.

        

         

  • Select all and go to next step.
  • Complete the ADT plugin installation and restart your computer.

How to set SDK location?

  • Open Eclipse >> Windows >> Preferences.
  • Select Android and set the SDK folder location.

         

How can we verify android SDK is configured properly with eclipse?

  • Open Eclipse and go to Windows >> Android SDK Manager and open Android SDK Manager.
  • IF Android SDK Manager dialog is shown as below, then we can confirm that android SDK is integrated properly with eclipse IDE using ADT plugin.

 

 

we should make sure that Microsoft .Net framework and Node JS is installed in our system before proceeding further.

 

How to inspect using UI Automator?

Enable the debugging mode in our computer and connect to the android device. Then, Open the UI automator tool by running the ‘uiautomatorviewer.bat’ from the SDK folder location.

Open the Calculator app in the Phone and take the screenshot of the app using UI automator.

After the screenshot is captured, the UI automator will show the calculator app’s UI on the left side. Right side of the tool have two parts, top parts displays the node structure of the app’s UI elements. Bottom Part displays property detail of the selected elements.

       

Now Let me brief some ways to locate android app elements. Consider button ‘5’ in app. See the different ways to write Xpath.

  1. Xpath using class and text attribute.

        

X path can be created using text attribute value with class name.

XPath = xpath(“//android.widget.Button[@text=’5′]”)

  1. XPath using Class and Resource ID.

       

Here we use contains function to get the Xpath like shown below.

Xpath = xpath(“//android.widget.Button[contains(@resource-id,’digit5′)]”)

  1. Xpath using Class, Text attribute and Resource ID.

XPath = xpath(“//android.widget.Button[contains(@resource-id,’digit5′) and @text=’5′]”)

  1. Xpath using class, text attribute and index.

Xpath  = xpath(“//android.widget.Button[@text=’5′ and @index=’1′]”)

  1. XPath using parent and Child hierarchy.

Here, Parent class android.widget.LinearLayout class with index = 1 has buttons 4, 5, 6 and X. So we can locate that specific row by it’s class index. Child element is member of class android.widget.Button with index = 1. So we can format XPath using parent and child class hierarchy as bellow.

Xpath = xpath(“//android.widget.LinearLayout[@index=’1′]/android.widget.Button[@index=’1′]”)

  1. Xpath using content-desc.

        

Content description is unique for a android app element. So this can also be used to create xpath.

Here, delete button have unique content-desc = delete.

Xpath = xpath(“//android.widget.Button[@content-desc=’delete’]”)

  1. Xpath using class name.

         

If class name is unique, we can use this method to find the xpath.

Xpath = xpath(“//android.widget.ImageButton”)

  1. Locating element by ID

Resource id is used for this type of locator.

        

Here, resource id of  button ‘5’ is “com.android.calculator2:id/digit5 “.

Xpath = id(“com.android.calculator2:id/digit5”)

  1. Locating by class name.

If class name is unique, we can use it to locate elements.

       

Here element can located by “By.className(“android.widget.ImageButton”)”.

  1. Locating android elements by Name.

If the element contains unique text, it is possible to locate by its name.

      

Here ‘=’ can be located by “By.name(“=”)”.

  1. Locating elements by find elements.

For calculator app, we can see that all buttons have the same class “android.widget.Button”. Using ‘find elements’ we can get list of all buttons and store the list using java List interface. Then, we can get any element using get() method of list interface.

      

List<WebElement> calcButtons = driver.findElements(By.xpath(“//android.widget.Button”));

The above syntax will store the button list in the list ‘calcButtons’.

Button

Array index ID

Delete

0

7

1

8

2

9

3

÷

4

4

5

5

6

6

7

X

8

1

9

2

10

3

11

12

.

13

0

14

=

15

+

16

Now, you can access the button u need to click using the index ID. Ie if you want to click on button ‘7’,  use syntax “calcButtons.get(6).click();”

That’s it. Find our more regarding UI automator in the following links.

  1.  https://www.tutorialspoint.com/android/android_ui_testing.htm
  2.  https://www.guru99.com/uiautomatorviewer-tutorial.html

 

Leave a Reply

SCROLL TO TOP
This site is registered on wpml.org as a development site.