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的作者點個星星,真的是一個好工具啊),這里只做拋磚引玉,如果本文能提供給你一點點點幫助,就請點個贊支持一下作者,感謝支持