LaunchActivity(pkg_name, cl_name):啓動應用的Activity。參數:包名和啓動的Activity。java
Tap(x, y, tapDuration): 模擬一次手指單擊事件。參數:x,y爲控件座標,tapDuration爲點擊的持續時間,此參數可省略。android
UserWait(sleepTime): 休眠一段時間shell
DispatchPress(keyName): 按鍵。參數: keycode。 RotateScreen(rotationDegree, persist): 旋轉屏幕。 參數:rotationDegree爲旋轉角度, e.g. 1表明90度;persist表示旋轉以後是否固定,0表示旋轉後恢復,非0則表示固定不變。測試
DispatchString(input): 輸入字符串。ui
DispatchFlip(true/false): 打開或者關閉軟鍵盤。spa
PressAndHold(x, y, pressDuration): 模擬長按事件。code
Drag(xStart, yStart, xEnd, yEnd, stepCount): 用於模擬一個拖拽操做。component
PinchZoom(x1Start, y1Start, x1End, y1End, x2Start, y2Start, x2End, y2End, stepCount): 模擬縮放手勢。orm
LongPress(): 長按2秒。事件
DeviceWakeUp(): 喚醒屏幕。
PowerLog(power_log_type, test_case_status): 模擬電池電量信息。
WriteLog(): 將電池信息寫入sd卡。
RunCmd(cmd): 運行shell命令。
DispatchPointer(downtime,eventTime,action,x,yxpressure,size,metastate,xPrecision,yPrecision,device,edgeFlags): 向指定位置,發送單個手勢。
DispatchPointer(downtime,eventTime,action,x,yxpressure,size,metastate,xPrecision,yPrecision,device,edgeFilags): 發送按鍵消息。
LaunchInstrumentation(test_name,runner_name): 運行一個instrumentation測試用例。
DispatchTrackball: 模擬發送軌跡球事件。
ProfileWait: 等待5秒。
StartCaptureFramerate(): 獲取幀率。
EndCaptureFramerate(input): 結束獲取幀率。
Monkey腳本主要包含兩部分,一部分是頭文件信息,一部分是具體的monkey命令。
type = raw events
count = 1
speed = 1.0
//下面爲monkey命令
start data >>
具體的monkey腳本內容
kyb.txt
#頭文件信息
type = raw events
count = 1
speed = 1.0
#啓動測試
start data >>
LaunchActivity(com.tal.kaoyan,com.tal.kaoyan.ui.activity.SplashActivity)
UserWait(2000)
Tap(624,900,1000) #點擊取消升級
UserWait(2000)
Tap(806,64,1000) #點擊跳過
UserWait(2000)
Tap(217,378,1000) #點擊用戶名輸入框
DispatchString(zxw1234)
UserWait(2000)
Tap(197,461,1000) #點擊密碼輸入框
DispatchString(zxw123456)
UserWait(2000)
Tap(343,637,1000) #點擊登陸按鈕
腳本編寫完成後,傳到手機設備上,而後執行。
adb push C:\Users\Shuqing\Desktop\kyb1.txt /sdcard
adb shell monkey -f /sdcard/kyb1.txt -v 1
執行結果
C:\Users\Shuqing>adb shell monkey -f /sdcard/kyb.txt -v 1
:Monkey: seed=1524592021303 count=1
:IncludeCategory: android.intent.category.LAUNCHER
:IncludeCategory: android.intent.category.MONKEY
Replaying 0 events with speed 1.0
:Switch: #Intent;action=android.intent.action.MAIN;category=android.intent.category.LAUNCHER;launchFlags=0x10200000;component=com.tal.kaoyan/.ui.activity.SplashActivity;end
// Allowing start of Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] cmp=com.tal.kaoyan/.ui.activity.SplashActivity } in package com.tal.kaoyan
:Sending Touch (ACTION_DOWN): 0:(267.0,1233.0)
// Allowing start of Intent { act=com.android.systemui.recent.action.TOGGLE_RECENTS cmp=com.android.systemui/.recent.RecentsActivity } in package com.android.systemui
:Sending Touch (ACTION_UP): 0:(267.0,1233.0)
Events injected: 5
:Sending rotation degree=0, persist=false
:Dropped: keys=0 pointers=0 trackballs=0 flips=0 rotations=0
## Network stats: elapsed time=7201ms (0ms mobile, 0ms wifi, 7201ms not connected)
// Monkey finished
頭文件代碼書寫注意「=」兩邊預留空格,不然會出現以下報錯。
java.lang.NumberFormatException: Invalid int: ""