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 ...
随机推荐
- JVM_七种垃圾收集器介绍
本文中的垃圾收集器研究背景为:HotSpot+JDK7 一.垃圾收集器概述 如上图所示,垃圾回收算法一共有7个,3个属于年轻代.三个属于年老代,G1属于横跨年轻代和年老代的算法. JVM会从年轻代和年 ...
- 【BZOJ 3993】【SDOI 2015】星际战争
http://www.lydsy.com/JudgeOnline/problem.php?id=3993 调了好长时间啊 这道题设时间为time,那么对于m个武器从S向这m个点连容量为time*Bi的 ...
- HTML5射击类游戏----【地球保卫战】
在线DEMO地址:打开: 游戏截图: 就不贴代码了, 因为代码太多了, 大概写一下这个游戏实现思路和一些实现: 游戏一共有三关, 每一关都有一个大Boss, Boss比较好杀,主要各种外星飞 ...
- cocoapods安装
什么是cocoapods CocoaPods是一个用来帮助我们管理第三方依赖库的工具.它可以解决库与库之间的依赖关系,下载库的源代码,同时通过创建一个Xcode的workspace来将这些第三方库和我 ...
- maven引入多个spring jar包中存在同名文件的问题
项目打包后执行报错:Configuration problem: Unable to locate Spring NamespaceHandler for XML schema namespaceht ...
- bootstrap在jsp中的应用
http://www.bootcss.com/ 下载bootstrap的js,css文件: 在jsp中的应用: 第一步: 新建一个web项目 将bootstrap下载下来的文件放入WEB-INF下面的 ...
- Html中自定义鼠标的形状
Html中自定义鼠标的形状 <html> <head> <title>自定义的鼠标形状</title> <meta http-equiv=&quo ...
- SharePoint Claim base authentication EnsureUser 不带claim(i:0#.w|)user Failed
环境信息: 带有Form base authentication(FBA).Active Directory Federation Services(ADFS).以及windows Authentic ...
- FORM
一 .新增的input输入属性 1.email类型 在表单提交E-mail地址时,无效的输入会生成很多无效数据,对后期的数据检索造成一定的影响.所以在表单提交之前,需要对输入的E-mail地址进行有效 ...
- 将JAR包反编译,修改后重新打包(转)
将JAR包反编译,修改后重新打包(转) 在学习和开发JAVA项目中,我们经常会用到第三方提供的一些jar.使用这些第三方工具包,可以提高我们开发的效率,缩短开发的时间.有的第三方工具,提供具体的 ...