標籤:模組 文法規則 tool 常用 dom list 命令工具 ref filter
Monkey是Android中的一個命令列工具,可以運行在模擬器裡或實際裝置中。它向系統發送偽隨機的使用者事件流(如按鍵輸入、觸控螢幕輸入、手勢輸入等),實現對正在開發的應用程式進行壓力測試。Monkey測試是一種為了測試軟體的穩定性、健壯性的快速有效方法。
Monkey測試參數建議
- 間隔時間:500毫秒;
- 種子數:隨機;
- 遇到錯誤:不停止
- 執行時間長度:每機型不小於12小時 或 點擊次數:100萬次;
- 機型覆蓋建議:覆蓋高中低端機型,不同晶片平台,不同解析度,不同安卓版本;
- 參考命令:
adb shell monkey -p com.package.xxx --throttle 500 --ignore-crashes --ignore-timeouts --ignore-security-exceptions --ignore-native-crashes --monitor-native-crashes -v -v -v 1000000>G:\MonkeyTest.log 2>&1 &
Monkey測試方案
Monkey測試指令碼
Android 的 monkey test 工具提供了 -f scriptfile 參數,可以指定 test 指令碼
什麼是monkey script
Monkey script是按照一定的文法規則編寫有序的使用者事件流並適用於monkey命令工具的指令碼
adb shell monkey -f <script file> <運行指令碼的次數>
- 例:adb shell monkey -f /sdcard/monkey.script 10,那麼這個指令碼裡面指定的動作就會被執行10次
說明:指令碼位置可以是絕對位置,也可以是相對位置(cd進入相應的目錄
在development/cmds/monkey/src/com/android/commands/monkey/MonkeySourceScript.java
源碼下有一段注釋規定了monkey script的基本規則:
monkey event queue. It takes a script to produce events sample script format: type= raw events count= 10 speed= 1.0 start data >> captureDispatchPointer(5109520,5109520,0, 230.75429,458.1814,0.20784314,0.06666667 ,0,0.0,0.0,65539,0) captureDispatchKey(5113146,5113146,0,20,0,0,0,0) captureDispatchFlip(true) ... */
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
指令碼的內容樣本(源碼修正版)
//header
type= raw events
count= 10
speed= 1.0
//line at the end of the header means that below it is the context of script
start data >>
DispatchPointer(5109520,5109520,0,230.75429,458.1814,0.20784314,
0.06666667,0,0.0,0.0,65539,0)
DispatchKey(5113146,5113146,0,20,0,0,0,0)
DispatchFlip(true)
...
說明:
1)、前3行是指令碼頭
2)、後3行是指令碼內容,每行為一個函數
monkey中提供的函數如下:
1..DispatchPointer(long downTime, long eventTime, int action, float x, float y, float pressure, float size, int metaState, float xPrecision, float yPrecision, int device, int edgeFlags) 2..DispatchTrackball(long downTime, long eventTime, int action, float x, float y, float pressure, float size, int metaState, float xPrecision, float yPrecision, int device, int edgeFlags) 軌跡球3..DispatchKey(long downTime, long eventTime, int action, int code, int repeat, int metaState, int device, int scancode) 4..DispatchFlip(boolean keyboardOpen) 實體鍵盤關閉與開啟5..DispatchPress(int keyCode) 6..LaunchActivity(String pkg_name, String cl_name) 7..UserWait(long sleeptime) 8..LongPress(int keyCode)
- 鍵盤按下/彈起
key [down|up] keycode
這個命令類比一次鍵盤輸入。
keycode參數值詳見KeyEvent類的KEYCODE列表。這個參數的格式很靈活,例如類比功能表按鈕可以使用82(功能表按鈕的索引值),也可以使用 KEYCODE_MENU(索引值的名稱,必須嚴格都是大寫字母),還可以使用menu(Monkey程式會自動添加KEYCODE部分,此時並不區分大小 寫)。
注意一次完整的敲擊(press)操作是一個按下(key down)和彈起(key up)的組合
- 觸摸按下/彈起/移動
touch [down|up|move] x y
This command injects a MotionEvent into the input system that simulates a user touching the touchscreen (or a pointer event). x and y specify coordinates on the display (0 0 being the upper left) for the touch event to happen. Just like key events, touch events at a single location require both a down and an up. To simulate dragging, send a “touch down”, then a series of “touch move” events (to simulate the drag), followed by a “touch up” at the final location.
- 滾動軌跡球
trackball dx dy
This command injects a MotionEvent into the input system that simulates a user using the trackball. dx and dy indicates the amount of change in the trackball location (as opposed to exact coordinates that the touch events use)
- 開啟/關閉實體鍵盤
flip [open|close]
類比一次實體鍵盤的開啟/關閉。
在沒有實體鍵盤的手機上此命令無效。
- 喚醒裝置(點亮螢幕)
wake
這個命令將喚醒裝置,並允許使用者輸入。
如果裝置鍵盤已鎖,這個命令並不能解鎖鍵盤
- 螢幕點擊
tap x y
這個命令類比一次螢幕點擊。
這個命令就是touch down和touch up命令的一次組合。
- 敲擊鍵盤
press keycode
這個命令類比一次鍵盤敲擊。
這個命令就是key down和key up命令的一次組合。
- 鍵入字串
type string
這個命令類比鍵入一個字串
該命令會完全類比每一個字元的鍵盤事件
列出環境變數
- listvar
該命令將列出Monkey的所有環境變數。
傳回值為空白格分隔的字串。
- 擷取環境變數值
getvar varname
該命令用於擷取指定的環境變數的值。
通過listvar命令擷取支援的環境變數列表。
- 退出Monkey
quit
完全退出Monkey
Monkey不再接受新的串連。
- 結束當前會話
done
結束當前會話。
Monkey還可以接受新的串連。
- 休眠
sleep ms
使Monkey進入休眠一段時間,參數為整數,單位毫秒
- 注釋
#
以#開頭的語句會被Monkey當做注釋。發送這樣的命令Monkey不會返回錯誤也不會返回OK。
常用函數介紹
以下列表提煉於源碼MonkeySourceScript.java
DispatchKey(downTime,eventTime,action,code,repeat,metaState,device,scancode)
@param long downTime //鍵最初被按下的時間
@param long eventTime //事件發生的時間
@param int action //(ACTION_DOWN=0,ACTION_UP=1,ACTION_MULTIPLE=2)
@param int code //索引值,比如KEYCODE_DPAD_DOWN(20)
@param int repeat //
@param int metaState //當前按下的meta鍵的標識
@param int device //事件發生的裝置id
@param int scancode //
DispatchPointer(downTime, eventTime,action, x, y, pressure, size, metaState, xPrecision,yPrecision,device, edgeFlags)
@param long downTime //鍵最初被按下的時間
@param long eventTime //事件發生的時間
@param int action //(ACTION_DOWN=0,ACTION_MOVE=1,ACTION_UP=2,ACTION_CANCEL=3)
@param float x //x座標
@param float y //y座標
@param float pressure //當前事件的壓力,範圍0-1
@param float size //觸摸的近似值,範圍0-1
@param int metaState //當前按下的meta鍵的標識
@param float xPrecision //x座標精確值
@param float yPrecision //y座標精確值
@param int device //事件來源,範圍0-x,0表示不來自物理裝置
@param int sedgeFlags //
DispatchTrackball(downTime, eventTime,action, x, y, pressure, size, metaState, xPrecision,yPrecision,device, edgeFlags)
Tap(x,y,duration);
DispatchPress(String key_name)
DispatchFlip(boolean keyboardOpen)
UserWait(long sleeptime)
LaunchActivity(String pkg_name, String cl_name,long alarmTime)
UserWait(long sleeptime)
LongPress()
樣本:
- Start Script
type = user
count = 49
speed = 1.0
start data >>
LaunchActivity(com.android.browser,com.android.browser.BrowserActivity)
UserWait(5000)
#back to home
captureDispatchPointer(5109520,5109520,0,1150,330,0,0,0,0,0,0,0);
captureDispatchPointer(5109521,5109521,1,1150,330,0,0,0,0,0,0,0);
UserWait(3000)
#close browser
captureDispatchPointer(5109520,5109520,0,205,31,0,0,0,0,0,0,0);
captureDispatchPointer(5109521,5109521,1,205,31,0,0,0,0,0,0,0);
UserWait(2000)
以上是一個樣本指令碼。
具體的操作步驟為:
- 將上述指令碼複製到script.txt中
- 將script.txt放到sd卡根目錄
adb shell monkey –v –v –v –f /sdcard/script.txt –throttle 1500 100 > monkey.txt
指令碼會按照script.txt發送的指令序列每隔1.5s執行一個指令,執行100遍,並將log存在monkey.txt檔案中
Monkey測試結果分析
Monkey測試的延伸
monkey工具不能實現指定模組測試,以下記錄兩種實現指定模組測試的方法
方法1:
from com.android.monkeyrunner import MonkeyRunner, MonkeyDevice
from random import randint
print("get device")
device = MonkeyRunner.waitForConnection()
package = ‘com.swl.a1vod‘
activity = ‘org.coolx.videoframework.vod.VodDetailsActivity‘
runComponent = package + ‘/‘ + activity
device.startActivity(component=runComponent)
#use commands like device.touch and device.drag to simulate a navigation and open my activity
#with your activity opened start your monkey test
print("start monkey test")
for i in range(1, 1000):
#here i go emulate only simple touchs, but i can emulate swiper keyevents and more... :D
device.touch(randint(0, 1000), randint(0, 800), ‘DOWN_AND_UP‘)
print("end monkey test")
#執行monkeyrunner G:\script\monkeytest.py
方法2:
<activity android:name="MonkeyActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.MONKEY" />
</intent-filter>
</activity>
adb shell monkey -p my.package -c android.intent.category.MONKEY -v 500
更多參考資料
1、Monkey
2、android 常見死機問題–log分析
3、分析bugreport
4、如何分析解決Android ANR
5、Android Tombstone Crash的log分析和定位
6、Android中對Log記錄檔的分析
7、Watchdog kills system service in system_server
8、android ANR異常及其解決方式
monkey測試===Android測試載入器Monkey用法簡介(轉載)