zoukankan      html  css  js  c++  java
  • 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.

  • 相关阅读:
    英语面试自我介绍范文(二)
    在PHP中PDO解决中文乱码问题的一些补充
    英文面试自我介绍(一)
    添加XP/2003的网络用户和密码及用户自动登录
    Windows Server 2000/2003/2008错误
    数据库连接字符串大全 (转载)
    flash中特殊字符解析的使用
    创建T100Monitor技术研究过程
    LINQ,SQL查询,LINQ 实现常见SQL查询
    Web.Config 的图形配置及配置项含意
  • 原文地址:https://www.cnblogs.com/tobecrazy/p/5167416.html
Copyright © 2011-2022 走看看