App随机测试之Monkey和Maxim
Monkey是我们app测试熟知的一个工具,monkey可以随机的产生很多事件来操控app,所以可以用来做压力测试、稳定性测试
常用的几个选项:
-p 指定测试的包
-s 种子,以后回溯的时候只要运行相同的种子就可以回溯相同的monkey操作步骤了,常用来提供给开发复现问题、开发修复完毕以后验证是否修复完成
-v 日志 -v -vv 【-vv日志比-v详细】
--throttle 500 每个动作之间的等待时间,单位毫秒
--pct-touch 触摸事件
--pct-motion 动作事件(直线滑动)
--pct-trackball 轨迹事件(曲线滑动)
--pct-majornav 主要导航事件(回退、菜单按键等)
可以通过【adb shell monkey】来查看命令monkey支持的其他选项
$ adb shell monkey usage: monkey [-p ALLOWED_PACKAGE [-p ALLOWED_PACKAGE] ...] [-c MAIN_CATEGORY [-c MAIN_CATEGORY] ...] [--ignore-crashes] [--ignore-timeouts] [--ignore-security-exceptions] [--monitor-native-crashes] [--ignore-native-crashes] [--kill-process-after-error] [--hprof] [--pct-touch PERCENT] [--pct-motion PERCENT] [--pct-trackball PERCENT] [--pct-syskeys PERCENT] [--pct-nav PERCENT] [--pct-majornav PERCENT] [--pct-appswitch PERCENT] [--pct-flip PERCENT] [--pct-anyevent PERCENT] [--pct-pinchzoom PERCENT] [--pct-permission PERCENT] [--pkg-blacklist-file PACKAGE_BLACKLIST_FILE] [--pkg-whitelist-file PACKAGE_WHITELIST_FILE] [--wait-dbg] [--dbg-no-events] [--setup scriptfile] [-f scriptfile [-f scriptfile] ...] [--port port] [-s SEED] [-v [-v] ...] [--throttle MILLISEC] [--randomize-throttle] [--profile-wait MILLISEC] [--device-sleep-time MILLISEC] [--randomize-script] [--script-log] [--bugreport] [--periodic-bugreport] [--permission-target-system] COUNT
举例:
$ adb shell monkey -p com.xueqiu.android --throttle 200 --pct-motion 80 --pct-majornav 20 -s 100 -vv 150
打印日志如下:
:Monkey: seed=100 count=150 :AllowPackage: com.xueqiu.android :IncludeCategory: android.intent.category.LAUNCHER :IncludeCategory: android.intent.category.MONKEY // Event percentages: // 0: 0.0% // 1: 80.0% // 2: 0.0% // 3: 0.0% // 4: -0.0% // 5: -0.0% // 6: 0.0% // 7: 20.0% // 8: 0.0% // 9: 0.0% // 10: 0.0% // 11: 0.0% :Switch: #Intent;action=android.intent.action.MAIN;category=android.intent.category.LAUNCHER;launchFlags=0x10200000;component=com.xueqiu.android/.view.WelcomeActivityAlias;end // Allowing start of Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] cmp=com.xueqiu.android/.view.WelcomeActivityAlias } in package com.xueqiu.android :Sending Touch (ACTION_DOWN): 0:(1144.0,36.0) :Sending Touch (ACTION_UP): 0:(1166.7858,0.0) :Sending Touch (ACTION_DOWN): 0:(695.0,1372.0) :Sending Touch (ACTION_UP): 0:(785.3247,1288.2296) :Sending Touch (ACTION_DOWN): 0:(1065.0,863.0) :Sending Touch (ACTION_UP): 0:(1060.9724,799.45374) :Sending Touch (ACTION_DOWN): 0:(840.0,608.0) :Sending Touch (ACTION_UP): 0:(818.21136,641.3733) :Sending Touch (ACTION_DOWN): 0:(954.0,1680.0) :Sending Touch (ACTION_UP): 0:(986.87213,1625.6768) :Sending Touch (ACTION_DOWN): 0:(518.0,280.0) :Sending Touch (ACTION_UP): 0:(504.64883,311.34415) :Sending Touch (ACTION_DOWN): 0:(542.0,814.0) :Sending Touch (ACTION_UP): 0:(542.8679,814.5196) :Sending Touch (ACTION_DOWN): 0:(329.0,1624.0) :Sending Touch (ACTION_UP): 0:(353.21414,1614.9838) // Allowing start of Intent { cmp=com.xueqiu.android/.main.view.MainActivity } in package com.xueqiu.android // Allowing start of Intent { cmp=com.xueqiu.android/.community.StatusDetailActivity } in package com.xueqiu.android // activityResuming(com.xueqiu.android) :Sending Touch (ACTION_DOWN): 0:(639.0,1447.0) :Sending Touch (ACTION_UP): 0:(650.47205,1434.4948) :Sending Touch (ACTION_DOWN): 0:(330.0,1600.0) :Sending Touch (ACTION_UP): 0:(285.38858,1726.274) :Sending Touch (ACTION_DOWN): 0:(454.0,673.0) :Sending Touch (ACTION_UP): 0:(516.2043,670.9724) :Sending Touch (ACTION_DOWN): 0:(313.0,1037.0) :Sending Touch (ACTION_UP): 0:(290.13596,1044.888) :Sending Touch (ACTION_DOWN): 0:(966.0,369.0) :Sending Touch (ACTION_UP): 0:(930.24414,400.18967) //[calendar_time:2020-11-20 16:37:56.463 system_uptime:652225] // Sending event #100 //[calendar_time:2020-11-20 16:37:56.695 system_uptime:652432] // Sending event #100 :Sending Touch (ACTION_DOWN): 0:(656.0,712.0) :Sending Touch (ACTION_UP): 0:(557.1301,660.5694) :Sending Touch (ACTION_DOWN): 0:(269.0,334.0) :Sending Touch (ACTION_UP): 0:(323.9889,344.96362) :Sending Touch (ACTION_DOWN): 0:(806.0,813.0) :Sending Touch (ACTION_UP): 0:(802.8336,830.3396) :Sending Touch (ACTION_DOWN): 0:(454.0,375.0) :Sending Touch (ACTION_UP): 0:(439.70593,411.3031) :Sending Touch (ACTION_DOWN): 0:(143.0,1312.0) :Sending Touch (ACTION_UP): 0:(194.25143,1208.045) // Allowing start of Intent { cmp=com.xueqiu.android/.stock.privatedetail.PrivateDetailActivity } in package com.xueqiu.android // activityResuming(com.xueqiu.android) // Allowing start of Intent { cmp=com.xueqiu.android/.common.account.LoginOptionActivity } in package com.xueqiu.android :Sending Touch (ACTION_DOWN): 0:(192.0,701.0) Events injected: 150 :Sending rotation degree=0, persist=false :Dropped: keys=0 pointers=0 trackballs=0 flips=0 rotations=0 ## Network stats: elapsed time=15983ms (0ms mobile, 0ms wifi, 15983ms not connected) // Monkey finished
但是呢,monkye有一些无法满足的点,亟需改善的是下面2点:
1.就有可能点击到页面的顶部,下拉通知栏,或者点击到底部返回键菜单栏(尤其是模拟器里执行的时候),会有各种误点操作
2.纯随机,没有任何遍历算法加持
针对上面这俩点,我们可以选择使用Maxim来代替monkey,而且Maxim还提供了很多意外之喜:
Maxim github地址:https://github.com/zhangzhao4444/Maxim
比如:比monkey更高效的性能、可以随机输入、支持点击xpath控件、定制滑动路径 等等(但笔者目前还有尝试这些用法,惭愧)
我们先使用一下和monkye类似的功能:
首先是安装(其实github里作者写的也很清楚,这里在重新备注一下):
$ git clone git@github.com:zhangzhao4444/Maxim.git $ adb push framework.jar /sdcard $ adb push monkey.jar /sdcard
只需要这3条命令就可以了,如果想要确认一下是否真的push到手机的话可以进入adh shell查看:
$ adb shell $ cd /sdcard $ ls
接下来就是使用,可以从官方copy命令
改成自己app包和jar包的路径,执行就好了
$ adb shell CLASSPATH=/sdcard/monkey.jar:/sdcard/framework.jar exec app_process /system/bin tv.panda.test.monkey.Monkey -p com.xueqiu.android --uiautomatormix --running-minutes 1 -v -v
以上就是Maxim的初级使用了,其实Maxim的github主页中写的还挺详细的,有兴趣的读者可以移步到主页深入学习(ps:也可以给Maxim的作者点个星星,真的是一个好工具啊),这里只做抛砖引玉,如果本文能提供给你一点点点帮助,就请点个赞支持一下作者,感谢支持