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:
- Start the Android SDK Manager.
- 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.
- Select the Android Support Repository item.
- 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的更多相关文章
- Uiautomator 2.0之BySelector类学习小记
1. BySelector与By静态类 1.1 BySelector类为指定搜索条件进行匹配UI元素, 通过UiDevice.findObject(BySelector)方式进行使用. 1.2 By类 ...
- Uiautomator 2.0之UiDevice新增API学习小记
1. InstrumentationRegistry类 1.1. 类说明: 一个暴露的注册实例,持有instrumentation运行的进程和参数,还提供了一种简便的方法调用instrumentati ...
- Uiautomator ——API详解(转载http://www.cnblogs.com/by-dream/p/4921701.html)
转载来源: 简单的例子 以一个简单的例子开始吧.我们完成一个 " 打开QQ,进入QQ空间,然后退出 " 的case. 代码如下: package QQ; import java.i ...
- UIautomator Python测试
#!/usr/bin/env python # -*- coding: utf-8 -*- import unittest from mock import MagicMock, patch impo ...
- uiautomator跑安卓端UI testing
用uiautomator做安卓的app端的UI testing的环境搭建及编jar包和运行case的步骤如下: 1.新建java工程 2.右键properties, 添加junit4的library, ...
- UiAutomator自动化测试框架介绍
UiAutomator自动化测试框架介绍 环境搭建 1 必要条件 1.1 JDK 1.2 SDK(API高于15) 1.3 Eclipse 2 ...
- android app自动化测试之UIAutomator
一.UIAutomator Android自动化测试工具有很多,但是要免费.易上手,本人觉得就直接使用Eclipse自带的UIAutomator就不错.测试人员无需跟开发要代码信息,只要手机上有安装之 ...
- 转载:Robotium之Android控件定位实践和建议(Appium/UIAutomator姊妹篇)
来源于:http://blog.csdn.net/zhubaitian/article/details/39803857 1. 背景 为保持这个系列的一致性,我们继续用SDK自带的NotePad实例应 ...
- 【Android测试】【第十三节】Uiautomator——如何组织好你的测试代码(项目实战)
◆版权声明:本文出自胖喵~的博客,转载必须注明出处. 转载请注明出处:http://www.cnblogs.com/by-dream/p/4996000.html 前言 前面我们已经了解Uiautom ...
随机推荐
- 如何把Spring制作成jar包,然后在项目里运行。
第一步:首先我们先把Spring的代码准备好.如图一 (图1). 第二步:我们在桌面新建一个文件夹,如图二 (图2). 我们要在这个文件夹里新建两个夹,一个文件夹是你项目的包名,也就是我们图1的aop ...
- Newick format tree
1. all branches + leaf names + internal supports ((D:0.723274,F:0.567784)1.000000:0.067192,(B:0.2793 ...
- Yii2开启enableprettyurl(url美化)无效
最终显示的url格式为:http://localhost/yii2/frontend/web/site/about 在/config/main.php中 'components'=>[] 中添加 ...
- PAT Basic Level 1001
大纲考察内容 数据存储结构:数组.链 基础算法:递归.排序.计算时间复杂度.空间复杂度.分析算法稳定性 1001.害死人不偿命的(3n+1)猜想 (15) https://www.patest.cn/ ...
- struts2-上传文件
package web; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; i ...
- 字符串 中的split 与数组中的join
关于基础,总是隔一段时间,就得看一次,要不不用总是忘,今天又重新看了,一下字符串对象的split,然后就想到了数组对象的join. var str='wo shi yi ge js'; var str ...
- Table 固定表头的几种方法
<style type="text/css"> /*所有内容都在这个DIV内*/ div.all { border: 3px solid #FF00FF; width: ...
- JQuery------分页插件下载地址
转载GitHub: https://github.com/pgkk/kkpager
- 三、基于hadoop的nginx访问日志分析--计算时刻pv
代码: # cat pv_hour.py #!/usr/bin/env python # coding=utf-8 from mrjob.job import MRJob from nginx_acc ...
- vue自定义指令
Vue自定义指令: Vue.directive('myDr', function (el, binding) { el.onclick =function(){ binding.value(); } ...