先来看看uiautomator命令的说明
Usage: uiautomator [options]
Available subcommands:
help: displays help message
runtest: executes UI automation tests
runtest <class spec> [options]
<class spec>: [-c | -e class ]
: a list of jar file containing test classes and dependencies. If
the path is relative, it's assumed to be under /data/local/tmp. Use
absolute path if the file is elsewhere. Multiple files can be
specified, separated by space.
: a list of test class names to run, separated by comma. To
a single method, use TestClass#testMethod format. The -e or -c option
may be repeated.
options:
--nohup: trap SIG_HUP, so test won't terminate even if parent process
is terminated, e.g. USB is disconnected.
-e debug [true|false]: wait for debugger to connect before starting.
-e runner [CLASS]: use specified test runner class instead. If
unspecified, framework default runner will be used.
-e : other name-value pairs to be passed to test classes.
May be repeated.
dump: creates an XML dump of current UI hierarchy
dump [file]
[file]: the location where the dumped XML should be stored, default is
/mnt/sdcard/window_dump.xml
events: prints out accessibility events until terminated
根据google官方的说明,uiautomator使用是有版本限制的。
Android SDK Tools, Revision 21 or higher
Android SDK Platform, API 16 or higher
一般使用的都是使用jar包的方式,进行ui测试。
但是查看该命令说明,还有个dump命令。下面就来详细讲解这个命令。
dump: creates an XML dump of current UI hierarchy
这个说明说的很清楚,这个命令是用来成成当前界面的UI层次,并用XML格式进行展示
那么我们来使用一下,界面使用豌豆荚手机客户端首页做展示。
界面如下:
然后在命令行工具中,执行如下命令
adb shell uiautomator dump
执行成功 系统会返回
UI hierchary dumped to: /mnt/sdcard/window_dump.xml
当然 这个文件地址也是可以更改的。
adb shell uiautomator dump [file]
我们把这个文件从手机上pull下来,写到本地计算机进行分析
XML下载网盘地址:http://pan.baidu.com/s/1pJ5YNW3
文件内容较多,我们截取出一些进行讲解。
"1" text="理财范" class="android.widget.TextView" package="com.wandoujia.phoenix2" content-desc="" checkable="false" checked="false" clickable="false" enabled="true" focusable="false" focused="false" scrollable="false" long-clickable="false" password="false" selected="false" bounds="[174,802][402,835]" />
界面上的 ‘理财范’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 控件绘制的长宽及位置 四个数据,分成两组,分别是 左上坐标和右下坐标
在使用这个命令时会遇到的一些问题:
1、在使用shell命令下 使用uiautomator dump时会报出下面的异常
java.lang.IllegalArgumentException: Illegal character (d83d)
at org.kxml2.io.KXmlSerializer.reportInvalidCharacter(KXmlSerializer.java:144)
at org.kxml2.io.KXmlSerializer.writeEscaped(KXmlSerializer.java:130)
at org.kxml2.io.KXmlSerializer.attribute(KXmlSerializer.java:465)
at com.android.uiautomator.core.AccessibilityNodeInfoDumper.dumpNodeRec(AccessibilityNodeInfoDumper.java:111)
at com.android.uiautomator.core.AccessibilityNodeInfoDumper.dumpNodeRec(AccessibilityNodeInfoDumper.java:129)
at com.android.uiautomator.core.AccessibilityNodeInfoDumper.dumpNodeRec(AccessibilityNodeInfoDumper.java:129)
at com.android.uiautomator.core.AccessibilityNodeInfoDumper.dumpNodeRec(AccessibilityNodeInfoDumper.java:129)
at com.android.uiautomator.core.AccessibilityNodeInfoDumper.dumpNodeRec(AccessibilityNodeInfoDumper.java:129)
at com.android.uiautomator.core.AccessibilityNodeInfoDumper.dumpNodeRec(AccessibilityNodeInfoDumper.java:129)
at com.android.uiautomator.core.AccessibilityNodeInfoDumper.dumpNodeRec(AccessibilityNodeInfoDumper.java:129)
at com.android.uiautomator.core.AccessibilityNodeInfoDumper.dumpNodeRec(AccessibilityNodeInfoDumper.java:129)
at com.android.uiautomator.core.AccessibilityNodeInfoDumper.dumpNodeRec(AccessibilityNodeInfoDumper.java:129)
at com.android.uiautomator.core.AccessibilityNodeInfoDumper.dumpNodeRec(AccessibilityNodeInfoDumper.java:129)
at com.android.uiautomator.core.AccessibilityNodeInfoDumper.dumpNodeRec(AccessibilityNodeInfoDumper.java:129)
at com.android.uiautomator.core.AccessibilityNodeInfoDumper.dumpWindowToFile(AccessibilityNodeInfoDumper.java:89)
at com.android.commands.uiautomator.DumpCommand.run(DumpCommand.java:99)
at com.android.commands.uiautomator.Launcher.main(Launcher.java:83)
at com.android.internal.os.RuntimeInit.nativeFinishInit(Native Method)
at com.android.internal.os.RuntimeInit.main(RuntimeInit.java:243)
at dalvik.system.NativeStart.main(Native Method)
Killed
这个错是因为,界面中包含了一些无法解析的文本,比如表情图标。
根据官方说明,已经修复了这个问题 以下为修复的源码
https://android.googlesource.com/platform/frameworks/testing/+/2b6d1da16a4e38a9704c2c67b33aadf44a85b1d2
但必须是Android4.4+(不包含4.4)版本的Android系统才修复了
2、关于5.0上dump出来中文字符为?的bug
这个问题也是5.0底层的bug ,在5.1版本修复了。