Monday, October 14, 2013

Automatica UI testing via UiAutomation for Android app

Google released a UI testing framework named UiAutomator in Android 4.1. Basically it provides functions to enumerate the currently forground activity UI hierarchy to get all the view items. Different with Monkey Runner, UIAutomator is not coordinate based, you can get the view item by it's label text or class name, and so on.

The UiAutomator is based on JUnit3. That means it is NOT a Android project, you can write the test case in Java but not Android Java, and get the jar file as the compiling result. The sample and introduction can be found on Android develop site here.

Except writing test case part(which take mostly effort actually), to get the UiAutomator run you need below steps:

1. Have JDK and ANT installed.

2. Create a Java project, NOT Android, in Eclipse.

3. In Properties > Java Build Path, select "Add Library > JUnit" to add JUnit3 lib, and select Add External JARs… ”to add android.jar and uiautomator.jar which located in android SDK folder.

4. Create the test case class, just use this file as mock up now. Let's say your project name is TestUI, it located at c:\workspace\TestUI, package name is com.testui, and you have the class as com.testui.LaunchSettings

5. In terminal, execute this command to get a build.xml:
        $android create uitest-project -n <project-name> -t 1 -p <project-path>
   so your command looks like this:
        $android create uitest-project -n TestUI -t 1 -p c:\workspace\TestUI

6. Execute this command to build the project:
        $ant build
   Watch the output to make sure the compiling is succeed.

7. Push the jar file into phone:
         $adb push bin\TestUI.jar /data/local/tmp

8. Run the test case:
          $adb shell uiautomator runtest TestUI.jar -c com.testui.LaunchSettings

Here we are all set.

No comments:

Post a Comment

Thrift fails on Windows - thrift failed error: The command line is too long.

Meet a thrift compiling failure on my project on Windows 7. Using Maven in IntelliJ so I choiced maven-thrift-pluging for thrift files. For ...