常用参数:developer.android.com/studio/test…
常见问题
** No activities found to run, monkey aborted.
现象:执行命令 "monkey -p com.huawei.hwdockbar -v -v --throttle 1000 --ignore-crashes --ignore-timeouts 800" 报错"No activities found to run, monkey aborted."
原因:不是所有APK都有Activity、都可以用 -p参数 启动。-p参数 的前提条件是,应用的Activity里面一定要有以下Category:android.intent.category.LAUNCHER 或者android.intent.category.MONKEY。
Monkey默认搜索以下截取
:IncludeCategory: android.intent.category.LAUNCHER
:IncludeCategory: android.intent.category.MONKEY
方案:因为有某些应用确实不方便添加android.intent.category.LAUNCHER,但是可以添加android.intent.category.MONKEY作为替换,而不会影响你的应用。如果以上两个Category 都不愿意添加,那可以使用 -c 参数,-c参数可以跟上你应用的一个Category名字,但是一定要注意这个Category 在手机中是独一无二的。故:建议添加此项:android.intent.category.MONKEY
备注:对于智慧多窗这种没有MainActivity的应用,也可考虑:①使用uiautomator>uidevice>swipe操作进行模拟,但难点在于只能模拟滑动,无法模拟滑动后长按一段时间;②使用am startservice -n com.huawei.hwdockbar/com.huawei.hwdockbar.DockMainService拉起应用,但验证结果是命令能执行成功,但无法调出智慧多窗;
精准monkey
背景:原生monkey基于坐标的点击导致无效操作过多,如果能基于控件点击就能提高操作有效性和问题发现概率
思路:通过“adb shell uiautomator dump”获取当前页面布局的xml文件(uiautomatorviewer.bat可以可视化查看),将“clickable=true”的控件过滤出来,再去计算这些控件的位置进行点击。另外,为了定位问题需要还原执行过程,所以在每次点击控件前进行点击坐标的标记、截图(可用adb shell/exec-out screencap -p xxx.png命令)。整体流程大致如下:
问题:1、获取布局文件、截图等操作增加耗时;2、如果测试对象是手机,测试每个apk的每个页面都需要如此操作,大大降低了测试执行效率;3、该方案是通过修改monkey源码实现,执行时替换/system/framework/monkey.jar,如果Android版本更新,需要重新适配;monkey源码分析可以参考:www.cnblogs.com/by-dream/p/…