Android App开发的自动化测试框架UI Automator使用教程

 更新时间:2016年07月11日 17:31:52   作者:Kevin_Yang  
UI Automator为Android程序的UI开发提供了测试环境,这里我们就来看一下Android App开发的自动化测试框架UI Automator使用教程,需要的朋友可以参考下

Android的自动化测试有很多框架,其中ui automator是google官方提供的黑盒UI相关的自动化测试工具,(GitHub主页:case使用java写,今天实践了一下官方文档中样例程序,其中还是有一些小问题需要总结一下的。
环境准备:
1.JDK(是的,你没看错,基础的android开发环境必备),以及对应的环境变量配置,不会的可以自己百度下下
2.Android Studio(IDE尊崇个人意愿)
3.android SDK以及配置
4.ANT(主要用于build我们的脚本,生成jar包)
ant的搭建主要分几步:
1.下载ant安装文件并且解压安装;
2.新建系统环境变量ANT_HOME,参数值是你的ant安装目录;
3.在Path环境变量中添加ant安装目录的bin文件夹,比如我的就是C:\cod\apache-ant-1.9.6\bin
4.配置完以后,测试一下,在命令行下输入ant -version,如果显示你所安装的ant版本信息,证明环境变量配置成功

使用流程
1、使用ADT创建一个java的项目
在创建项目的时候要加上JUnit与你使用的Android platforms中对应的android.jar与uiautomator.jar

2016711171444606.png (638×286)

2、新建一个包(我这里就只叫com)
3、再这个包下创建一个class,输入以下java代码,代码全是官方文档上的代码,除了最上面的package

package com;

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 Runer 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、使用ant工具生成build.xml
我这里在使用ADT自已的ant插件时提示
build.xml:26: Class not found: javac1.8

网上查了查,是插件与我java环境不符,下载最新的ant插件就可以了http://ant.apache.org/bindownload.cgi

2016711171556435.png (540×271)

下载这个tar.gz包,解压,然后将apache-ant-1.9.4\bin目录添加到环境变量PATH中
然后cmd到android sdk的tools目录,使用andrlid list命令,记住你将要在模拟器中运行的(也是你刚刚导入android.jar与uiautomator.jar包时所在的platforms)
在cmd下使用

android create uitest-project -n <name> -t <android-sdk-ID> -p <path>

-n 为生成的jar包名称,自已任意定义,
-t 为上面查看到的值,我这里是1
-p 为输出路径,这里就是刚才创建的java项目所在的路径

android create uitest-project -n AutoRunner -t 1 -p D:\myAndroidStudy\androidTest

然后再cmd进入D:\myAndroidStudy\androidTest,使用ant build命令生成AutoRunner.jar文件
5、将这个AutoRunner.jar文件push到模拟器中

adb push AutoRunner.jar /data/local/tmp

6、使用

adb shell uiautomator runtest AutoRunner.jar –c com.Runer 

使Runer类运行

2016711171651759.png (654×321)

我的代码里又在官方基础上多了一个点击”sound”的操作与点击Home键操作

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

2016711171931815.png (517×470)

这个其实也只是一个简单的玩具代码,没有什么意义,但是官方作为一个引导,其中也使用了一些最常见的接口。以后再深入的学习uiautomator

总结
优点:
1.可以对所有操作进行自动化,操作简单;
2.不需要对被测程序进行重签名,且,可以测试所有设备上的程序,比如~某APP,比如~拨号,比如~发信息等等
3.对于控件定位,要比robotium简单一点点
缺点:
1.uiautomator需要android level 16以上才可以使用,因为在level 16及以上的API里面才带有uiautomator工具
2.如果想要使用resource-id定位控件,则需要level 18及以上才可以
3.对中文支持不好(不代表不支持,第三方jar可以实现)
4.个人感觉,控件定位不如robotium那样层级分明,仅仅个人感觉,用户行为注入还是和插桩有点点区别的

相关文章

  • Android 判断是否是是全汉字、全字母、全数字、数字和字母等(代码)

    Android 判断是否是是全汉字、全字母、全数字、数字和字母等(代码)

    这篇文章主要介绍了Android 判断是否是是全汉字、全字母、全数字、数字和字母等的实例代码,需要的朋友可以参考下
    2016-12-12
  • Android  ADB详细介绍及用法

    Android ADB详细介绍及用法

    本文主要介绍Android ADB,这里整理了Android ADB的文档资料,详细介绍了adb 命令,有需要的小伙伴可以参考下
    2016-08-08
  • Android View 布局流程(Layout)全面解析

    Android View 布局流程(Layout)全面解析

    这篇文章主要为大家全面解析了Android View 布局流程Layout,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-02-02
  • Android自定义控件实现温度旋转按钮效果

    Android自定义控件实现温度旋转按钮效果

    这篇文章主要给大家介绍了关于Android如何通过自定义控件实现温度旋转按钮的效果,文中通过思路与方法一步步介绍的很详细,相信对大家的理解和学习具有一定的参考借鉴价值,有需要的朋友们下面来一起看看吧。
    2016-12-12
  • Android自定义View之渐变色折线图的实现

    Android自定义View之渐变色折线图的实现

    折线图的实现方法在github上有很多开源的程序,但是对于初学者来讲,简单一点的教程可能更容易入门,下面这篇文章主要给大家介绍了关于Android自定义View之渐变色折线图的相关资料,需要的朋友可以参考下
    2022-04-04
  • Android学习笔记45之gson解析json

    Android学习笔记45之gson解析json

    JSON即JavaScript Object Natation,是一种轻量级的数据交换格式,采用完全独立于语言的文本格式,为Web开发提供了一种理想的数据交换格式。通过本篇文章给大家介绍Android学习笔记45之gson解析json的相关内容,对android gson解析json相关知识感兴趣的朋友一起学习吧
    2015-12-12
  • Android实现打地鼠小游戏

    Android实现打地鼠小游戏

    这篇文章主要为大家详细介绍了Android实现打地鼠小游戏,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2020-04-04
  • Android实现简单手电筒功能

    Android实现简单手电筒功能

    这篇文章主要为大家详细介绍了Android实现简单手电筒,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2019-09-09
  • Android中SharedPreferences简单使用实例

    Android中SharedPreferences简单使用实例

    这篇文章主要介绍了Android中SharedPreferences简单使用案例,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-10-10
  • Android 编辑头像功能简单实现实例(图片选取,裁剪)

    Android 编辑头像功能简单实现实例(图片选取,裁剪)

    这篇文章主要介绍了Android 编辑头像功能简单实现实例(图片选取,裁剪),非常具有实用价值,需要的朋友可以参考下
    2017-06-06

最新评论