uiautomator执行命令详解
uiautomator一般由adb shell调用,也可以在手机端使用runTime执行。使用 adb shell uiautomator helper可以查看到uiautomator所有的命令
uiautomator的执行测试用例的命令如下:
#adb shell uiautomator runtest $jar_name -c $test_class_or_method [options]
下面详细的说一下命令:
1、runtest
runtest是执行测试的关键命令,用来执行jar包的测试用例。
2、jar_name
用于指定需要执行的测试用例所在的jar包名称,使用相对路径(uiautomator的jar包放置在安卓手机的/data/local/tmp/目录下),可以多指定jar包。
(1)若只存在一个测试jar包 A.jar,命令如下:
#adb shell uiautomator runtest A.jar -c $test_class_or_method [options]
(2)若 A.jar 测试过程中引入了第三方的jar包 B.jar ,则并列写,中间使用空格分隔即可,命令如下:
#adb shell uiautomator runtest A.jar B.jar -c $test_class_or_method [options]
3、-c $test_class_or_method
用于指定具体的case,参数指定时需要写全全量路径(包名 .class#method),可以有三种指定方式。
假设 A.jar 中存在两个测试类 C.java、D.java,分别测试方法 C.testC_e,C.testC_f,D.testD_g,D.testD_h
(1)什么也不指定(-c class):
#adb shell uiautomator runtest A.jar
会执行指定jar包中所有测试用例(以test开头的方法,通过反射进行调用,按字母顺序顺序),再次会执行 C.testC_e,C.testC_f,D.testD_g,D.testD_h
(2)指定测试用例类名:
#adb shell uiautomator runtest A.jar -c com.uiautomator.demo.C
会执行指定类下的所有测试用例,如果有多个类可以并列指定,中间用空格分开隔开,如以上指令会运行 C.testC_e,C.testC_f;
(3)指定测试用例类名加方法名(-c class#method)
#adb shell uiautomator runtest A.jar -c com.uiautomator.demo.C#testC_e -c com.uiautomator.demo.D#testD_g
会执行指定类下指定的测试方法,同样多个方法之前使用空格分开并列指定,如上命令会运行testC_e,estD_g;
4、 [options]:
(1)--nohup
指定后台挂起运行若没有该参数,通过adb shell 执行uiautomator在设备断开连接的时候会停止运行,使用该参数的效果等价于使用“&”挂起,这是个比较常用的命令,建议一直加载运行测试用例命令中使用。如:
#adb shell uiautomator runtest uiautomatorDemo.jar --nohup -c com.uiautomator.demo.Home
(2)-e <key> <value>
给执行过程中指定参数,以键值对的方式动态封装成Bundle供测试过程使用,在UiAutomatorTestCase中使用getParams()可获得,可以多项指定。
假设我需要想jar包传三个参数,String uiserName,String password,long runTime,则对应的命令应该如下:
#adb shell uiautomator runtest uiautomatorDemo.jar -e userName tangbc -e runTime 1000000 -e password 123 --nohup -c com.uiautomator.demo.Home
在UiautomatorTestCase中对应接受参数的地方应该书写如下:
public void test1() throws Exception {
Bundle bundle=getParams();//获取键值对
String userName=bundle.getString("userName");
String password=bundle.getString("password");
long runTime=Long.parseLong(bundle.getString("runTime"));
System.out.println(runTime+"======="+password+"========"+userName);
}
注意:非String类型的参数不能通过bundle.getLong这种方法获取,这样是获取不到的,这些键值对必须通过getString来获取,然后对应转化成你要的类型。
(3)-e debug [true|false]
本质上和上面的-e是一样的用法,起特别的地方在于debug为uiautomator指令中的保留字,指定参数可以打开执行过程中的调试端口,默认调试模式为关闭
#adb shell uiautomator runtest A.jar -c com.uiautomator.demo.C-e debug true
http://blog.****.net/zhangmiaoping23/article/details/51702054
5、dump [file]
dump当前界面的xml至文件夹中,通常用于保留当前页面供调试分析,file为保存文件路径,不指定默认为/storage/sdcard0/window_dump.xml。命令如下:
#adb shell uiautomator dump [file]
该xml文件内容较多,截取出一些进行讲解
<node index="1" text="" resource-id="" class="android.widget.ImageView" package="com.miui.home" content-desc="第2屏" checkable="false" checked="false" clickable="true" enabled="true" focusable="false" focused="false" scrollable="false" long-clickable="false" password="false" selected="false" bounds="[520,1560][560,1620]" />
界面上的 ‘第2屏’TextView 在dump后显示成这样的节点
index 在当前的ViewGroup中的序号 从0开始
text 控件中显示的文本内容
class 控件的类型
package 包名
content-desc 说明
checkable 是否允许check
checked check状态
clickable 是否允许click
enabled 控件状态
focusable 是否允许获取焦点
focused 是否获取到焦点
scrollable 是否允许滚动
long-clickable 是否允许长安
password 是否是密码控件
selected select状态
bounds 控件绘制的长宽及位置 四个数据,分成两组,分别是 左上坐标和右下坐标
不过效果跟uiautomatorviewer来查看当前界面的层级,更加清晰。
6、events:
对应EventsCommand这个类,获取accessibility events,你在命令行运行'uiautomator events'然后在链接设备上操作一下就会看到相应的事件打印出来,events使用频率极低。
7、 -e outputFormat simple | -s
启用JUnit风格输出(终端)
这是原始风格。