Feb 22, 2014

UIAutomator Tutorial - Part I


Reference: http://developer.android.com/tools/help/uiautomator


What is UIAutomator / UIAutomator Introduction


  • UIAutomator  is Introduced  by Google and it is available from Jellybean Versions of Android.
  • The UIAutomator is a testing framework provided by Google's Android 

Use of UIAutomator


  • To test Android Applications efficiently by creating automated functional UI test cases that can be run against applications on one or more devices.
  • Works on all the Android applications, including Google’s installed apps such as Settings, Contacts, Phone, etc.

Skills Required for working on UIAutomator


Core Java
Software: ANT, Android SDK, Eclipse

How UIAutomator works

  • Java project is created using android.jar and uiautomator.jar. 
  • uiautomator.jar provided by Android,  has in built methods which are used to perform UI actions  on Android device.
  • Create the Android test project based on your requirement; build a Jar file for the Java project is created and push the JAR file to /data/local/tmp location of Android device. 
  • Using adb shell, one can access the methods inside the Jar

How to build UIAutomator Project


cd D:\UIAutomator
D:
android create uitest-project -n UIAutomatorTest -t 1 -p D:\UIAutomator
ant build
cd bin
adb push UIAutomatorTest.jar /data/local/tmp


The above commands create a JAR file in the bin folder
Then push the JAR file to /data/local/tmp folder of Android phone


How to invoke a method in the JAR file

   adb shell uiautomator runtest UIAutomatorTest.jar -c com.test.uiautomator.Bluetooth#btPair


Debug UIAutomator


  • Using android DDMS tool (present in Eclipse), we can analyze properties of Android screen (Activity)
  • Using android DDMS tool, we can get the screenshot of the android screen and access the layout of the android screen, there implement the logic of what action we want to achieve

Advantages / Pros


  • Free ware, developed by Google, trust-able
  • Simple, uses class object OOPS methodology, less time to implement
  • Handling asynchronous events like Toasts,dialog and alerts occurring  during tests is easy. (JAR approach)

Disadvantages / Cons


  • Works only from Android version 4.1. 



UIAutomator API


  • It provides six different classes, these classes with interfaces and exceptions allows to capture and manipulate UI components on Android Device.

Following are the classes : 
   UiDevice 
   UiObject 
   UiScrollable 
   UiSelector 
   UiCollection 
   UiConfigurator 

UiDevice 

  Provides access to state information about the device. We can  also use this class to simulate user actions on the device, such  as pressing the d-pad hardware button or pressing the Home and Menu buttons. 

UiObject 

 Represents a user interface (UI) element. 

UiScrollable 

 Provides support for searching for items in a scrollable UI . 

UiSelector 

 Represents a query for one or more target UI elements on a device screen. 

UiCollection 

  Used to enumerate  screen elements for the purpose of counting, or targeting a sub elements by a child's text or description. 

UiConfigurator 

  Allows  to set key parameters for running uiautomator tests  



Thanks for reading. I will discuss more samples/examples on UIAutomator in detail in the next post

No comments:

Post a Comment