uiautomator

The uiautomator testing framework lets you test your user interface (UI) efficiently by creating automated functional UI testcases that can be run against your app on one or more devices.

For more information on testing with the uiautomator framework, see UI Testing.

Syntax

To run your testcases on the target device, you can use the adb shell command to invoke the uiautomator tool. The syntax is:

adb shell uiautomator runtest <JARS> -c <CLASSES> [options]

Here’s an example:

adb shell uiautomator runtest LaunchSettings.jar -c com.uia.example.my.LaunchSettings

Command-line Options

The following table describes the subcommands and options for uiautomator.

Table 1. Command-line options for uiautomator

Subcommand Option Description
runtest <JARS> Required. The <JARS> argument is the name of one or more JAR files that you deployed to the target device which contain your uiautomator testcases. You can list more than one JAR file by using a space as a separator.
-c <CLASSES> Required (API 17 or lower).The <CLASSES> argument is a list of test classes or test methods in <JARS> to run.

Each class or method must be fully qualified with the package name, in one of these formats:

  • package_name.class_name
  • package_name.class_name#method_name
You can list multiple classes or methods by using a space as a separator.

Note:This argument is not required for API 18 and higher. If not specified, all test cases in <JARS> will be run.

--nohup Runs the test to completion on the device even if its parent process is terminated (for example, if the device is disconnected).
-e <NAME> <VALUE>

Specify other name-value pairs to be passed to test classes. May be repeated.

Note: The -e options cannot be combined; you must prefix each option with a separate -e flag.

-e debug [true|false] Wait for debugger to connect before starting.
dump [file] Generate an XML file with a dump of the current UI hierarchy. If a filepath is not specified, by default, the generated dump file is stored on the device in this location /storage/sdcard0/window_dump.xml.
-e outputFormat simple | -s Enables less verbose JUnit style output.
events   Prints out accessibility events to the console until the connection to the device is terminated

uiautomator API

The uiautomator API is bundled in the uiautomator.jar file under the <android-sdk>/platforms/ directory. The API includes these key classes, interfaces, and exceptions that allow you to capture and manipulate UI components on the target app:

Classes

Class Description
com.android.uiautomator.core.UiCollection Used to enumerate a container's user interface (UI) elements for the purpose of counting, or targeting a sub elements by a child's text or description.
com.android.uiautomator.core.UiDevice Provides access to state information about the device. You can also use this class to simulate user actions on the device, such as pressing the d-pad hardware button or pressing the Home and Menu buttons.
com.android.uiautomator.core.UiObject Represents a user interface (UI) element.
com.android.uiautomator.core.UiScrollable Provides support for searching for items in a scrollable UI container.
com.android.uiautomator.core.UiSelector Represents a query for one or more target UI elements on a device screen.
com.android.uiautomator.core.Configurator Allows you to set key parameters for running uiautomator tests.

Interfaces

Interface Description
com.android.uiautomator.core.UiWatcher Represents a conditional watcher on the target device.
com.android.uiautomator.testrunner.IAutomationSupport Provides auxiliary support for running test cases.
com.android.uiautomator.testrunner.UiAutomatorTestCase Defines an environment for running multiple tests. All uiautomator test cases should extend this class.

Exceptions

Exception Description
com.android.uiautomator.core.UiObjectNotFoundException Indicates when a a UiSelector could not be matched to any UI element displayed.