UiAutomator2.0入门
总是听说UiAutomator这个框架,但从来没有使用过。找了篇入门,实践一下。实践之后感觉,uiautomator写测试代码,还是有点费劲。接口名比较多,比较长。网易的atx里使用的uiautomator相当于原生uiautomator的python wrapper。接口就显得简洁很多。
1. 新建项目
使用推荐的android studio创建android空工程。File->New -> New project,在application name处填上工程名,点击 Next, 在target android devices页面,选择 支持的phone and tablet的最小sdk。这个可根据自己设备上的android版本选择。点出next,先把add no activity,完成了一个空工程的新建。
2. 配置
在Module:app里的build.gradle,新增绿色的内容。
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
androidTestCompile('com.android.support.test.espresso:espresso-core:2.0', {
exclude group: 'com.android.support', module: 'support-annotations'
})
compile 'com.android.support:appcompat-v7:24.+'
testCompile 'junit:junit:4.12'
androidTestCompile 'com.android.support.test.uiautomator:uiautomator-v18:2.1.1'
}
我使用的gradle版本为2.14.1,相应的android plugin version是2.1.3。之前配置不正确,总是提示gradle sync不成功,然后测试代码就不能正常运行。另外,如果dependencies里的插件配置有重复,没有下载到本地的,还需要处理好。将项目设置为Android Tests, 可看到目录结构为下图所示。与test相关的文件背景变为绿色。

