用uiautomator做安卓的app端的UI testing的环境搭建及编jar包和运行case的步骤如下:

1、新建java工程

2、右键properties,

添加junit4的library,并添加android.jar和uiautomator的jar包依赖

3、在工程下新建package,之后新建类,代码如下:

(备注:extends的类需要写清楚是UiAutomatorTestCase ,具体的方法需要以test开头,并且throws UiObjectNotFoundException )

 package com.ganji.loginhelper;

 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 Runner 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()); 

   UiObject reportBug = new UiObject(new UiSelector().text("Sound"));

   reportBug.clickAndWaitForNewWindow();

   UiObject soundValidation = new UiObject(new UiSelector()

   .text("Volumes"));

    assertTrue("Unable to detect Sound",

        soundValidation.exists()); 

   getUiDevice().pressHome();

   }  

 }

4、之后编译和发布uiautomator测试:

创建一个编译文件:

C:\Users\58>android create uitest-project -n autoLogin -t 15 -p E:\Debin2\autoLogin

Added file E:\Debin2\autoLogin\build.xml

以上是创建成功了,叫做build.xml

这个命令是:

Android create uitest-project -n xxx -t xxx -p xxx

其中-n 后面的xxx代表要project的name

-t 的15是这样获取的:通过android list,能够得到一个列表,因为我用的android.jar和uiautomator.jar是19的,所以查找到对应的是:

所以我的-t 就写成了15

然后就是-p 后面的参数,是工程所在的位置,因为这个build.xml的生成目录就是这个工程目录

命令行创建提示创建成功之后,即added file xxx这句话出现之后,在工程目录下能够看到这个文件,通过eclipse目录刷新也可以看到:(多了三个文件)

之后如果eclipse中直接有ant的这个打包设置,就可以通过在build.xml上右键Run As选择ant build,然而我的eclipse中没有配置成功,但是没关系,只要在命令行里面能运行成功就可以。

build.xml的文件打开后,能看到default后面本来是"help"的,改成build

然后我的机器中没有安装ant,去官网下载ant的文件,之后安装成功,需要进行环境变量的配置,主要包含三个地方:

ANT_HOME  D:\Program Files\apache-ant-1.9.7-bin\apache-ant-1.9.7

path  D:\Program Files\apache-ant-1.9.7-bin\apache-ant-1.9.7\bin

classpath D:\Program Files\apache-ant-1.9.7-bin\apache-ant-1.9.7\lib

然后在cmd下直接运行ant 和ant -version,如果提示正确,不是出现ant不是可用的命令这句话,就说明环境变量配置成功了

接下来是比较容易出现问题的地方:

进入到工程目录下,

命令行输入:ant + build.xml

应该是没有后面的后缀名,直接ant build

然后能看到已经build成功了,并且在工程目录的bin文件夹下,文件名叫做:autoLogin.jar

5、接下来就是将jar包push到手机中,然后运行程序了

push进入的手机的目录是/data/local/tmp,

执行case的命令是:adb shell uiautomator runtest autoLogin.jar -c com.ganji.loginhelper.Runner

runtest后面是之前push进去的jar包,-c后面是要执行的class(class前面要加包名的,即xx.xx.package.classname)

然后就会有跑case之后的信息打出来,完成,后面就是怎么写case实现自己想要的测试效果啦~~~

针对部分元素需要通过参数直接传递进去进行设置的需求,可以通过如下方式调用:

E:\Debin2>adb shell uiautomator runtest cheshangtongLogin.jar -e username xxx
-e passwd yyyy -c com.cheshangtong.loginhelper.LoginTest

