UiAutomator

1、UiAutomator简介

(1) 可以对所有操作进行自动化,操作简单

(2) 不需要对被测程序进行重签名,且,可以测试所有设备上的程序

(3) 从android sdk api 16开始,Android SDK开始支持两个做功能UI测试的新工具。uiautomatorviewer,一个用以扫描以及分析Android应用程序的UI部件的工具。以及uiautomator,一个提供API用以自定义UI测试的Java库。

(4)对中文支持不好(不代表不支持,第三方jar可以实现)

(5) 如果想要使用resource-id定位控件,则需要level 18及以上才可以

(6)一个测试的Java库,包含了创建UI测试的各种API和执行自动化测试的引擎。



2、[endif]UiAutomator环境搭建

(1) [endif]JDK

(2) [endif]eclipse

(3) [endif]android sdk

(4) ANT

(5) [endif]在使用uiautomator之前需要进行导入一个uiautomator.jar和android.jar的包;

链接:http://download.csdn.net/download/qq_30840197/9614039

主要使用到的类只有几个,如下UiDevice,UiSelector,UiObject,UiScrollable,Uiobjection。


eclipse-->新建java project-->libraries-->Add External JARS添加uiautomator.jar和android.jar-->Add Libraries添加Junit


[if !supportLists]3、[endif]UiAutomator自动化测试步骤

[if !supportLists](1) [endif]eclipse中编写脚本,UIAUtomator框架需要用到Junit、android.jar、uiautomator.jar

[if !supportLists]① [endif]添加Junit库:

new java project-->Labraries-->Add Labraries选择Junit

[if !supportLists]② [endif]添加android.jar和uiautomator.jar:

new java project-->Labraries-->Add External JARS...-->

选择android.jar和uiautomator.jar

(路径在:D:\android sdk\android-sdk_r24.4.1-windows\android-sdk-windows\platforms)




创建新类:选择ui-->uid


[if !supportLists](2) [endif]cmd中cd到D:\android sdk\android-sdk_r24.4.1-windows\android-sdk-windows\tools

[if !supportLists](3) [endif]运行命令android list target,查看对应android版本的SDK的ID值

要选择SDK API版本大于16的版本

[if !supportLists](4) [endif]在tools目录下,运行

android create uitest-project -n Checker -t 1 -p D:\eclipse\workspace\UIAUtomator

备注:

android create uitest-project -n -t -p

name:将来生成的测试项目jar包的名字,可以自己随意定义;

android-sdk-ID:第三步骤中查找出的id值;

project_path:Eclipse里刚才建立的测试工程脚本的路径的根目录



[if !supportLists](5) [endif]运行命令后,将会在工程的根目录下生成build.xml文件。


[if !supportLists](6) [endif]cmd中cd进入到eclipse生成build.xml文件所在目录,运行ant build,将使用ant编译生成jar,成功后,会在工程bin目录下生成对应名称的jar包。




[if !supportLists](7) [endif]将jar包push到手机data/local/tmp目录下,对应的命令是:

adb push

[if !supportLists](8) [endif]使用命令行启动我们已经push进去的uiautomator脚本

adb shell uiautomator runtest

手机会执行脚本并完成对应操作。

adb shell uiautomator runtest Checker.jar

-c Automator.QQTest.UiAutomatorQQTest

[if !supportLists](9) [endif]详细操作步骤参考:https://www.testwo.com/blog/7057


[if !supportLists]4、[endif]基础对象、类以及定位方式

参考链接:https://www.cnblogs.com/JianXu/category/783771.html

[if !supportLists](1) [endif]类对象:

[if !supportLists]① [endif]UiDevice

此类主要包含了获取设备状态信息,和模拟用户使用设备的操作两类api。 按键主要为模拟设备的物理按键,如home键,menu键,back键,音量键等

takeScreenshot()允许随时对设备截屏。




代码如:

a、截图

File f = new File("/sdcard/Screenshot.png");

UiDevice.getInstance().takeScreenshot(f);


b、按home键:

UiDevice.getInstance().pressHome();

或者getUiDevice().pressHome();


c、按小写/大写字母a/A:

UiDevice.getInstance().pressKeyCode(KeyEvent.KEYCODE_A);

UiDevice.getInstance().pressKeyCode(KeyEvent.KEYCODE_A,1);


d、获取屏幕宽高度:

int height = UiDevice.getInstance().getDisplayHeight();

int width = UiDevice.getInstance().getDisplayWidth();


e、实现滑动:

device.swipe(100,100,100,500,5);  //从(100,100)到(100,500),长度5毫秒,步数越长越慢


f、实现拖动:

device.drag(100,100,100,500,5);


g、唤醒和熄灭屏幕:

UiDevice.getInstance().wakeUp();

UiDevice.getInstance().sleep();


h、获取当前界面package包名:

String package_name =

UiDevice.getInstance().getCurrentPackageName();



[if !supportLists]② [endif]UiSelector

主要是通过一定条件筛选方式,定位到所要操作的UI元素。

一般UI元素均可通过以下API定位:

text() , textStartsWith(),

className() ,classNameMatches(String regex), 

className(Class type) ,Description(), 

descriptionMatches(String regex)

descriptionStartsWith(), descriptionContains() ,packageName(), 

