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

2024-03-22 23:28

本文主要是介绍Android App开发的自动化测试框架UI Automator使用教程,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

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 oneUiObject 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那样层级分明,仅仅个人感觉,用户行为注入还是和插桩有点点区别的

​现在我也找了很多测试的朋友,做了一个分享技术的交流群,共享了很多我们收集的技术文档和视频教程。
如果你不想再体验自学时找不到资源,没人解答问题,坚持几天便放弃的感受
可以加入我们一起交流。而且还有很多在自动化,性能,安全,测试开发等等方面有一定建树的技术大牛
分享他们的经验,还会分享很多直播讲座和技术沙龙
可以免费学习!划重点!开源的!!!
qq群号:485187702【暗号:csdn11】

最后感谢每一个认真阅读我文章的人,看着粉丝一路的上涨和关注,礼尚往来总是要有的,虽然不是什么很值钱的东西,如果你用得到的话可以直接拿走! 希望能帮助到你!【100%无套路免费领取】

这篇关于Android App开发的自动化测试框架UI Automator使用教程的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/836417

相关文章

SpringBoot集成redisson实现延时队列教程

《SpringBoot集成redisson实现延时队列教程》文章介绍了使用Redisson实现延迟队列的完整步骤,包括依赖导入、Redis配置、工具类封装、业务枚举定义、执行器实现、Bean创建、消费... 目录1、先给项目导入Redisson依赖2、配置redis3、创建 RedissonConfig 配

基于 Cursor 开发 Spring Boot 项目详细攻略

《基于Cursor开发SpringBoot项目详细攻略》Cursor是集成GPT4、Claude3.5等LLM的VSCode类AI编程工具,支持SpringBoot项目开发全流程,涵盖环境配... 目录cursor是什么?基于 Cursor 开发 Spring Boot 项目完整指南1. 环境准备2. 创建

Python使用FastAPI实现大文件分片上传与断点续传功能

《Python使用FastAPI实现大文件分片上传与断点续传功能》大文件直传常遇到超时、网络抖动失败、失败后只能重传的问题,分片上传+断点续传可以把大文件拆成若干小块逐个上传,并在中断后从已完成分片继... 目录一、接口设计二、服务端实现(FastAPI)2.1 运行环境2.2 目录结构建议2.3 serv

Spring Security简介、使用与最佳实践

《SpringSecurity简介、使用与最佳实践》SpringSecurity是一个能够为基于Spring的企业应用系统提供声明式的安全访问控制解决方案的安全框架,本文给大家介绍SpringSec... 目录一、如何理解 Spring Security?—— 核心思想二、如何在 Java 项目中使用?——

springboot中使用okhttp3的小结

《springboot中使用okhttp3的小结》OkHttp3是一个JavaHTTP客户端,可以处理各种请求类型,比如GET、POST、PUT等,并且支持高效的HTTP连接池、请求和响应缓存、以及异... 在 Spring Boot 项目中使用 OkHttp3 进行 HTTP 请求是一个高效且流行的方式。

Java使用Javassist动态生成HelloWorld类

《Java使用Javassist动态生成HelloWorld类》Javassist是一个非常强大的字节码操作和定义库,它允许开发者在运行时创建新的类或者修改现有的类,本文将简单介绍如何使用Javass... 目录1. Javassist简介2. 环境准备3. 动态生成HelloWorld类3.1 创建CtC

使用Python批量将.ncm格式的音频文件转换为.mp3格式的实战详解

《使用Python批量将.ncm格式的音频文件转换为.mp3格式的实战详解》本文详细介绍了如何使用Python通过ncmdump工具批量将.ncm音频转换为.mp3的步骤,包括安装、配置ffmpeg环... 目录1. 前言2. 安装 ncmdump3. 实现 .ncm 转 .mp34. 执行过程5. 执行结

Java使用jar命令配置服务器端口的完整指南

《Java使用jar命令配置服务器端口的完整指南》本文将详细介绍如何使用java-jar命令启动应用,并重点讲解如何配置服务器端口,同时提供一个实用的Web工具来简化这一过程,希望对大家有所帮助... 目录1. Java Jar文件简介1.1 什么是Jar文件1.2 创建可执行Jar文件2. 使用java

C#使用Spire.Doc for .NET实现HTML转Word的高效方案

《C#使用Spire.Docfor.NET实现HTML转Word的高效方案》在Web开发中,HTML内容的生成与处理是高频需求,然而,当用户需要将HTML页面或动态生成的HTML字符串转换为Wor... 目录引言一、html转Word的典型场景与挑战二、用 Spire.Doc 实现 HTML 转 Word1

Java中的抽象类与abstract 关键字使用详解

《Java中的抽象类与abstract关键字使用详解》:本文主要介绍Java中的抽象类与abstract关键字使用详解,本文通过实例代码给大家介绍的非常详细,感兴趣的朋友跟随小编一起看看吧... 目录一、抽象类的概念二、使用 abstract2.1 修饰类 => 抽象类2.2 修饰方法 => 抽象方法,没有