We learnt how to use UI Automator Viewer in PREVIOUS POST to locate and get properties details of android native software app's any element. Now we need to learn what are the different ways to locate elements of android software app and how to determine those locators to use them in test script. Most successful ways to locate android software app's elements are XPath, ID, Name And className so we will learn them rights now. Also we can use findElements method of webdriver to locate element of android app.We will use UI Automator Viewer to locate android app element and get it property and hierarchy detail.
PREREQUISITES : Previous 9 STEPS of appium android software test automation tutorials should be completed.
Locating Android App Element By XPath
Earlier we learnt how to locate element by XPath in selenium webdriver software testing tool in THIS POST and also given different examples to write XPath of element in different ways in THIS POST. So i am suggesting you to refer both these posts before going ahead.
We will use android calculator app to get element's XPath in different ways. Supposing I need XPath of button 5. So i can write XPath in multiple ways for it as bellow.
UI Automator Viewer screenshot for button 5 is as bellow. You can download Android Calculator App from THIS PAGE if it is not available with you.
1. XPath using class and text attribute :
In above image you can see that button 5 has text attribute with value = 5 which is unique. Class name is android.widget.Button which is same for all number buttons. So we can create XPath using text attribute value with class name as bellow.
xpath("//android.widget.Button[@text='5']")
2. XPath using class and resource-id :
Here resource-id for button 5 is com.android.calculator2:id/digit5. Trailing part (e.g digit5, digit4, digit7) of resource-id is changing for every number buttons so we can use contains function to get relative XPath of button 5 as bellow.
xpath("//android.widget.Button[contains(@resource-id,'digit5')]")
3. XPath using class, text attribute and resource-id :
We can use combination of text attribute and resource-id with class name to create unique XPath of element button 5 as bellow.
xpath("//android.widget.Button[contains(@resource-id,'digit5') and @text='5']")
4. XPath using class, text attribute and index :
We can use same approach as described in point 3 but with index number. Button 5 has index 1 but it is not unique as number buttons 0,2 and 8 has also same index with same class name. So we can include text attribute with index attribute to locate number button 5.
xpath("//android.widget.Button[@text='5' and @index='1']")
5. XPath using parent and child class hierarchy
As you can see in above image, 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("//android.widget.LinearLayout[@index='1']/android.widget.Button[@index='1']")
6. XPath using content-desc
Supposing element has content-desc which is unique then you can use it to create XPath. Example : DELETE button has unique content-desc = delete. So we can write XPath for delete button using it as bellow.
xpath("//android.widget.Button[@content-desc='delete']")
7. XPath using class name
Note : Using class name In By.className or By.xpath will works only if it is unique for element. If same class name is provided to multiple elements then it will not work.
If software application's element has a class name and it is unique for element then you can use class name only to create XPath. See above image. Options button has class name android.widget.ImageButton which is unique. So we can use it only to create xpath as bellow.
xpath("//android.widget.ImageButton")
Locating Android App Element By ID
Earlier we learnt how to locate element by ID for web application in THIS POST. We can locate elements by ID in android app too. We can use resource-id as a id. Example is as bellow.
resource-id for button 5 is "com.android.calculator2:id/digit5" so we can locate to it by id as bellow.
id("com.android.calculator2:id/digit5")
Locating Android App Element By className
Same way, We can use class name to locate element if it is unique.
Class name for above option button is "android.widget.ImageButton". We can locate it using className as bellow.
By.className("android.widget.ImageButton")
Locating Android App element by Name
It is possible to locate element by name if element contains unique text.
If you see in above image, button Equals has text value "=". So we can locate it by name using bellow given syntax.
By.name("=")
Locating element by findElements
If you inspect all buttons of android calculator app, All the buttons has same class "android.widget.Button". If you know, findElements method is useful to get list of all the matching elements from current page as per given element locator mechanism. VIEW MORE DETAIL on findElements. So here we can collect list of all buttons using findElements and store list using java
List interface. Then we access required element using get() method of List interface.
Array list Index IDs of button elements of "android.widget.Button" class is as bellow.
List<WebElement> calcButtons = driver.findElements(By.xpath("//android.widget.Button"));
Above given syntax will store list of all buttons in list calcButtons with bellow given array list index ids. Bellow given table illustrate array index ids for different buttons of calculator application.
Button
|
Array index ID
|
DELETE
|
0
|
7
|
1
|
8
|
2
|
9
| 3 |
÷(divide)
| 4 |
4
| 5 |
5
| 6 |
6
| 7 |
×(multiply)
| 8 |
1
| 9 |
2
| 10 |
3
|
11
|
−(minus)
| 12 |
.(point)
| 13 |
0
| 14 |
=(equals)
| 15 |
+(plus)
| 16 |
So now we can access required button using it's index which is stored in list calcButtons. Means if you wants to tap on button 5 then you can use bellow given syntax.
calcButtons.get(6).click();
Here 6 is list index id of button 5 as shown in above table.
So all these are different ways to locate android software app elements. We will use all these element locator methods practically in our test scripts which I will publish in upcoming steps.
Hey very nice article and very informative
ReplyDeleteCan u please post advance selenium topics like integrating of autoit jars to selenium
Deletein ur lear-automation block......please
Hi Mukesh sir....can u add some more posts in ur block like advance selenium topics like integrating autoit jars to selenium....thanks in advance
DeleteHi all ...
DeleteI have a system with following configuration: intel i5@2.70Ghz and 8.00 ram. But in my system when tried to test a mobile app (Calculator) following each step perfectly the emulator itself did not run properly . it was too slow. can anyone give a solution to my problem
i dont have a physical device to test
plz help me
Thanks a lot Arvind. wonderful explanation. you are Amazing. it helped me lot to understand the mobile automation testing
ReplyDeleteThanks you Aravind
ReplyDeletevery useful link... I like it.
ReplyDeletevery useful link... I like it.
ReplyDeleteGreat one Sir ... we owe you !
ReplyDeleteVery useful one. Thank you.
ReplyDeletevery useful site...thankyou
ReplyDeleteThanks a lot. Very useful site..
ReplyDeleteVery good info... Thanks
ReplyDeleteSimple and awesome
ReplyDeleteHi ,Nice tutorials but i observed in this topic that please provide all elements related xpath or any other where we can easily identify the edit boxes and able to enter text using sendKeys but u have taken only button .My question is if incase there is not text: ,claname is same for all editboxes how can i identify elements using ui automator
ReplyDeleteHi Sir,
ReplyDeleteI have a Question that in my app there are multiple Frame layouts and Multiple Linear Layouts in a Screen. There is no unique id's for the objects in that layouts.
They have given only index and class names. All class names are same. I am struck up here and unable to move forward. Can any one help me out to move forward.
You may use XPath using parent and child class hierarchy as you have relative indexes.
DeleteI have written code like this but not working
ReplyDeletedriver.findElementById("com.taubman.shopshorthills:id/register_letus_go").click();
driver.findElementByClassName("android.widget.FrameLayout").findElement(By.className("android.widget.LinearLayout")).findElements(By.id("com.taubman.shopshorthills:id/favorites_data_linearlayout"));
driver.findElementById("android.widget.LinearLayout").click();
driver.findElementById("com.taubman.shopshorthills:id/categoriesTransparentLayer").click();
May I know how to select whole frameLayout as webelement?
If any other tool available in the same as UI automator?
ReplyDeleteIf any other tool available in the same as UI automator?
ReplyDeleteI have getting error while using following code:-
ReplyDeletedriver.findElement(By.xpath("//com.hmh.api.widget.TextView[contains(@resource-id,'date_picker_day')]")).sendKeys("25");
driver.findElement(By.xpath("//com.hmh.api.widget.TextView[contains(@resource-id,'date_picker_month')]")).sendKeys("Aug");
driver.findElement(By.xpath("//com.hmh.api.widget.TextView[contains(@resource-id,'date_picker_year')]")).sendKeys("2015");
Thank you for the article! we were using resource-id as xpath and now we can use id. Keep sharing.. :)
ReplyDeleteGreat articles, many thanks...
ReplyDeleteI'm wondering, is the speed of localization an element depends on the xpath or method we use ?
For example maybe using parent and child class hierarchy will be faster that using only the id ?
Hey
ReplyDeleteHave any method to find out the element id of android app in selendroid if i debug the unknown app.
Great article, it is help me a lot
ReplyDeleteThanks
Great help!
ReplyDeleteHow TO Perform Logout Task On Android App through Selenium
ReplyDeleteReally useful. Thank u dude.
ReplyDeleteHow do I identify a password field which has similar property as user id field. Only unique property is bound. Can someone help please> ?
ReplyDeleteNicely described, could you explain the difference between clickable and enabled in the node details. I found cases where clickable is false and yet enabled were true. It seems a bit confusing
ReplyDeleteNice article! thanks for the info.
ReplyDeleteI getting this error :
ReplyDeleteorg.openqa.selenium.NoSuchElementException: An element could not be located on the page using the given search parameters. (WARNING: The server did not provide any stacktrace information)