UIAutomator

UI Automator Viewer

The uiautomatorviewer tool provides a convenient GUI to scan and analyze the UI components currently displayed on an Android device. You can use this tool to inspect the layout hierarchy and view the properties of UI components that are visible on the foreground of the device. This information lets you create more fine-grained tests using UI Automator, for example by creating a UI selector that matches a specific visible property.

The uiautomatorviewer tool is located in the <android-sdk>/tools/ directory.

Access to device state

The UI Automator testing framework provides a UiDevice class to access and perform operations on the device on which the target app is running. You can call its methods to access device properties such as current orientation or display size. The UiDevice class also let you perform actions such as:

  • Change the device rotation
  • Press a D-pad button
  • Press the Back, Home, or Menu buttons
  • Open the notification shade
  • Take a screenshot of the current window

For example, to simulate a Home button press, call the UiDevice.pressHome() method.

UI Automator APIs

The UI Automator APIs allow you to write robust tests without needing to know about the implementation details of the app that you are targeting. You can use these APIs to capture and manipulate UI components across multiple apps:

  • UiCollection: Enumerates a container's UI elements for the purpose of counting, or targeting sub-elements by their visible text or content-description property.
  • UiObject: Represents a UI element that is visible on the device.
  • UiScrollable: Provides support for searching for items in a scrollable UI container.
  • UiSelector: Represents a query for one or more target UI elements on a device.
  • Configurator: Allows you to set key parameters for running UI Automator tests.

For example, the following code shows how you can write a test script that brings up the default app launcher in the device:

// Initialize UiDevice instance
mDevice = UiDevice.getInstance(getInstrumentation());

// Perform a short press on the HOME button
mDevice().pressHome();

// Bring up the default launcher by searching for
// a UI component that matches the content-description for the launcher button
UiObject allAppsButton = mDevice
        .findObject(new UiSelector().description("Apps"));

// Perform a click on the button to bring up the launcher
allAppsButton.clickAndWaitForNewWindow();

To learn more about using UI Automator, see the API reference and Testing UI for Multiple Apps training.

Testing Support Library Setup


The Android Testing Support Library package is included with the latest version of the Android Support Repository, which you can obtain as a supplemental download through the Android SDK Manager.

To download the Android Support Repository through the SDK Manager:

  1. Start the Android SDK Manager.
  2. In the SDK Manager window, scroll to the end of the Packages list, find the Extras folder and, if necessary, expand to show its contents.
  3. Select the Android Support Repository item.
  4. Click the Install packages... button.

After downloading, the tool installs the Support Repository files to your existing Android SDK directory. The library files are located in the following subdirectory of your SDK: <sdk>/extras/android/m2repository directory.

The Android Testing Support Library classes are located under the android.support.test package.

To use the Android Testing Support Library in your Gradle project, add these dependencies in your build.gradle file:

dependencies {
  androidTestCompile 'com.android.support.test:runner:0.4'
  // Set this dependency to use JUnit 4 rules
  androidTestCompile 'com.android.support.test:rules:0.4'
  // Set this dependency to build and run Espresso tests
  androidTestCompile 'com.android.support.test.espresso:espresso-core:2.2.1'
  // Set this dependency to build and run UI Automator tests
  androidTestCompile 'com.android.support.test.uiautomator:uiautomator-v18:2.1.2'
}

To set AndroidJUnitRunner as the default test instrumentation runner in your Gradle project, specify this dependency in your build.gradle file:

android {
    defaultConfig {
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
}

It is strongly recommended that you use the Android Testing Support Library together with the Android Studio IDE. Android Studio offers capabilities that support test development, such as:

  • Flexible Gradle-based build system that supports dependency management for your test code
  • Single project structure to contain your unit and instrumented test code together with your app source code
  • Support for deploying and running tests on virtual or physical devices, from a command line or graphical user interface

For more information about Android Studio and to download it, see Download Android Studio and SDK Tools.

posted @ 2016-01-28 20:30  to be crazy  阅读(1243)  评论(0编辑  收藏  举报