本文章轉(zhuǎn)載于搜狗測試
作為移動端測試必須掌握的初級Android穩(wěn)定性工具:monkey,提到它時,腦海里一般涌現(xiàn)出兩句話:
1.我會用,很簡單
就是一行命令,一回車就開始跑起來了
2.使用問題多,不好用
太隨機(jī),很多操作沒意義
達(dá)到深層頁面的概率極低
雖然加了各種忽略異常,但是monkey進(jìn)程還是經(jīng)常死掉
無法有針對性(指定頁面or指定操作)的測試
等等
帶著這些問題,將更智能的monkey介紹給大家
智能monkey之monkeyscript(一)
解決問題:
有針對性的指定操作進(jìn)行穩(wěn)定性測試
需求(參考搜狗搜索APP):
啟動app,搜索1次,退出。重復(fù)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 >>
#固定頭部,參數(shù)和值均不影響腳本
LaunchActivity(com.sogou.activity.src, com.sogou.search.entry.EntryActivity)
#啟動app,參數(shù)1:包名,參數(shù)2:主activity名
UserWait(1500)
#等待1500毫秒
captureDispatchPointer(10,10,0,500,500,1,1,-1,1,1,0,0)
#按下坐標(biāo)500,500(搜索框位置)
captureDispatchPointer(10,10,1,500,500,1,1,-1,1,1,0,0)
#抬起坐標(biāo)500,500(搜索框位置)
UserWait(500)
#等待500毫秒
captureDispatchString(test)
#在搜索框輸入“test”
captureDispatchPress(66)
#輸入回車進(jìn)行搜索
UserWait(3000)
#等待3000毫秒
Drag(500,1500,500,500,500)
#上劃瀏覽搜索結(jié)果頁
captureDispatchPress(4)
#輸入back回退至app首頁
UserWait(500)
等待500毫秒
captureDispatchPress(4)
captureDispatchPress(4)
#雙back退出app
運(yùn)行:
1.將腳本保存命名(例test_search)
2.將腳本文件放置測試機(jī)中
adb shell mkdir /sdcard/script
#創(chuàng)建腳本文件夾
adb push test_search /sdcard/script
#將腳本文件push到文件夾
3.運(yùn)行腳本文件1萬次
adb shell monkey -f /sdcard/script/test_search10000
運(yùn)行效果:
附件1
Monkeyscript API介紹
#單點事件
DispatchPointer(downTime,eventTime,action,x,y,pressure,size,metaStat,xPrecision,yPrecision,device,edgeFlags)
downTime,//touchdown的時間
eventTime,//touch時間發(fā)生的時間
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 -v1000)
#發(fā)送tap事件(一個touchdown和touch up事件),時間長可以模擬長按,時間單位為ms
Tap(x,y,tapDuration)
#啟動喚醒設(shè)備
DeviceWakeUp()
#獲取最后一個touch時間的點做長按
LongPress()
#按住duration時長
PressAndHold(x,y,duration)
#屏幕旋轉(zhuǎn)
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掛機(jī)鍵6
KEYCODE_HOME按鍵Home3
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揚(yáng)聲器靜音鍵164
KEYCODE_VOLUME_UP音量增加鍵24
KEYCODE_VOLUME_DOWN音量減小鍵25
控制鍵
鍵名描述鍵值
KEYCODE_ENTER回車鍵66
KEYCODE_ESCAPEESC鍵111
KEYCODE_DPAD_CENTER導(dǎo)航鍵 確定鍵23
KEYCODE_DPAD_UP導(dǎo)航鍵 向上19
KEYCODE_DPAD_DOWN導(dǎo)航鍵 向下20
KEYCODE_DPAD_LEFT導(dǎo)航鍵 向左21
KEYCODE_DPAD_RIGHT導(dǎo)航鍵 向右22
KEYCODE_MOVE_HOME光標(biāo)移動到開始鍵122
KEYCODE_MOVE_END光標(biāo)移動到末尾鍵123
KEYCODE_PAGE_UP向上翻頁鍵92
KEYCODE_PAGE_DOWN向下翻頁鍵93
KEYCODE_DEL退格鍵67
KEYCODE_FORWARD_DEL刪除鍵112
KEYCODE_INSERT插入鍵124
KEYCODE_TABTab鍵61
KEYCODE_NUM_LOCK小鍵盤鎖143
KEYCODE_CAPS_LOCK大寫鎖定鍵115
KEYCODE_BREAKBreak/Pause鍵121
KEYCODE_SCROLL_LOCK滾動鎖定鍵116
KEYCODE_ZOOM_IN放大鍵168
KEYCODE_ZOOM_OUT縮小鍵169