作為移動端測試必須掌握的初級Android穩定性工具:monkey,提到它時,腦海裡一般湧現出兩句話:
1.我會用,很簡單
就是一行命令,一回車就開始跑起來了
2.使用問題多,不好用
太隨機,很多操作沒意義
達到深層頁面的概率極低
雖然加了各種忽略異常,但是monkey程式還是經常死掉
無法有針對性(指定頁面or指定操作)的測試
等等帶著這些問題,將更智慧的monkey介紹給大家
智慧monkey之monkeyscript(一)
解決問題:
有針對性的指定操作進行穩定性測試
需求(參考搜狗搜尋APP):
啟動app,搜尋1次,退出。重複1萬次
Script:
type=user
count=10
speed=1.0
start data >>
LaunchActivity(com.sogou.activity.src, com.sogou.search.entry.EntryActivity)
UserWait(3000)
captureDispatchPointer(10,10,0,500,500,1,1,-1,1,1,0,0)
captureDispatchPointer(10,10,1,500,500,1,1,-1,1,1,0,0)
UserWait(500)
captureDispatchString(test)
UserWait(500)
captureDispatchPress(66)
UserWait(3000)
Drag(500,1500,500,500,500)
captureDispatchPress(4)
UserWait(1000)
captureDispatchPress(4)
captureDispatchPress(4)
script逐行解釋:
type=user
count=10
speed=1.0
start data >>
#固定頭部,引數和值均不影響指令碼
LaunchActivity(com.sogou.activity.src, com.sogou.search.entry.EntryActivity)
#啟動app,引數1:包名,引數2:主activity名
UserWait(1500)
#等待1500毫秒
captureDispatchPointer(10,10,0,500,500,1,1,-1,1,1,0,0)
#按下座標500,500(搜尋框位置)
captureDispatchPointer(10,10,1,500,500,1,1,-1,1,1,0,0)
#抬起座標500,500(搜尋框位置)
UserWait(500)
#等待500毫秒
captureDispatchString(test)
#在搜尋框輸入“test”
captureDispatchPress(66)
#輸入回車進行搜尋
UserWait(3000)
#等待3000毫秒
Drag(500,1500,500,500,500)
#上劃瀏覽搜尋結果頁
captureDispatchPress(4)
#輸入back回退至app首頁
UserWait(500)
等待500毫秒
captureDispatchPress(4)
captureDispatchPress(4)
#雙back退出app
執行:
1.將指令碼儲存命名(例test_search)
2.將指令碼檔案放置測試機中
adb shell mkdir /sdcard/script
#建立指令碼資料夾
adb push test_search /sdcard/script
#將指令碼檔案push到資料夾
3.執行指令碼檔案1萬次
adb shell monkey -f /sdcard/script/test_search 10000
執行效果:
附件1
Monkeyscript API介紹
#單點事件
DispatchPointer(downTime,eventTime,action,x,y,pressure,size,metaStat,xPrecision,yPrecision,device,edgeFlags)
downTime,//touchdown的時間
eventTime,//touch時間發生的時間
action,//Action code: either {@link #ACTION_DOWN=0}, {@link #ACTION_UP=1}, or {@link #ACTION_MULTIPLE=2}. 如果時間是0,2,1可以模擬滑屏
x, //The X coordinate of this event.
y, //The Y coordinate of this event.
pressure, //The current pressure of this event. The pressure generally ranges from 0 (no pressure at all) to 1 (normal pressure), however values higher than 1 may be generated depending on the calibration of the input device.
size, //A scaled value of the approximate size of the area being pressed touched with the finger. The actual value in pixels corresponding to the finger touch is normalized with a device specific range of values and scaled to a value between 0 and 1.
metaStatex //The state of any meta / modifier keys that were in effect when the event was generated.
xPrecision, //The precision of the X coordinate being reported.
yPrecision, //The precision of the Y coordinate being reported.
deviceId, //The id for the device that this event came from. An id of zero indicates that the event didn't come from a physical device; other numbers are arbitrary and you shouldn't depend on the values.
edgeFlags, // A bitfield indicating which edges, if any, where touched by this MotionEvent
example:
DispatchPointer(0,0,0,830,1000,0,0,0,0,0,0,0); touchDown
DispatchPointer(0,0,1,830,1000,0,0,0,0,0,0,0);touchUp
#拖動
Drag(x1,y1,x2,y2,stepCount)
example:
快速下滑:Drag(300,500,300,300,15)
快速上滑:Drag(300,300,300,500,15)
#雙指縮放
PinchZoom(xstart1,ystart1,xstart2,ystart2,xend2,yend2,xend1,yend1,step)
example:
#放大
PinchZoom(400,400,200,300,550,550,700,700,3);
#cmd: shell命令
RunCmd(cmd)
example
RunCmd(monkey -v 1000)
#傳送tap事件(一個touchdown和touch up事件),時間長可以模擬長按,時間單位為ms
Tap(x,y,tapDuration)
#啟動喚醒裝置
DeviceWakeUp()
#獲取最後一個touch時間的點做長按
LongPress()
#按住duration時長
PressAndHold(x,y,duration)
#螢幕旋轉
RotateScreen(rotationDegree,persist)
// rotationDegree只能支援0,90,180,270
// persist 0/1
// 對指定keycode模擬touch事件(keycode列表見尾部)
DispatchKey(downTime,eventTime,action,code,repeat,metaState,device,scancode)
// 對指定keycode模擬press事件
DispatchPress(KeyCode)
KEYCODE列表
電話鍵
鍵名 | 描述 | 鍵值 |
---|---|---|
KEYCODE_CALL | 撥號鍵 | 5 |
KEYCODE_ENDCALL | 掛機鍵 | 6 |
KEYCODE_HOME | 按鍵Home | 3 |
KEYCODE_MENU | 選單鍵 | 82 |
KEYCODE_BACK | 返回鍵 | 4 |
KEYCODE_SEARCH | 搜尋鍵 | 84 |
KEYCODE_CAMERA | 拍照鍵 | 27 |
KEYCODE_FOCUS | 拍照對焦鍵 | 80 |
KEYCODE_POWER | 電源鍵 | 26 |
KEYCODE_NOTIFICATION | 通知鍵 | 83 |
KEYCODE_MUTE | 話筒靜音鍵 | 91 |
KEYCODE_VOLUME_MUTE | 揚聲器靜音鍵 | 164 |
KEYCODE_VOLUME_UP | 音量增加鍵 | 24 |
KEYCODE_VOLUME_DOWN | 音量減小鍵 | 25 |
控制鍵
鍵名 | 描述 | 鍵值 |
---|---|---|
KEYCODE_ENTER | Enter鍵 | 66 |
KEYCODE_ESCAPE | ESC鍵 | 111 |
KEYCODE_DPAD_CENTER | 導航鍵 確定鍵 | 23 |
KEYCODE_DPAD_UP | 導航鍵 向上 | 19 |
KEYCODE_DPAD_DOWN | 導航鍵 向下 | 20 |
KEYCODE_DPAD_LEFT | 導航鍵 向左 | 21 |
KEYCODE_DPAD_RIGHT | 導航鍵 向右 | 22 |
KEYCODE_MOVE_HOME | 游標移動到開始鍵 | 122 |
KEYCODE_MOVE_END | 游標移動到末尾鍵 | 123 |
KEYCODE_PAGE_UP | 向上翻頁鍵 | 92 |
KEYCODE_PAGE_DOWN | 向下翻頁鍵 | 93 |
KEYCODE_DEL | 退格鍵 | 67 |
KEYCODE_FORWARD_DEL | 刪除鍵 | 112 |
KEYCODE_INSERT | 插入鍵 | 124 |
KEYCODE_TAB | Tab鍵 | 61 |
KEYCODE_NUM_LOCK | 小鍵盤鎖 | 143 |
KEYCODE_CAPS_LOCK | 大寫鎖定鍵 | 115 |
KEYCODE_BREAK | Break/Pause鍵 | 121 |
KEYCODE_SCROLL_LOCK | 滾動鎖定鍵 | 116 |
KEYCODE_ZOOM_IN | 放大鍵 | 168 |
KEYCODE_ZOOM_OUT | 縮小鍵 | 169 |
注:本文轉自搜狗測試,如有侵權,請第一時間聯絡我進行刪除。