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']"))



Different actions performed using uiautomator or Appium

Perform Actions

Once the element is identified, you want to perform different actions just like a normal user does on mobile.

So different actions which you can be performed are:

1. click():
     Clicks the center of the visible UI element

2. dragTo():
    Drags this object to arbitrary coordinates.

3.setText():
   Allows you to set a text in editable text field.

4. ClearTextField():
    This method is used to clear the text field if any text is already available.

5. swipeUp():
    Performs the swipe up action on the UI element.

6. swipeLeft():
    Perform the swipe left action on the UI element.

7. swipeRight():
    Perform the swipe right action on the UI element.

8. swipeDown():
    Perform the swipe down action on the UI element.

Thursday 25 April 2019

Ways to identify mobile app elements

How to identify the elements on mobile UI

You can identify element using either uiautomator viewer or using appium. Let me show you how you can use the uiautomator for identifying the UI elements and later on I will explain element identification using appium

Element identification using uiautomator viewer

It is convenient GUI Tool to scan and identify the UI elements of the app which is currently displayed on the device under test. uiautomator viewer is located at <android-sdk>/tools/bin




Double click on the batch file and you will be able see the uiautomatorviewer. Once you have clicked on the uiautomatorviewer, it will display a window which would be like as below


To check the elements on UI , you need to click on third element from element and then below screen will appear on uiautomatorviewer (Make sure you have connected a mobile device to laptop)

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...