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!
Cool tutorila, but u forgot to say that need to download ant ))
If you have ADT with Eclipse then you can find ant in the plugins directory of Eclipse. You can also run ant from within Eclipse (http://blog.blundell-apps.com/running-an-ant-script-with-eclipse/)
It is obvious. Thinking that way I could have also written that you need to download eclipse
Can u just tell me how can i run the different Methods of the class from .xml file…..and how to integrate JUNIT / TestNG with UIAutomator so that i can get the better Result as the output.?
I am afraid I don’t know what you mean saying ‘methods defined in xml’
And regarding jUnit – you can use it with uiautomator, since there is support of it. You can for instance find UIObject add you usually do, get its text content and call jUnit assert method on it. Just simple as that
Hi Jacek,
Is any library file available to create UIAutomation test cases for ‘Accessibility’ testing?
Could you tell any tips for creating ‘Accessiblity (TalkBack)’ test cases.
Thank in advance.
Regards,
Mohan
I am not aware of any tools to du this kind of testing
Hi Jacek,
Is there a way to execute the tests written without following the lengthy procedure of -Creating a jar, building the xml file, pushing the jar file in the device.
Also, can you tell how to test the application on the emulator rather than a device
no, I don’t know any other way than generating jar. UiAutomator is quite new and there is no IDE support yet. But I belive it will be soon
My solution is to create batch/shell script that will execute those commands for you:
adb push /sdcard/
adb shell uiautomator runtest /sdcard/MyUIAutomatorTest.jar -c com.looksok.uiautomator.TestSampleBackButton
You can also push this jar file to an emulator – just like on the device.
Yes, I have recently published an open source tool UIautomator-bot. This exactly does what u want and more. You can download it from http://sourceforge.net/projects/uiautomator/ . UIautomator-bot is currently available for Linux.
Thanks for your work and for sharing it!
Hi Jacek ,
Thanks for sharing this article. I stopped by to brag : ) about my new open source project which makes the task of creating and running test cases simple and easy. You can download it from here http://sourceforge.net/projects/uiautomator/
and for further info refer http://uiautomator-bot.blogspot.in/
Please try this new tool. Waiting for your valuable inputs.
Thanks in advance.
Why JUnit3 and not JUnit4?
How-to-switch-to-other-android-ui-elements-by-using-ui-automator-classes
I am using Android Ui Automator for the Functional testing of Contacts app. My mobile is LG D model. Unable to switch to other Android Ui elements, I have tried the below code By using UiObject and even the UiScrollable classes.
UiScrollable phoneContSwipe = new UiScrollable(new UiSelector().scrollable(true));
UiObject nameTest=phoneContSwipe.getChild(new UiSelector().text(“Name”));
nameTest.setText(“Ramu”);
UiObject phoneText=phoneContSwipe.getChild(new UiSelector().text(“Phone”));
phoneText.setText(“11111111111”);
What is happening is, the nameText and phoneText are entering in the same field.
I have created one android app which runs the command to execute testcase,jar using uiautomator runtest -c command… How can I pass value using uiautomator command and please also tell how to fetch this value in TestCase file which extends UiAutomatorTestCase