3. case代码
case代码写在androidTest目录下,示例内容如下:
@RunWith(AndroidJUnit4.class)
public class ExampleInstrumentedTest {
private UiDevice mUIDevice = null;
private Context mContext = null;
String APP = "XXX"; @Before
public void setUp() throws RemoteException{
mUIDevice = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation()); //获得device对象
mContext = InstrumentationRegistry.getContext(); if(!mUIDevice.isScreenOn()){ //唤醒屏幕
mUIDevice.wakeUp();
}
mUIDevice.pressHome(); //按home键
} @Test
public void test1(){
Intent myIntent = mContext.getPackageManager().getLaunchIntentForPackage(APP); //启动app
mContext.startActivity(myIntent);
mUIDevice.waitForWindowUpdate(APP, * );
UiObject sender = mUIDevice.findObject(new UiSelector().text("Send")); //定位text内容为Send的控键
try {
sender.click(); //点击按键
}catch (Exception e){
e.printStackTrace();
}
assertTrue(true); //断言,随便乱写的,此处未起到断言作用
} }
连接设备,点击运行测试代码后,可看到运行的日志如下。
$ adb push D:\AndroidBook\m_adr_atom_hotel\HelloUI2\app\build\outputs\apk\app-debug.apk /data/local/tmp/com.example.mandasun.helloui2
$ adb shell pm install -r "/data/local/tmp/com.example.mandasun.helloui2"
pkg: /data/local/tmp/com.example.mandasun.helloui2
Success $ adb push D:\AndroidBook\m_adr_atom_hotel\HelloUI2\app\build\outputs\apk\app-debug-androidTest-unaligned.apk /data/local/tmp/com.example.mandasun.helloui2.test
$ adb shell pm install -r "/data/local/tmp/com.example.mandasun.helloui2.test"
pkg: /data/local/tmp/com.example.mandasun.helloui2.test
Success Running tests $ adb shell am instrument -w -r -e package com.example.mandasun.helloui2 -e debug false com.example.mandasun.helloui2.test/android.support.test.runner.AndroidJUnitRunner
Client not ready yet..
Started running tests
Tests ran to completion.
从日志中可以看到,uiautomator将测试代码打成的app-debug.apk包和app-debug-androidTest-unaligned.apk推到adr机上,然后安装。之后,使用命令运行了这两个 apk。
本文参考文档:uiautomator 2.0 demo与使用 uiautomator使用入门官方教程 android测试之UI自动化测试工具Uiautomator介绍
UiAutomator2.0入门的更多相关文章
- ASP.NET Core 1.0 入门——了解一个空项目
var appInsights=window.appInsights||function(config){ function r(config){t[config]=function(){var i= ...
- ASP.NET Core 1.0 入门——Application Startup
var appInsights=window.appInsights||function(config){ function r(config){t[config]=function(){var i= ...
- Omnet++ 4.0 入门实例教程
http://blog.sina.com.cn/s/blog_8a2bb17d01018npf.html 在网上找到的一个讲解omnet++的实例, 是4.0下面实现的. 我在4.2上试了试,可以用. ...
- 《VC++ 6简明教程》即VC++ 6.0入门精讲 学习进度及笔记
VC++6.0入门→精讲 2013.06.09,目前,每一章的“自测题”和“小结”三个板块还没有看(备注:第一章的“实验”已经看完). 2013.06.16 第三章的“实验”.“自测题”.“小结”和“ ...
- spring web flow 2.0入门(转)
Spring Web Flow 2.0 入门 一.Spring Web Flow 入门demo(一)简单页面跳转 附源码(转) 二.Spring Web Flow 入门demo(二)与业务结合 附源码 ...
- Json.Net6.0入门学习试水篇
原文:Json.Net6.0入门学习试水篇 前言 JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式.简单地说,JSON 可以将 JavaScript 对象中 ...
- SignalR 2.0 入门与提高
SignalR 2.0 入门与提高 SignalR 2.0 最近整理了SignalR2.0 部分知识点,原文翻译,由于自己是土鳖,翻译得不好的地方,欢迎指正!仅供各位初学者学习! 第一节. 入门ASP ...
- ASP.NET SignalR 2.0入门指南
ASP.NET SignalR 2.0入门指南 介绍SignalR ASP.NET SignalR 是一个为 ASP.NET 开发人员的库,简化了将实时 web 功能添加到应用程序的过程.实时Web功 ...
- Bootstrap3.0入门学习系列
Bootstrap3.0入门学习系列规划[持续更新] 前言 首先在此多谢博友们在前几篇博文当中给与的支持和鼓励,以及在回复中提出的问题.意见和看法. 在此先声明一下,之前在下小菜所有的随笔文章中, ...
随机推荐
- IHttpModule不起作用的两个原因
最近在将系统改造成微服务的过程中,需要对以前的Url请求做兼容性处理,于是就采用了HttpModules模型,但在测试中碰到IHttpModules模块不起作用. 一.IIS配置问题 我用的IIS7. ...
- python中的-1
-1单个使用时表示最后一个: >>> [1,2,3,4][-1] 4 表示范围(区间)时,因为是开区间表示方法,如[0:2]是不包括2的所以 [0:-1]只能访问到倒数第二个(不包括 ...
- HDU-6356 Glad You Came (线段树)
题目链接:Glad You Came 题意:数组有n个数初始为0,m个询问,每个询问给出L R V(按照给定函数生成),将数组的下标L到R的数与V取较大值,最后输出给定的公式结果. 题意:哇~打比赛的 ...
- Linux常用rmp包网址
* HA:http://download.opensuse.org/repositories/network:/ha-clustering:/Stable/ * Openstack:https://r ...
- WPF DataGrid列设置为TextBox控件的相关绑定
在wpf的DataGrid控件中,某一列的数据模板为TextBox控件的话,绑定Text="{Binding TxtSn, UpdateSourceTrigger=PropertyChang ...
- [Direct2D开发] 绘制网格
转载请注明出处:http://www.cnblogs.com/Ray1024 一.引言 最近在使用Direct2D进行绘制工作中,需要实现使用Direct2D绘制网格的功能.在网上查了很多资料,终于实 ...
- Unity游戏AI记录(2d横板为例)
using System.Collections;using System.Collections.Generic;using UnityEngine; public class GeneralPeo ...
- 【文章存档】如何通过 GitLab 进行持续部署
好久没写博客了,这几天存档一下新文章. 链接 https://docs.azure.cn/zh-cn/articles/azure-operations-guide/app-service-web/a ...
- SQL手工注入漏洞测试(Sql Server数据库)
还是先找到注入点,然后order by找出字段数:4 通过SQL语句中and 1=2 union select 1,2,3……,n联合查询,判断显示的是哪些字段,就是原本显示标题和内容时候的查询字段. ...
- nginx反向代理tomcat应用,struts2网站程序redirect时导致请求地址错误的解决方法
一个使用struts2的网站在登录页面需要进行redirect跳转,大致如下: <package name="admin" extends="httl-defaul ...