Monday, December 16, 2013

How to Run UiAutomator Tests?

As of now, you have seen some posts on the writing the UiAutomator test code. Now, we see how to run the UiAutomator tests on the Android device

Steps to run UiAutomator tests
  1. Create required build configuration file to build the output jar
  2. Set ANDROID_HOME environment variable
  3. Build the configuration file using ANT
  4. Copy the output Jar file to the device
  5. Run the UiAutomator Test case

Google has provided good documentation on how to run the test. You can find simple steps below

Step 1: Build configuration file
<android-sdk>/tools/android create uitest-project -n <name> -t 1 -p <path>
The <name> is the name of the project that contains your uiautomator test source files, and the <path> is the path to the corresponding project directory.
-t is the target ID. To get the target ID, run the command "android list targets", which will list the available targets
$ android list targets
Available Android targets:
----------
id: 1 or "android-8"
     Name: Android 2.2
     Type: Platform
     API level: 8
     Revision: 3
     Skins: WVGA800 (default), WQVGA400, HVGA, QVGA, WQVGA432, WVGA854
     ABIs : armeabi
----------
.
.
----------
id: 11 or "android-17"
     Name: Android 4.2.2
     Type: Platform
     API level: 17
     Revision: 2
     Skins: WVGA800 (default), WQVGA400, HVGA, WSVGA, QVGA, WXGA800, WXGA800-7in, WXGA720, WQVGA432, WVGA854
     ABIs : armeabi-v7a
----------
id: 13 or "android-18"
     Name: Android 4.3
     Type: Platform
     API level: 18
     Revision: 2
     Skins: WVGA800 (default), WQVGA400, HVGA, WSVGA, QVGA, WXGA800, WXGA800-7in, WXGA720, WQVGA432, WVGA854
     ABIs : armeabi-v7a
 

Select the target ID from the above list, suppose if you want to run on Android 4.2.2, choose the target as "17". Please note that the target ID varies from machine to machine.

Step 2: From the command line, set the ANDROID_HOME variable:
  • In Windows:
    set ANDROID_HOME=<path_to_your_sdk>
  • In UNIX:
    export ANDROID_HOME=<path_to_your_sdk>
Step 3: Go to the project directory where your build.xml file is located and build your test JAR.
ant build
Step 4:  Deploy your generated test JAR file to the test device by using the adb push command:
adb push <path_to_output_jar> /data/local/tmp/
 Here’s an example:
adb push ~/dev/workspace/LaunchSettings/bin/LaunchSettings.jar /data/local/tmp/

Step 5: Running UIAutomator Test
adb shell uiautomator runtest <JAR> -c <Class Name>
Here’s an example of how to run a test that is implemented in the LaunchSettings.jar file. The tests are bundled in thecom.uia.example.my package:
adb shell uiautomator runtest LaunchSettings.jar -c com.uia.example.my.LaunchSettings

Shell Script:

I have written shell script to make this job simple
  • Copy both "run.sh" and "build.sh" to your local directory
  • Provide executable permissions (chmod 777 <File>)
  • Run the following command 
    ./run.sh <Project Name> <Project path> <SDK path> <Class (including the package name)>
  •  Here is an example
    ./run.sh UiAutomator ~/workspace/UiAutomator ~/Android/sdk Notes.CreateNewNote