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用例。