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.

UIAutomator的更多相关文章

  1. Uiautomator 2.0之BySelector类学习小记

    1. BySelector与By静态类 1.1 BySelector类为指定搜索条件进行匹配UI元素, 通过UiDevice.findObject(BySelector)方式进行使用. 1.2 By类 ...

  2. Uiautomator 2.0之UiDevice新增API学习小记

    1. InstrumentationRegistry类 1.1. 类说明: 一个暴露的注册实例,持有instrumentation运行的进程和参数,还提供了一种简便的方法调用instrumentati ...

  3. Uiautomator ——API详解(转载http://www.cnblogs.com/by-dream/p/4921701.html)

    转载来源: 简单的例子 以一个简单的例子开始吧.我们完成一个 " 打开QQ,进入QQ空间,然后退出 " 的case. 代码如下: package QQ; import java.i ...

  4. UIautomator Python测试

    #!/usr/bin/env python # -*- coding: utf-8 -*- import unittest from mock import MagicMock, patch impo ...

  5. uiautomator跑安卓端UI testing

    用uiautomator做安卓的app端的UI testing的环境搭建及编jar包和运行case的步骤如下: 1.新建java工程 2.右键properties, 添加junit4的library, ...

  6. UiAutomator自动化测试框架介绍

    UiAutomator自动化测试框架介绍 环境搭建 1         必要条件 1.1       JDK 1.2       SDK(API高于15) 1.3       Eclipse 2    ...

  7. android app自动化测试之UIAutomator

    一.UIAutomator Android自动化测试工具有很多,但是要免费.易上手,本人觉得就直接使用Eclipse自带的UIAutomator就不错.测试人员无需跟开发要代码信息,只要手机上有安装之 ...

  8. 转载:Robotium之Android控件定位实践和建议(Appium/UIAutomator姊妹篇)

    来源于:http://blog.csdn.net/zhubaitian/article/details/39803857 1. 背景 为保持这个系列的一致性,我们继续用SDK自带的NotePad实例应 ...

  9. 【Android测试】【第十三节】Uiautomator——如何组织好你的测试代码(项目实战)

    ◆版权声明:本文出自胖喵~的博客,转载必须注明出处. 转载请注明出处:http://www.cnblogs.com/by-dream/p/4996000.html 前言 前面我们已经了解Uiautom ...

随机推荐

  1. java 中正则表达式匹配

    String str = "#a#,#b#"; String reg="\\#+[^\\#]+\\#+"; Pattern p=Pattern.compile( ...

  2. 解决NetBeans运行卡顿问题

    NetBeans安装目录下的此文件打开编辑 找到这一行,在后面添加最大的运行内存,这里我改成了900M(红色部分),重启NetBeans即可.netbeans_default_options=&quo ...

  3. python httplib get和post获取数据

    httplib 下的 status http请求的状态  200 404 500... reason 返回答复 OK或者 FAULRE read()  读取内容 get方法: #!/usr/bin/e ...

  4. BootLoader(2440)核心初始化代码

    1.gboot.lds OUTPUT_ARCH(arm)ENTRY(_start)SECTIONS {    . = 0x30008000; //起始地址        . = ALIGN(4);// ...

  5. win10下的使用

    一.win10下使用win7那样的图片查看器快些. 打开图片的设置,关闭自动增强我的图片 二.查看win10秘钥 1.打开注册表,依次定位:HKEY_LOCAL_MACHINE/SOFTWARE/Mi ...

  6. opencv_判断两张图片是否相同

    QQ:231469242 pip install opencv 如果找不到版本,去非官方下载opencv第三方包http://www.lfd.uci.edu/~gohlke/pythonlibs/ 下 ...

  7. jquery复选框checkbox实现删除

    实现这样一个基本需求,页面有很多数据,可以删除一条或多条,删除前判断是否选中至少一条,否则提示. function deleteUser() { //当时是想把内容以str+="" ...

  8. 分享公司DAO层数据库结果映射到对象的方法

    主题 前面写过一篇文章,分享了公司是怎么动态封装SQL查询条件的(http://www.cnblogs.com/abcwt112/p/5874401.html). 里面提到数据库查询结果二维数组最后是 ...

  9. Axure RP动态面板操作

    参考资料: 动态面板实现banner的轮播效果

  10. 阿里提前批校招内推offer经历

    经过一个半月的阿里内推面试,今天终于收到了阿里的offer邮件 .阿里的内推面试一共有四轮,本人是7月19号投的内推邮件,8月28号收到了offer的邮件.首先本人谈谈内推的看法.内推是公司招聘人才的 ...