packageNameMatches(String regex),index()和 instance()。


index():是当前页面的ID编号

instance()则表示在一定都搜索结果下,获取的子元素集的第几个元素。


代码如:


a、通过id获取:

UiObject new_target1 = new UiObject(new UiSelector().resourceId("com.tencent.mobileqq:id/name"));

new_target1.click();


b、通过class-name获取:instance()为角标,这个是在第几次出现,1为第二个

UiObject new_target2 = new UiObject(new

  UiSelector().className("android.widget.Button").instance(1));


c、在信息中输入电话号码:10086:

UiSelector uisel_message_number =

new UiSelector().className(

"android.widget.MultiAutoCompleteTextView").

instance(0);

UiObject uiobj_message_number =

new UiObject(uisel_message_number);

if(uiobj_message_number.exists()){

uiobj_message_number.setText("10086");

}

else{

System out println("uiobj_message_number对象不存在!");

}


[if !supportLists]③ [endif]UiScrollable

UiScrollable 用来表示可以滑动的界面元素,

继承自UiCollection ,而UiCollection 继承自UiObject 


代码如:

首先定义一个UiScrollable对象,识别这个对象的唯一条件就是,屏幕上有可滑动的控件。(这里有一个问题,就是如果屏幕上同时存在2个可滑动的控件,就会报错了)

然后我们判断这个可滑动对象是否存在

[if !supportLists]· [endif]存在,则使用getChildByText方法,获取我们想要点击的那个控件,然后点击它

[if !supportLists]· [endif]不存在,则说明页面不可滑动,也就是所有控件均已显示在界面上。那我们就可以直接使用UiObject获取控件并操作它。


public void scrollClickObject(String targetClassName,String targetName) throws UiObjectNotFoundException {

    UiScrollable collectionObject = new UiScrollable(new UiSelector().scrollable(true));

    if(collectionObject.exists()) {

        UiObject scrollableObject = collectionObject.getChildByText(new UiSelector().className("com.tencent.Tim"), targetName);

        scrollableObject.clickAndWaitForNewWindow();

    } else {

        UiObject targetObject = new UiObject(new UiSelector().className(targetClassName).text(targetName));

        targetObject.clickAndWaitForNewWindow();

    }

}


或者

//以步长为5(默认值)快速向下(前)滑动

UiScrollable scroll=new UiScrollable(new  UiSelector().className("android.widget.ListView"));

scroll.flingForward();


[if !supportLists]④ [endif]UiObject

UiObject可代表页面的任意元素,它的各种属性定位通常通过UiSelector来完成。

[if !supportLists]⑤ [endif]UiCollection控件定位器:

继承自UiObject

3种调用方式:UiCollection对象假设为collection:

//两个参数一个是UiSelector参数,一个可以指定为描述或者文本或者实例条件。

方式1:collection.getChildByDescription(childPattern,text);

方式2:collection.getChildByInstance(childPattern,instance);

方式3:collection.getChildByText(childPattern,text);


原理:

从业务流程分析(如我要打开应用QQ、QQ在文件夹“聊天”下,而文件夹在当前页----如图)

那我们就可以将这四个作为一个集合。



代码如:

//1.通过类名获取所有组件的集合

UiCollection collection = new UiCollection(new UiSelector().

className("android.view.ViewGroup").instance(0));

//2.通过类名对步骤1中的集合进行筛选

UiObject obj_chat = collection.getChildByDespriton(

new UiSelector().className("android.widget.LinearLayout").

index(3),"聊天");

//3.点击“聊天”文件夹    

obj_chat.click();


[if !supportLists]⑥ [endif]UiAutomatorTestCase

每个测试用例都需要继承UiAutomatorTestCase,以实现测试环境的setup,teardown等功能。而UiAutomatorTestCase则是通过继承Junit3中的TestCase类,并调用其中的setUp()、tearDown() 、getParams() 函数。其中主要是用Bundle实现Android Activity之间的通讯。在UiAutomatorTestCase,还加入了getUiDevice()等关于UiDevice的 函数,以实现在测试的任意地方均可调用UiDevice()。

脚本实例:

http://blog.chengyunfeng.com/?p=504



[if !supportLists]5、[endif]关于断言:

为了确认某一个对象是否被成功获得,加入断言语句:

代码如:

UiObject new_target1 = new UiObject(new UiSelector().resourceId("com.tencent.mobileqq:id/name"));

assertTrue("HAS BEEN NOT FOUND",new_target1.exists());

System out println("The icon has been found!");

new_target1.click();



[if !supportLists]6、[endif]UI Auromator测试用例继承自UiAutomatorTestCase类,UiAutomatorTestCase类继承自junit.framwork.TestCase类,所以可以用Junit的Assert类来比较测试结果。

[if !supportLists]7、[endif]sleep(1000);延时1秒钟




以编程的思维考虑:参考链接:

https://blog.csdn.net/fhaohaizi/article/details/77941155


截图对于图片大小的处理:

https://blog.csdn.net/u011277123/article/details/53535296


常用API整理:

http://www.cnblogs.com/fnng/p/9291855.html


查漏补缺:

http://www.cnblogs.com/by-dream/p/4996000.html

你可能感兴趣的:(UiAutomator)