Friday 26 April 2019

Locating UI Element Using uiautomator

How to use uiautomator for inspecting UI elements on mobile?

So there are multiple ways available by which you can identify the element on mobile. some of them as below. So here I am taking an example of Hotstar app for illustrating the various ways of element identification. Also I am using python as my scripting language.

1. By ID

This is the most efficient way to locate an element as IDs are unique. This is the most prefered way to identify the elements of android app. 


So for this the code will be as below.
app_opened=d(packageName='in.startv.hotstar', resourceId='in.startv.hotstar:id/iv_ad_background').wait.exists(timeout=20000)  
You can see that I have highlighted the portion where it depicts that element is identified by ID.

2. By className:

A class contains many elements. So you have to little careful while using element identification using class name because if an element has many classes then it will match against each of them. 



So for this the code will be as below.
home_page_click=d(packageName='in.startv.hotstar', className='android.support.v4.view.ViewPager',resourceId='in.startv.hotstar:id/pager').click()

3. By Name:

It is possible to locate the element by name but they should be unique.



So for this the code will be as below.
dream11=d(packageName='in.startv.hotstar',resourceId='in.startv.hotstar:id/tv_header',  text='Dream11').click()  

4. By Xpath:

The very last option to be used for identifying the elements. You end up in many situations that you don't have option but to use xpath for identifying the elements.There are two types of xpath, relative xpath and absolute xpath. It is suggested to use relative xpath because absolute xpath uses indexes and if there is any change in the code, it will result in the failure of test scripts which is trying to identify the UI element.

So for this the code will be as below.
dream11 = d(By.xpath("//*[@text = 'Dream11']"))



No comments:

Post a Comment

Feature Posts

Python Appium - Step by step procedure for capturing screenshot

 Why To capture screenshot? It is as important as your logs. If there is any failure for any test scenario, we can provide screenshot for th...