Appium : Multiple ways to identify the UI elements of Android app

In this post, We will discuss about UIAutomator tool. It is provided by Android SDK to scan and analyze the UI elements of android native apps. Using this tool, we can easily get the element properties and we can also create custom XPATH or use other element properties like class name, id etc to identify the UI element of android mobile app.

Pre-Requisite

  • JDK must be installed
  • Android SDK must be installed
  • Android device must be connected to computer
  • Enable USB Debugging

How to use UIAutomatorviewer?

Just follow below steps :

1. Open Application under test on android device.
NOTE : Here, we are taking an example Android Calculator app, just open it on your android device.
calculatorapp
2. Open Command Prompt and then navigate to your Android SDK’s \platform-tools\ directory
(Eg. cd C:\Users\himanshu.puri\AppData\Local\Android\sdk\platform-tools). Run “adb devices” command.
You should see your connected devices listed in Command Prompt window. As android device is already connected to computer via USB, so you should be able to see the unique id of it. We are doing this action just to make sure that the android device is successfully connected or not.
adbdevices
3. Now, navigate to \Android\sdk\tools\bin directory.

androidsdkbindirectory

4. Double click on uiautomatorviewer.bat file. It will open up UIAutomation Viewer.
UIAutomator
5. Click on the Device screenshot option on the top left. It will take the screen shot of your android device.
device-screenshot
6. Now move your mouse to an element which you want to inspect and click on it. Now you will be able to see the properties of that element on the right side of UIAutomationviewer.
UIAutomatorInspector

Lets discuss about the possible locators that we can use to identify UI Element

Let’s take an example of calculator app. Refer above screen shot.

First one is : X-PATH
Most of you aware of x-path as we use it in web automation testing.

X-path for button 7 in Calculator App would be :

//android.widget.Button[@text=’7′]

Command to click on button 7 using XPATH would be :

driver.findElement(By.xpath("//android.widget.Button[@text='7']")).click();
//In Appium, xpath starts with class name.

Second one is : ID
Id is always our first priority, so if we have the ID then there is no need to use any other locator.

Command to click on button 7 using ID would be :

driver.findElement(By.id("com.sec.android.app.popupcalculator:id/bt_07")).click();

Third one is : className

driver.findElement(By.className("android.widget.Button")).click();

//In the above screen shot, class name is same for all buttons. So, above command will identify the first element from the top. Using class name in this case is not a good idea.

If you find the information provided above is useful, please don’t forget to like us on Facebook, you can also leave the comment.

Leave a Reply

Your email address will not be published. Required fields are marked *