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.
How to set android home variable?
How to use android SDK with Eclipse?
How to set SDK location?
How can we verify android SDK is configured properly with eclipse?
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.
X path can be created using text attribute value with class name.
XPath = xpath(„//android.widget.Button[@text=’5′]“)
Here we use contains function to get the Xpath like shown below.
Xpath = xpath(„//android.widget.Button[contains(@resource-id,’digit5′)]“)
XPath = xpath(„//android.widget.Button[contains(@resource-id,’digit5′) and @text=’5′]“)
Xpath = xpath(„//android.widget.Button[@text=’5′ and @index=’1′]“)
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′]“)
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‘]“)
If class name is unique, we can use this method to find the xpath.
Xpath = xpath(„//android.widget.ImageButton“)
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“)
If class name is unique, we can use it to locate elements.
Here element can located by “By.className(„android.widget.ImageButton“)”.
If the element contains unique text, it is possible to locate by its name.
Here ‘=’ can be located by “By.name(„=“)”.
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
Delete
0
7
1
8
2
9
3
÷
4
5
6
X
10
11
–
12
.
13
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.
Du musst angemeldet sein, um einen Kommentar abzugeben.