UiAutomator自动化测试框架介绍
UiAutomator自动化测试框架介绍
环境搭建
1 必要条件
1.1 JDK
1.2 SDK(API高于15)
1.3 Eclipse
2 简要步骤
2.1 安装JDK并添加bin目录到环境变量
2.2 添加SDK中的tools目录和platform-tools到环境变量
2.3 安装Eclipse并安装ADT插件
建立工程
1 用eclipse建立Java project
2 添加Junit库
3 添加Android库
找到sdk/platforms/android-19/下面的android.jar和uiautomator.jar并添加进来
4 添加完成后如下图
在工程中编写case
1 分析Device当前界面的UI内容
在编写Case之前,需要找到界面上不同控件的id,text,class或者description等等,来定位具体的点击事件。我们可以使用uiautomatorviewer工具来分析界面的组织架构。
2 官方用例
package com.uia.example.my;
// Import the uiautomator libraries
import com.android.uiautomator.core.UiObject;
import com.android.uiautomator.core.UiObjectNotFoundException;
import com.android.uiautomator.core.UiScrollable;
import com.android.uiautomator.core.UiSelector;
import com.android.uiautomator.testrunner.UiAutomatorTestCase;
public class LaunchSettings extends UiAutomatorTestCase {
public void testDemo() throws UiObjectNotFoundException {
// Simulate a short press on the HOME button.
getUiDevice().pressHome();
// We’re now in the home screen. Next, we want to simulate
// a user bringing up the All Apps screen.
// If you use the uiautomatorviewer tool to capture a snapshot
// of the Home screen, notice that the All Apps button’s
// content-description property has the value “Apps”.
We can
// use this property to create a UiSelector to find the
button.
UiObject allAppsButton = new UiObject(new UiSelector()
.description("Apps"));
// Simulate a click to bring up the All Apps screen.
allAppsButton.clickAndWaitForNewWindow();
// In the All Apps screen, the Settings app is located in
// the Apps tab. To simulate the user bringing up the Apps
tab,
// we create a UiSelector to find a tab with the text
// label “Apps”.
UiObject appsTab = new UiObject(new UiSelector()
.text("Apps"));
// Simulate a click to enter the Apps tab.
appsTab.click();
// Next, in the apps tabs, we can simulate a user swiping
until
// they come to the Settings app icon. Since the
container view
// is scrollable, we can use a UiScrollable object.
UiScrollable appViews = new UiScrollable(new UiSelector()
.scrollable(true));
// Set the swiping mode to horizontal (the default is
vertical)
appViews.setAsHorizontalList();
// Create a UiSelector to find the Settings app and simulate
// a user click to launch the app.
UiObject settingsApp = appViews.getChildByText(new
UiSelector()
.className(android.widget.TextView.class.getName()),
"Settings");
settingsApp.clickAndWaitForNewWindow();
// Validate that the package name is the expected one
UiObject settingsValidation = new UiObject(new
UiSelector()
.packageName("com.android.settings"));
assertTrue("Unable to detect Settings",
settingsValidation.exists());
}
}
Build工程
1
产生build工程所需要的配置文件
<android-sdk>/tools/android create
uitest-project -n <name> -t 1 -p <path>
name:
build jar文件的名字
1: 使用命令android list可以查看
path:
工程目录
2
build工程jar文件
2.1配置文件创建完成后,刷新工程目录,将会产生三个文件:build.xml、local.properties和project.properties
2.2 打开build.xml,将default值改成build,如下图
2.3 build工程
运行工程
1
导入
将build好的jar文件,push到手机目录/data/local/tmp/下
2
运行命令
adb shell uiautomator runtest
LaunchSettings.jar(build出来的文件) -c com.uia.example.my.LaunchSettings(class名)
如果想要运行class类里具体的方法,可以在类后面加上#方法名即可
如果想手机单独运行程序,不受数据线的约束,可以使用--nohup
API详解
1
UiDevice
提供关于设备的状态信息。你也可以使用这个类来模拟设备上的用户的行为,如按方向键或者按菜单键
如若获取UiDevice的对象,请使用UiDevice.getInstance()方法
Public Methods |
||
void |
清除上一次输入的文本 |
|
Clears the text |
||
boolean |
点击坐标 |
|
Perform a click |
||
boolean |
从开始坐标拖拽到终点坐标位置 |
|
Performs a swipe |
||
void |
dump当前界面的层次结构,并保存到/data/local/tmp/目录下 |
|
Helper method |
||
void |
禁用传感器,并且在当前界面冻结转屏 |
|
Disables the |
||
String |
获取当前Activity的名字,此方法已被丢弃,不建议使用 |
|
This method is |
||
String |
获取当前包名 |
|
Retrieves the |
||
int |
获取显示高度,以像素为单位 |
|
Gets the height |
||
int |
返回当前的旋转值,0,90,180,270 |
|
Returns the current rotation of the display, as defined |
||
Point |
|
|
Returns the |
||
int |
获取显示宽度,以像素为单位 |
|
Gets the width |
||
static UiDevice |
Java单例模式,获取UiDevice的对象 |
|
Retrieves a |
||
String |
获取最后一次输入的text |
|
Retrieves the |
||
String |
获取设备的名字 |
|
Retrieves the |
||
boolean |
检查是否有监听器触发过 |
|
boolean |
检查某个特定的监听器是否触发过 |
|
boolean |
检查设备是否是正常的竖屏状态 |
|
Check if the |
||
boolean |
检查屏幕是否亮起 |
|
Checks the power |
||
boolean |
打开notification通知栏 |
|
Opens the |
||
boolean |
打开quicksetting |
|
Opens the Quick |
||
boolean |
按返回键 |
|
Simulates a |
||
boolean |
轨迹球 |
|
Simulates a |
||
boolean |
轨迹球 |
|
Simulates a |
||
boolean |
轨迹球 |
|
Simulates a |
||
boolean |
轨迹球 |
|
Simulates a |
||
boolean |
轨迹球 |
|
Simulates a |
||
boolean |
删除键 |
|
Simulates a |
||
boolean |
回车键 |
|
Simulates a |
||
boolean |
home键 |
|
Simulates a |
||
boolean |
keycode,参照keyevent |
|
Simulates a |
||
boolean |
keycode,参照keyevent |
|
Simulates a |
||
boolean |
menu键 |
|
Simulates a |
||
boolean |
recent键 |
|
Simulates a |
||
boolean |
search键 |
|
Simulates a |
||
void |
registerWatcher(String |
注册一个监听器,当前指定动作被打断的时候,处理中断异常 |
Registers |
||
void |
移除之前注册的监听器 |
|
void |
重置一个监听器 |
|
void |
强制运行所有的监听器 |
|
This method |
||
void |
应用或禁用布局层次压缩 |
|
Enables or |
||
void |
通过禁用传感器,然后模拟设备向左转,并且固定位置 |
|
Simulates |
||
void |
通过禁用传感器,然后模拟设备转到其自然默认的方向,并且固定位置 |
|
Simulates |
||
void |
通过禁用传感器,然后模拟设备向右转,并且固定位置 |
|
Simulates |
||
void |
锁屏 |
|
This method |
||
boolean |
在点阵列中滑动,5秒一步 |
|
Performs a swipe |
||
boolean |
swipe(int startX, int startY, int endX, int endY, int |
通过坐标滑动屏幕 |
Performs a swipe |
||
boolean |
当前窗口截屏,并将其存储为PNG格式默认1.0f的规模(原尺寸)和90%的图片质量,参数为file类的文件路径 |
|
Take a |
||
boolean |
当前窗口截屏,可以自定义尺寸和图片质量 |
|
Take a screenshot |
||
void |
重新启用传感器,并且允许旋转屏幕 |
|
Re-enables the |
||
void |
等待当前应用程序处于空闲状态 |
|
Waits for the |
||
void |
等待当前应用程序处于空闲状态 |
|
Waits for the |
||
boolean |
等待窗口内容更新时间的发生 |
|
Waits for a |
||
void |
唤醒屏幕 |
|
This method |
2
UiSelector
用于按照一定的条件,定位界面上的控件元素。UiObject对象通过UiSelector来选取
Public Constructors |
||
|
构造方法 |
|
Public Methods |
||
UiSelector |
是否是可选择的属性 |
|
Set the search criteria to match widgets that are |
||
UiSelector |
是否已经被选择 |
|
Set the search criteria to match widgets that are |
||
UiSelector |
childSelector(UiSelector selector) |
子类 |
Adds a child UiSelector criteria to this selector. |
||
UiSelector |
类名 |
|
Set the search criteria to match the class property for |
||
<T> UiSelector |
泛型类名 |
|
Set the search criteria to match the class property for |
||
UiSelector |
类名正则表达式 |
|
Set the search criteria to match the class property for |
||
UiSelector |
是否可以被点击 |
|
Set the search criteria to match widgets that are |
||
UiSelector |
通过content-description来选取 |
|
Set the search criteria to match the |
||
UiSelector |
通过content-description所包含的字符来选取 |
|
Set the search criteria to match the |
||
UiSelector |
通过content-description所符合正则表达式 的字符来选取 |
|
Set the search criteria to match the |
||
UiSelector |
通过content-description开始字符匹配来选取 |
|
Set the search criteria to match the |
||
UiSelector |
是否已经被enabled |
|
Set the search criteria to match widgets that are |
||
UiSelector |
焦点属性 |
|
Set the search criteria to match widgets that are |
||
UiSelector |
当前焦点属性 |
|
Set the search criteria to match widgets that have |
||
UiSelector |
fromParent(UiSelector selector) |
父类 |
Adds a child UiSelector criteria to this selector which |
||
UiSelector |
编号 |
|
Set the search criteria to match the widget by its node |
||
UiSelector |
索引 |
|
Set the search criteria to match the widget by its |
||
UiSelector |
是否可以被长按 |
|
Set the search criteria to match widgets that are |
||
UiSelector |
包名 |
|
Set the search criteria to match the package name of |
||
UiSelector |
正则包名 |
|
Set the search criteria to match the package name of |
||
UiSelector |
资源id |
|
Set the search criteria to match the given resource ID. |
||
UiSelector |
资源id正则表达式 |
|
Set the search criteria to match the resource ID of the |
||
UiSelector |
是否可以被滑动 |
|
Set the search criteria to match widgets that are |
||
UiSelector |
当前选择属性 |
|
Set the search criteria to match widgets that are |
||
UiSelector |
根据text来选取控件或者应用程序 |
|
Set the search criteria to match the visible text |
||
UiSelector |
根据text中包含的字符串 来选取控件或者应用程序 |
|
Set the search criteria to match the visible text in a |
||
UiSelector |
通过正则表达式来选取匹配的控件或者应用程序 |
|
Set the search criteria to match the visible text |
||
UiSelector |
通过文本开始字符来选取 |
|
Set the search criteria to match visible text in a |
||
String |
|
|
Protected Methods |
||
UiSelector |
克隆UiSelector |
3
UiObject
UiObject主要对对象进行操作
Public Constructors |
||
|
UiObject(UiSelector selector) |
UiObject的构造方法 |
Constructs a UiObject to represent a |
||
Public Methods |
||
void |
清除编辑框中的文本 |
|
Clears the existing text contents in an |
||
boolean |
点击事件 |
|
Performs a click at the center of the |
||
boolean |
点击对象,等待新窗口的出现, 参数为等待超时时长 |
|
Performs a click at the center of the |
||
boolean |
点击对象,等待新窗口的出现 |
|
Waits for window transitions that would |
||
boolean |
点击对象的右下角 |
|
Clicks the bottom and right corner of |
||
boolean |
点击对象的左上角 |
|
Clicks the top and left corner of the UI |
||
boolean |
dragTo(UiObject destObj, int steps) |
拖拽到另一个对象处,步长可调整拖拽的速度 |
Drags this object to a destination |
||
boolean |
拖拽到目标坐标处,步长可调整拖拽的速度 |
|
Drags this object to arbitrary coordinates. |
||
boolean |
对象是否存在 |
|
Check if view exists. |
||
Rect |
获取对象的矩形坐标左上角和右下角的坐标 |
|
Returns the |
||
UiObject |
getChild(UiSelector selector) |
获得对象的子类对象,可以递归获取 子孙当中的某个对象 |
Creates a new UiObject for a child view |
||
int |
获取下一级子类的数量 |
|
Counts the child views immediately under |
||
String |
获取类名 |
|
Retrieves |
||
String |
获取description |
|
Reads |
||
UiObject |
getFromParent(UiSelector selector) |
从父类获取子类,按照UiSelector获取兄弟类 |
Creates a new UiObject for a sibling |
||
String |
获取包名 |
|
Reads the |
||
获得selector用于调试 |
||
Debugging helper. |
||
String |
获得对象的文本属性 |
|
Reads the text property of the |
||
Rect |
返回可见视图的范围,如果视图是可见的, 只有可见部分报告的范围 |
|
Returns the visible bounds of the view. |
||
boolean |
检查对象的checkable属性是否为true |
|
Checks if the UI |
||
boolean |
检查对象的checked属性是否为true |
|
Check if the UI |
||
boolean |
检查对象的clickable属性是否为true |
|
Checks if the UI |
||
boolean |
检查对象的enabled属性是否为true |
|
Checks if the UI |
||
boolean |
检查对象的focusable属性是否为true |
|
Check if the UI |
||
boolean |
检查对象的focused属性是否为true |
|
Check if the UI |
||
boolean |
检查对象的longclickable属性是否为true |
|
Check if the |
||
boolean |
检查对象的scrollable属性是否为true |
|
Check if the |
||
boolean |
检查对象的selected属性是否为true |
|
Checks if the UI |
||
boolean |
长按 |
|
Long clicks the center of the visible |
||
boolean |
长按对象的右下角 |
|
Long clicks bottom and right corner of |
||
boolean |
长按对象的左上角 |
|
Long clicks on the top and left corner |
||
boolean |
执行单手触控手势,可定义任意手势和形状 |
|
Performs a multi-touch gesture. |
||
boolean |
执行任意的双手触控手势 |
|
Generates a two-pointer gesture with |
||
boolean |
手势操作,两点向内收缩 |
|
Performs a two-pointer gesture, where |
||
boolean |
手势操作,两点向外张开 |
|
Performs a two-pointer gesture, where |
||
boolean |
在对象中输入文字 |
|
Sets the text in an editable field, |
||
boolean |
向下滑动 |
|
Performs the swipe down action on the |
||
boolean |
向左滑动 |
|
Performs the swipe left action on the |
||
boolean |
向右滑动 |
|
Performs the swipe right action on the |
||
boolean |
向上滑动 |
|
Performs the swipe up action on the |
||
boolean |
等待对象出现 |
|
Waits a specified length of time for a |
||
boolean |
等待对象消失 |
|
Waits a specified length of time for a |
4
UiScrollable
Public Constructors |
||
|
UiScrollable(UiSelector container) |
UiScrollable的构造方法 |
Constructor. |
||
Public Methods |
||
boolean |
以步长为5的值向后滑动 |
|
Performs a backwards fling action with |
||
boolean |
以步长为5的值向前滑动 |
|
Performs a forward fling with the |
||
boolean |
滑动到最前面一屏 |
|
Performs a fling gesture to reach the |
||
boolean |
滑动到最后一屏 |
|
Performs a fling gesture to reach the |
||
getChildByDescription(UiSelector childPattern, |
是否允许滑动来查找具备描述条件的object对象 |
|
Searches for a child element in the |
||
getChildByDescription(UiSelector childPattern, |
默认滑动来查找具备描述条件的对象 |
|
Searches for a child element in the |
||
getChildByInstance(UiSelector childPattern, |
获取具备UiSelector的子集,再从子集当中筛选想要的元素(不滚动) |
|
Searches for a child element in the |
||
getChildByText(UiSelector childPattern, |
是否允许滑动来查找具备通过文本条件的object对象 |
|
Searches for a child element in the |
||
getChildByText(UiSelector childPattern, |
默认通过滑动来查找具备通过文本条件的object对象 |
|
Searches for a child element in the |
||
int |
获取搜索滑动过程中的最大滑动次数,默认常量为30 |
|
Gets the maximum number of scrolls |
||
double |
默认常量为0.1,10% |
|
Returns the percentage of a widget's |
||
boolean |
自定义步长向后滑动 |
|
Performs a backward scroll. |
||
boolean |
以默认步长55向后滑动 |
|
Performs a backward scroll with the |
||
boolean |
滚动到描述所在的位置 |
|
Performs a forward scroll action on the |
||
boolean |
以默认步长55向前滑动 |
|
Performs a forward scroll with the |
||
boolean |
自定义步长向前滑动 |
|
Performs a forward scroll. |
||
boolean |
scrollIntoView(UiSelector selector) |
滚动到元素所在的位置 |
Perform a scroll forward action to move |
||
boolean |
scrollIntoView(UiObject obj) |
滚动到对象所在的位置 |
boolean |
滚动到文本对象所在的位置 |
|
Performs a forward scroll action on the |
||
boolean |
自定义扫动距离滚动到开始位置 |
|
Scrolls to the beginning of a scrollable |
||
boolean |
自定义扫动距离以及步长滚动到开始位置 |
|
Scrolls to the beginning of a scrollable |
||
boolean |
自定义扫动距离以及步长滚动到结束位置 |
|
Scrolls to the end of a scrollable |
||
boolean |
自定义扫动距离滚动到结束位置 |
|
Scrolls to the end of a scrollable |
||
UiScrollable |
设置界面为水平滑动 |
|
Set the direction of swipes to be |
||
UiScrollable |
设置界面为竖直滑动,此为默认值 |
|
Set the direction of swipes to be |
||
UiScrollable |
设置最大可扫描次数 |
|
Sets the maximum number of scrolls |
||
UiScrollable |
设置一个部件的大小,在滑动时,视为无接触区的百分比 |
|
Sets the percentage of a widget's size |
5
UiCollection
UiCollection继承UiObject,用于枚举一个容器的用户界面(UI)元素计数的目的,或安装子元素的文本或描述条件获取子元素对象。
Public Constructors |
||
|
UiCollection(UiSelector selector) |
UiCollection的构造方法 |
Public Methods |
||
getChildByDescription(UiSelector childPattern, String text) |
通过包含的条件寻找符合的子元素 |
|
Searches for child UI element within the constraints of |
||
getChildByInstance(UiSelector childPattern, int instance) |
通过包含的条件寻找符合的子元素 |
|
Searches for child UI element within the constraints of |
||
getChildByText(UiSelector childPattern, String text) |
通过包含的条件寻找符合的子元素 |
|
Searches for child UI element within the constraints of |
||
int |
getChildCount(UiSelector childPattern) |
递归计算符合条件的子元素的数量 |
Counts child UI element instances matching |
6 UiWatcher
Public Methods |
||
abstract boolean |
抽象方法,在监听程序中要实现此方法 |
|
监听器要在中断代码之前运行,我们首先要注册一个监听器,然后运行。注册监听器的方法,在UiDevice的API中有介绍,
void |
registerWatcher(String name, UiWatcher watcher) |
注册一个监听器,当前指定动作被打断的 时候,处理中断异常 |
Registers a UiWatcher to run automatically when the testing framework is unable to find a match using a UiSelector. |
注册监听器的结构方法如下:
UiDevice.getInstance().registerWatcher(“register watcher method”, new UiWatcher(){
Public boolean checkForCondition{
//在此方法体内定义监听的方法
}
});
编写用例的注意事项
1.每一个类都要继承UiAutomatorTestCase
2.最好在每个类里都定义setUp和tearDown方法,用于初始化(最好都以Home界面为测试开始点)和结束测试(最好也以Home界面为测试结束点)
3.因为此套测试框架式继承与Junit3的,所以每个方法最好都要以test开头,运行单个类时,程序会run以test开头的方法。当然这不是强制的,也可以以#方法run用例。
UiAutomator自动化测试框架介绍的更多相关文章
- Selenium自动化测试框架介绍
Selenium自动化测试框架介绍 1.测试架构作用 a.可维护性 b.提高编写脚本效率 c.提高脚本的可读性 2.框架的几大要素: Driver管理,脚本,数据,元素对象,LOG,报告,运行机制,失 ...
- 『与善仁』Appium基础 — 8、Appium自动化测试框架介绍
目录 1.主流的移动端自动化测试框架 (1)Robotium (2)Macaca (3)Appium 2.自动化测试工具的选择 3.Appium简介 提示:我们前面说的Android环境搭建和adb命 ...
- 快学UiAutomator各种框架介绍
Monkey 编写语言:命令行 运行环境:使用adb连接PC运行测试对象:Android平台自动化测试的一种手段,通过Monkey程序模拟用户触摸屏幕.滑动Trackball.按键等操作来对设备上的程 ...
- 『心善渊』Selenium3.0基础 — 1、Selenium自动化测试框架介绍
目录 1.Selenium介绍 2.Selenium的特点 3.Selenium版本说明 4.拓展:WebDriver与Selenium RC的区别 5.Webdriver工作原理 1.Seleniu ...
- Python Nose 自动化测试框架介绍
文章目录 1. unittest 简介 1.1 python 单元测试 1.2 unittest 测试框架 1.3 默认模式 1.4 手工模式 2. nose 扩展框架 2.1 `nose` 的安装和 ...
- STAF自动化测试框架
STAF自动化测试框架介绍 http://baike.baidu.com/link?url=9oPZN3JntRakidI7xizqCbyGRISMvCKGfXHBB_WH7OAkKjAKZjq88q ...
- 接口自动化测试框架 -- reudom
reudom Automated testing framework based on requests and unittest interface. 基于 Unittest 和 Requests ...
- RobotFramework自动化测试框架-移动手机自动化测试AppiumLibrary介绍
在使用AppiumLibrary库时,需要预先安装好Appium自动化工具,Appium官网地址为:http://appium.io/ Appium的GitHub地址为:https://github. ...
- Android自动化测试框架UIAutomator原理浅析
UIAutomator是一个Android自动化测试框架,是谷歌在Android4.1版本发布时推出的一款用Java编写的UI测试框架,它只能用于UI即黑盒方面的测试.所以UIAutomator只能运 ...
随机推荐
- java BigDecimal add 等方法遇到的问题
//这篇随笔是为了提醒自己避免重复错误 //原先的代码是这样的,想着输出会是9.00,可是结果却是0.00 BigDecimal day_fee = new BigDecimal("0.00 ...
- jQuery on 绑定的事件触发多次
jquery用on绑定事件,在代码执行过程中,可能会遇到多次执行的情况. 解决方案是在on的事件前面加上一个off,再on. $('#btnBind').off('click').on('click' ...
- equals标准写法
@Overridepublic boolean equals(Object obj) { //为了提高效率 if(this == obj){ return true; } //为了提供程序的健壮性 / ...
- StrangeIoc框架学习----在项目中实战
最近,因为公司的项目一直在研究StrangeIoc框架,小有所得,略作记录. StrangeIoc是一款基于MVCS的一种框架,是对MVC思想的扩展,是专门针对unity开发的一款框架,非常好用. 一 ...
- 随便翻翻qcon 2014
随便翻了翻QCon 2014的ppt,土人看看最新的成果: 基本感受: 1. 高大上公司比如阿里,腾讯啥的已经挺过第一轮,已经重构重构再重构了,整个架构不能说成熟,但是可以说可用了 2. 创业公司或者 ...
- Java三大框架的配置
1. 首先是spring,右键项目-myeclipse-capabilitise-install spring etc.类似的就好,生成applicationContext.xml和spring一些类 ...
- $(document).ready()方法和window.onload区别
事件: javascript 和 HTML之间的交互式通过用户和浏览器操作页面时引发的事件来处理的.当文档或者它的某些元素发生某些变化和操作时,浏览器会自动生成一个事件:例如:当用户单击某个按钮时,也 ...
- js关于页面坐标api
网页可见区域宽: document.body.clientWidth;网页可见区域高: document.body.clientHeight;网页可见区域宽: document.body.offset ...
- 实现iframe 全屏显示
componentDidMount(){var elem = document.getElementById('iframe');; var elem = document.getElementByI ...
- VUE 入门基础(1)
一,安装 Vue.js 不支持 IE8 及其以下版本,因为 Vue.js 使用了 IE8 不能模拟的 ECMAScript 5 特性. Vue.js 支持所有兼容 ECMAScript 5 的浏览器. ...