home.


Tagged: android-uiautomator


Android UiAutomator: Running a single test

To run ui automator tests, you need to use the shell in ADB.

adb shell uiautomator runtest your_tests.jar -e class your_full_class_name_inc_package

You can launch the device’s shell then run ‘uiautomator runtest your_tests.jar -e class your_full_class_name_inc_pacakge’ but if you do that you don’t keep the history of your command in bash, zsh or whatever shell you use.

To run a single test, append #YourClassName to the previous command.

This all assumes you’ve added the tests jar to your device by running the ‘install’ command in the uibuild.xml ant file that comes with UI automator.

android android-uiautomator

Android: Gradle with UiAutomator

UiAutomator does not directly support Gradle as yet, but you can get it to work with this gradle build file, calling in the ant file in your sdk:

ant.properties['sdk.dir']="$System.env.ANDROID_HOME"
ant.properties['target']='android-19'
ant.properties['out.filename']=project.name+'_tests.jar'
ant.properties['out.dir']=project.projectDir.toString()+'/bin/'
ant.properties['source.dir']=project.projectDir.toString()+'/uiTests' // Or wherever your tests are


task uiBuild() << {
    ant.project.executeTarget('build')
}

task uiInstall(dependsOn: ['uiBuild']) << {
    ant.project.executeTarget('install')
}

task uiRun(dependsOn: ['uiInstall']) << {
    Process proc = ["adb", "shell", "uiautomator", "runtest", project.name+"_tests.jar", "-e because broken"].execute()
    proc.consumeProcessErrorStream(System.err)
    proc.consumeProcessOutputStream(System.out)
    if (proc.waitFor() != 0) {
    throw new RuntimeException('exec failed')
    }
}

(Note: Ensure your ANDROID_HOME is set)

You should change the ‘target’ and ‘source.dir’ above to suit you.

This means you do not need to put the uiautomator files in a different directory. I am running them in my main project, under the ‘uiTests’ directory.

Now run it:

$ gradle uiRun
:uiBuild
Android SDK Tools Revision 22.3.0
Installed at /home/user/android-sdk-linux
Using latest Build Tools: 19.0.0
Project Target:   Android 4.4
API level:        19
input: /home/user/android-sdk-linux/tools/ant/bin/classes
No new compiled code. No need to convert bytecode to dalvik format.
:uiInstall
:uiRun
INSTRUMENTATION_STATUS: numtests=1
INSTRUMENTATION_STATUS: stream=
org.denevell.droidnatch.uitests._1_ListThreads:
INSTRUMENTATION_STATUS: id=UiAutomatorTestRunner
INSTRUMENTATION_STATUS: test=testListThreads
INSTRUMENTATION_STATUS: class=Your.Test.Class
INSTRUMENTATION_STATUS: current=1
INSTRUMENTATION_STATUS_CODE: 1
// Any System.out.println you have set
INSTRUMENTATION_STATUS: numtests=1
INSTRUMENTATION_STATUS: stream=.
INSTRUMENTATION_STATUS: id=UiAutomatorTestRunner
INSTRUMENTATION_STATUS: test=testListThreads
INSTRUMENTATION_STATUS: class=Your.Test.Class
INSTRUMENTATION_STATUS: current=1
INSTRUMENTATION_STATUS_CODE: 0
INSTRUMENTATION_STATUS: stream=
Test results for WatcherResultPrinter=.
Time: 11.565

OK (1 test)


INSTRUMENTATION_STATUS_CODE: -1

BUILD SUCCESSFUL

Total time: 14.871 secs

Your new uiautormator jar will now be in your /bin directory. (You can change that in the above script).

Beware, that if you have no devices attached in debugging mode, it will just return

:uiInstall FAILED

FAILURE: Build failed with an exception.

* Where:
Build file '/home/user/workspace/Natch-Android/build.gradle' line: 12

* What went wrong:
Execution failed for task ':uiInstall'.
> exec returned: 1
android android-uiautomator gradle

Page 1 of 1