Sunday, September 22, 2013

Starting with uiautomator


Uiautomator:


Uiautomator was introduced along with the release of Android Jelly Bean 4.2.
Its a perfect tool to start off with your UI Testing on Android Devices.
Its a clear indication of support provided by Google to help the testers struggling with their changing frameworks.

Prerequisites to test:

  • Have your Android SDK version upgraded to revision 21.
  • If you do not have the Android SDK installed in your system. Here is the link to get going: http://developer.android.com/sdk/index.html
  • Your test device must have an Android API version 16 or more. 
  • You can the device's Android API version through the following command:  
  • adb shell getprop ro.build.version.release
    

Starting with the tests:

  • Open Eclipse downloaded from the above link and create a new Java project
  • Import the android.jar and uiautomator.jar  into your project from the following path : <adt-bundle>/sdk/platforms/<android-17/18>/
  • You can import the jars in you project by:
    • Right-click Project -> Properties -> Java Build Path -> Libraries ->Add External JAR's

Code Snippet:

    package com.uiautomator.tests;
    
    import com.android.uiautomator.core.UiObject;
    import com.android.uiautomator.core.UiObjectNotFoundException;
    import com.android.uiautomator.core.UiSelector;
    import com.android.uiautomator.testrunner.UiAutomatorTestCase;
    
    public class FirstTest extends UiAutomatorTestCase{
     
     public void testFirst() throws UiObjectNotFoundException {
                    getUiDevice().pressHome();
      UiObject obj = new UiObject(new UiSelector().text("ButtonText"));
      obj.clickAndWaitForNewWindow();
     }
    }
    

Execution of tests:

The execution of the tests is an easy four step process:

Step 1:
Create the configuration file to build the jar file:
<android-sdk>/tools/android create uitest-project -n <name> -t 1 -p <path>
The is the name of the project that contains your uiautomator test source files, and the is the path to the corresponding project directory.

Step 2:
Create the JAR file. Go to the project directory where your build.xml is located. Then execute:
ant build

Step3:
Push the jar file to the device:
adb push ~/dev/workspace/Settings/bin/LaunchSettings.jar /data/local/tmp/

Step4:
Trigger the tests on the device after pushing the jar file to device:
adb shell uiautomator runtest LaunchSettings.jar -c com.my.LaunchSettings

4 comments:

  1. Hi Akash,

    Nice blog.
    I have recently published an open source project UIautomator-bot that simplifies creation and execution of Android UIautomator. You can download it from here http://sourceforge.net/projects/uiautomator/

    For further info you can check http://uiautomator-bot.blogspot.in/ . Waiting for your valuable inputs.

    Thanks
    Syed Mehtab

    ReplyDelete
  2. This comment has been removed by the author.

    ReplyDelete
  3. Hi
    On android pie "adb shell uiautomator dump" command quite often produces "ERROR: null root node returned by UiTestAutomationBridge". Please anyone help me to solve this issue.

    ReplyDelete