UiAutoMator 测试框架

Uiautomator 看名字可以知道它是用来对你的Android应用程序UI进行测试的,是一种自动化的测试工具。

在Android SDK Plateform,API 16 或更高版本后出现。


Uiautomator 测试框架的测试流程概述:

1,你需要将你需要测试的Android应用安装在测试设备上,并且保证应用能够被uiautomator测试框架访问;(关于应用如何才能被测试框架访问的问题,后面再讨论)

2,创建自动化的测试用例模拟交互操作;

3,编译并将测试用例打包成 jar 包发送到安装了要测试的应用的设备上;

4,运行测试用例,观察测试结果;

5,接下来就可以更正Bug 或其他缺陷了。


UI分析工具 uiautomatorviewer:

Android的SDK提供了一个可视化的观察应用界面布局参数的工具uiautomatorviewer。

uiautomatorviewer开启方法:(Windows 和 Linux下都一样)

1,将设备连接到电脑;

2,打开终端命令行,cd 到 <android-sdk> / tools ;

3, 输入命令 “ uiautomatorviewer ” 即可打开 uiautomatorviewer;

4,点击左上角的手机形状按钮,刷新到当前手机显示的页面;

5,点击右侧黄色三角感叹号,可以显示当前页面组件是否可以被uiautomator访问(显示黄色说明无法访问,但是个人实践发现在不同的设备上会有不同的效果,有时显示黄色,也可以被访问到);




当鼠标选中左侧任意的组件时,在右侧就会显示该选中组件的布局信息和属性信息。


测试前的一些准备工作:

安装测试应用到设备

鼠标操作即可完成,或者用adb工具将apk包复制到设备。

确认应用中要测试的UI组件

需测试的组件最好是可见的,可交互的,组件最好包括 text labels, android:contentDescription  这些属性,使用uiautomatorviewer进行分析

保证应用是可访问的

1,ImageView、ImageButton、CheckBox 和其他user interface controls 要使用 android:contentDescription 来标注;

2,EditText 要提供 android:hint 属性;

3,Associate an android:hint attribute with any graphical icons used by controls that provide feedback to the user (for example, status or state information).

4,Make sure that all the user interface elements are accessible with a directional controller, such as a trackball or D-pad.

5,Use the uiautomatorviewer tool to ensure that the UI component is accessible to the testing framework. You can also test the application by turning on accessibility services like TalkBack and Explore by Touch, and try using your application using only directional controls.

(后面几条我自己还不是很清楚)


配置开发环境:

Eclipse的配置:

1,创建一个 Java Project,例如“ MyUiautomatorDemo ” ,在这个工程中我们会编写正对某一应用的测试用例;

2,从 Project Explore 中选中“MyUiautomatorDemo ” 右键进入 选择 build path

选择 “add library” > Junit   再选择Junit3 加入

选择“configure build path” > add External Jars  选择最新版本SDK下的 uiautomator.jar 和 android.jar 加入


创建测试用例:

在开始编码之前可以先了解一下,uiautomator测试框架用到的一些类,大多简单易懂好用,但是唯一令人头疼的是:UiScrollable

1,UiDevice

2,UiSelector

3,UiObject

4,UiCollection

5,UiScrollable


在开发者文档上有一个官方的例子,但是并不是在在所有的设备上都好使,因此为这里提供一个更加简单的示例仅供参考:

public class LaunchSettings extends UiAutomatorTestCase {

    public void testDemo() throws UiObjectNotFoundException {

        // Simulate a short press on the HOME button.
        getUiDevice().pressHome();
		// Simulate a short press on the Menu button.
        getUiDevice().pressMenu();
		// Find the System settings application
        UiObject settings = new UiObject(new UiSelector().text("System settings"));

        settings.clickAndWaitForNewWindow();
  
    }
}

这个测试用例只是简单的打开home页面,点击menu键,进入 设置页面。


部署测试用例到设备:

1,创建build.xml 配置文件用于打包成Jar包:

启动终端命令行:

<android-sdk>/tools/ android create uitest-project -n <name> -t 1 -p <path>
name : 工程的名字 “ MyUiautomatorDemo 

path: 对应工程的目录路径  ../.../MyUiautomatorDemo 


2,设置ANDROID_HOME 环境变量:

Windows:

set ANDROID_HOME=<path_to_your_sdk>
Unix:

export ANDROID_HOME=<path_to_your_sdk>

3,切换到你的build.xml文件所在的工程目录,创建jar包:

ant build

在Eclipse中,可以通过右键工程目录下的build.xml文件 run as > ant build 来打包Jar包,但是在此操作之前,你需要需该build.xml配置文件中的:

<project name="LaunchSettings" default="help">
改为:

<project name="LaunchSettings" default="build">


Jar包在工程目录下的 ../bin目录下


4,部署Jar包到设备:

adb push <path_to_output_jar> /data/local/tmp/


运行测试用例:

adb shell uiautomator runtest LaunchSettings.jar -c com.uia.example.my.LaunchSettings


美好的一天早上~~~










  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值