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只能运 ...
随机推荐
- 这些年正Android - 身在他乡
“从不敢想到想去做到,做到我想的,事实证明我并不像他们想象的那样脆弱.我只是需要一盏灯,一架钢琴,一支麦克风.曾经想象过做一名医生救死扶伤,也曾想过做律师,做记者,做奥运冠军,但是都没有结果.因为我最 ...
- webApi 导入Excel
/// <summary> /// 导入 /// </summary> /// <param name="organizationId">< ...
- MYSQL性能优化分享(分库分表)
1.分库分表 很明显,一个主表(也就是很重要的表,例如用户表)无限制的增长势必严重影响性能,分库与分表是一个很不错的解决途径,也就是性能优化途径,现在的案例是我们有一个1000多万条记录的用户表mem ...
- autolayout
autolayout.因为之前都是用frame,用代码来做,并且在布局时也很少用storyboard和xib.使得我再这方便经验很欠缺,想用,但是又怕用不好,出现各种意想不到的bug.但是又忽然想到, ...
- sql server2008安装说明 详细完整版
SQL Server 2008是一个重大的产品版本,它推出了许多新的特性和关键的改进,使得它成为至今为止的最强大和最全面的SQL Server版本. 在现今数据的世界里,公司要获得成功和不断发展,他们 ...
- Android SDK的安装与环境变量配置
配置Andriod环境变量前提是要先安装好JAVA环境 1.下载Android SDK,点击安装,直接默认路径即可! 下载地址:http://developer.android.com/sdk/ind ...
- 如何在java中拟合正态分布
前言 最近在工作中需要拟合高斯曲线,在python中可以使用 scipy,相关代码如下: #!/usr/bin/env python # -*- coding=utf-8 -*- %matplotli ...
- 关于Linux:chmod和chown知识
将vendor下的root权限递归更改为user命令:sudo chown -R user:user fujitsu 指令名称 : chmod 使用权限 : 所有使用者 使用方式 : chmod [- ...
- git 忽视大小写
git config core.ignorecase false http://stackoverflow.com/questions/3011625/git-mv-and-only-change-c ...
- Unity 几种优化建议
转: http://user.qzone.qq.com/289422269/blog/1453815561?ptlang=2052 Unity 几种优化建议 最简单的优化建议: 1.PC平台的话保持场 ...