uiautomator跑安卓端UI testing的更多相关文章

  1. CPF C#跨平台UI框架发布安卓端预览版

    CPF的安卓端适配采用Xamarin的安卓绑定库,而不是Xamarin.Form.CPF和flutter差不多,完全由skia绘制,基本不依赖原生控件. 当前还只是预览版,不建议用在正式项目中. 可能 ...

  2. <自动化测试方案_7>第七章、PC端UI自动化测试

    第七章.PC端UI自动化测试 UI自动化测试又分为:Web自动化测试,App自动化测试.微信小程序.微信公众号UI层的自动化测试工具非常多,比较主流的是UFT(QTP),Robot Framework ...

  3. 移动端UI设计规范模板参考以及设计规范的好处

    2018也快要过完了(-_-),我们的移动端的UI设计规范也层出不穷.很多APP设计师也要在年底给公司或者是团队做一个总结.那么一个像样的APP ui设计规范也是很有必要的作品回顾. 在创业公司做着一 ...

  4. 移动端UI界面设计:APP字体排版设计的七个原则

    移动端UI界面设计:APP字体排版设计的七个原则 发布于: 2015 年 2 月 9 日 by admin 再来谈移动端APP字体排版设计,也许有人会说,这个还有什么好说的呢?但是真正能够运用好APP ...

  5. 支持在安卓中UI(View)的刷新功能

     这是一款可以支持在安卓中UI(View)的刷新功能,Android中对View的更新有很多种方式,使用时要区分不同的应用场合.我感觉最要紧的是分清:多线程和双缓冲的使用情况.   现在可以尝试理解下 ...

  6. 恩布企业 IM 安卓端 1.3,服务端 1.12 公布

    恩布企业IM的 Android 安卓开源手机client EntboostIM 公布 1.3 版本号.同一时候恩布IM服务端更新至 1.12 版本号; 安卓端主要更新内容: 添加收发手机文件功能: 登 ...

  7. 杭州蓝松科技推出的安卓端的USB转串口调试助手, 欢迎下载使用

    杭州蓝松科技推出的安卓端的USB转串口调试助手, 欢迎下载使用 下载地址:http://files.cnblogs.com/guobaPlayer/%E8%93%9D%E6%9D%BEUSB%E4%B ...

  8. JAVA 后台SSM框架接收安卓端的json数据

    最近项目上与安卓端做JSON数据交互,使用的SSM框架,刚开始的时候感觉很简单,想着不就是把安卓端的JSON数据封装为Bean类对象吗? 于是就这样写了 可是这样一直报400,百度原因是因为请求url ...

  9. 优秀的基于VUE移动端UI框架合集

    1. vonic 一个基于 vue.js 和 ionic 样式的 UI 框架,用于快速构建移动端单页应用,很简约,是我喜欢的风格 star 2.3k 中文文档 在线预览 2.vux 基于WeUI和Vu ...

随机推荐

  1. Win8.1安装Visual Studio 2015提示需要KB2919355

    http://www.microsoft.com/zh-cn/download/details.aspx?id=42335 安装说明: 1.若要开始下载,请单击“下载”按钮,然后执行以下操作之一,或者 ...

  2. MyEclipse10建立Maven Webapp项目并通过git传到GitHub

    先创建Maven Webapp项目 图文详解MyEclipse中新建Maven webapp项目的步骤(很详细) 在web项目的路径中右键(前提是你机器已经装了git)“Git Init Here”, ...

  3. symbol(s) not found for architecture x86_64 之 linker command failed with exit code 1 (use -v to see invocation)解决方案排查

    这样的错误 ,我的解决方案是, 第一种:   查看他说在 ****.o 中,你要查看这样的关键点,然后去查看,你 项目中有没有引进这样的文件,在项目中查找,看项目中有没有,如果没有那就是没添加进来,你 ...

  4. AngularJS-Uncaught Error: [$injector:modulerr]

    我在实验AngularJS-系统代码的配置和翻译的时候遇到了如下图所示的错误: 在JS编程的时候会经常遇到,XXX不是一个函数,XXX未定义等等错误,只要看到和自己编写的代码语句相关的东西直接找到就能 ...

  5. php-fpm 在centos 7下的安装配置

    安装php: sudo yum install php php-fpm php-mysql php-mbstring php-mcrypt php-sockets php-curl php-commo ...

  6. wordpress stratus模板使用 产品显示问题

    产品不显示,只显示展示产品代码. 1.研究原站demo,思考产品展示调用自woocommerce. 2.查看woocommerce文档,更新展示代码. 3.修改后产品出现,但是多余的关联推荐也展示出来 ...

  7. (分享)多功能 PDF转换器v3.0版本

    转换的效果非常不错,值得使用.破解成功的截图:这个程序必须随便输入注册码注册,不然会有水印的. 这是程序主界面了 正在测试pdf转word过程,转换结果个人感觉非常不错,跟原版pdf的格式非常接近,个 ...

  8. leetcode 202

    202. Happy Number Write an algorithm to determine if a number is "happy". A happy number i ...

  9. autolayout

    autolayout.因为之前都是用frame,用代码来做,并且在布局时也很少用storyboard和xib.使得我再这方便经验很欠缺,想用,但是又怕用不好,出现各种意想不到的bug.但是又忽然想到, ...

  10. editplus工具支持sql高亮提示

    editplus默认不识别sql关键件,添加文件使其对sql高亮提示. 首先就是要自己编写一段代码,存为.stx 文件(例如sql.stx) 然后在editplus的菜单栏 工具-> 配置用户工 ...