Remember my post about MonkeyTalk? UIAutomator seems to be even better automated andrroid app test suite – it does not require any dedicated libs in Android project and can work in any application (even if you do not have access to its source code. All this because it is nested in Android OS, so it can control anything and emulate clicks in any application.
This post will help you to set up basic UIAutomator test.
Note: remember that UIAutomator will work only in Android 4.1+
Instruction is based on this reference.
1. Create new Java Project
Create new Java project that will generate jar with UIAutomator tests. This jar will be later deployed on the Android device (Not: jar, not apk). The jar is separate from application. Thanks to that, it can automate clicks not only in the app view, but whole Android.
2. Add JUnit library
UIAutomator is built upon the JUnit, so you need to add it to build path by
Right click > Properties > Java Build Path > Add Library > JUnit > select JUnit3
3. Add UIAutomator library
On the same window, click ‘Add External JAR’ and navigate to SDK directory > platforms. There choose the android.jar and uiautomator.jar from latest platform available
4. Create sample uiautomator test case
Add new file to the src directory in your project:
New > JUnit Test Case
Make it extend the UiAutomatorTestCase, give it name you like. Mine test will try to automatically click the device back button:
public void testBackPress() {
getUiDevice().pressBack();
}
5. Create test project build configuration
Android tools can create ant build.xml file containing the build configuration for test project. It is issued by this command:
/tools/android create uitest-project -n -t 1 -p
-n is the project name param
-p is the path to your project param
-t is the target installed on your system
To set the -t param, according to help in this post, execute:
android list targets
and choose the highest id installed, in my case it is:
id: 34 or "Google Inc.:Google APIs:19"
Name: Google APIs
Type: Add-On
Vendor: Google Inc.
Revision: 2
Description: Android + Google APIs
Based on Android 4.4.2 (API level 19)
As a result you should be informed, that the build.xml file was created
6. Build project with ant
Navigate to project directory and build project with ant from command line:
ant build
To avoid the
android-sdk\tools\ant\uibuild.xml:183: Error running javac.exe compiler
Ensure that your default JAVA_HOME points to JDK installation, not the JRE, and check if your compiler is in the PATH. Try to type “javac” in the command prompt. If javac is not found, then you should put your bin directory in the PATH.
For example, in Windows:
SET PATH=c:/jdk1.5.0_07/bin;%PATH%
Then restart eclipse if needed. What is more – I needed to reboot my computer to make it work and find javac!
7. Copy test project to your device
You can do it with file explorer or use adb push command like this:
adb push /sdcard/
replacing the with path to .jar file generated by and build
8. Run test
execute adb shell command to run the particular class in test:
adb shell uiautomator runtest /sdcard/ -c
which for example may look like:
adb shell uiautomator runtest /sdcard/MyUIAutomatorTest.jar -c com.looksok.uiautomator.TestSampleBackButton
You can even run particular method in chosen class by calling the method name like that:
-c com.looksok.uiautomator.TestSampleBackButton#testMethod
Did I help you?
I manage this blog and share my knowledge for free, sacrificing my time. If you appreciate it and find this information helpful, please consider making a donation in order to keep this page alive and improve quality

Thank You!
Like this:
Like Loading...