知乎首發(fā)地址:https://zhuanlan.zhihu.com/p/134158584
常規(guī)Monkey測試執(zhí)行的是隨機(jī)的事件流,但如果只是想讓Monkey測試某個(gè)特定場景這時(shí)候就需要用到自定義腳本了,Monkey支持執(zhí)行用戶自定義腳本的測試,用戶只需要按照Monkey腳本的規(guī)范編寫好腳本,存放到手機(jī)上库快,啟動(dòng)Monkey通過-f 參數(shù)調(diào)用腳本即可。
一、Monkey腳本API方法
LaunchActivity(pkg_name, cl_name):啟動(dòng)應(yīng)用江解,參數(shù):包名和Activity
Tap(x, y, tapDuration): 模擬一次手指單擊事件,參數(shù):x,y為控件坐標(biāo)徙歼,tapDuration為點(diǎn)擊的持續(xù)時(shí)間犁河,此參數(shù)可省略
DispatchPress(keyName): 按鍵鳖枕。參數(shù): keycode。 詳細(xì)的Android Keycode列表:?http://blog.csdn.net/feizhixuan46789/article/details/16801429
RotateScreen(rotationDegree, persist): 旋轉(zhuǎn)屏幕桨螺,參數(shù):rotationDegree為旋轉(zhuǎn)角度宾符,1代表90度;persist表示旋轉(zhuǎn)之后是否固定灭翔,0表示旋轉(zhuǎn)后恢復(fù)魏烫,非0則表示固定不變。
DispatchFlip(true/false): 打開或者關(guān)閉軟鍵盤
LongPress(2000): 長按2秒
PressAndHold(x, y, pressDuration): 模擬長按事件
DispatchString(input): 輸入字符串
Drag(xStart, yStart, xEnd, yEnd, stepCount): 用于模擬一個(gè)拖拽操作
PinchZoom(x1Start, y1Start, x1End, y1End, x2Start, y2Start, x2End, y2End, stepCount): 模擬縮放手勢
UserWait(sleepTime): 休眠一段時(shí)間
DeviceWakeUp(): 喚醒屏幕
RunCmd(cmd): 運(yùn)行shell命令
DispatchPointer(downtime,eventTime,action,x,yxpressure,size,metastate,xPrecision,yPrecision,device,edgeFlags): 向指定位置肝箱,發(fā)送單個(gè)手勢
DispatchPointer(downtime,eventTime,action,x,yxpressure,size,metastate,xPrecision,yPrecision,device,edgeFilags): 發(fā)送按鍵消息
LaunchInstrumentation(test_name,runner_name): 運(yùn)行instrumentation測試用例哄褒。
DispatchTrackball: 模擬發(fā)送軌跡球事件
ProfileWait(5000): 等待5秒
StartCaptureFramerate(): 獲取幀率
EndCaptureFramerate(input): 結(jié)束獲取幀率
二、monkey自定義腳本實(shí)戰(zhàn)
1煌张、測試案例
啟動(dòng)QQ呐赡,然后點(diǎn)擊登錄按鈕進(jìn)入登錄頁面,在登錄界面輸入用戶名骏融、密碼链嘀,點(diǎn)擊登錄。
2档玻、搭建模擬器環(huán)境
在itools或者夜神模擬器中管闷,安裝QQ最新版本,檢查是否連接ADB窃肠,并獲取App包名包个。
C:\Users\chang>adb devices
List of devices attached
127.0.0.1:56001 device
#獲取App的包名,先啟動(dòng)qq并獲取焦點(diǎn)
adb shell dumpsys window | findstr mCurrentFocus
3冤留、腳本開發(fā)
Monkey腳本只能通過坐標(biāo)的方式來定位點(diǎn)擊和移動(dòng)事件的屏幕位置碧囊,這里就需要提前獲取坐標(biāo)信息。獲取坐標(biāo)信息的方法很多纤怒,最簡單的方法就是打開手機(jī)中的開發(fā)人員選項(xiàng)糯而,打開“顯示指針位置”。隨后泊窘,在屏幕上的每次操作熄驼,在導(dǎo)航欄上都會(huì)顯示坐標(biāo)信息
Monkey腳本主要包含兩部分,一部分是頭文件信息烘豹,一部分是具體的monkey命令瓜贾。
將下面腳本復(fù)制到e:\qq.txt文件中。
#頭文件信息
type = raw events
count = 1
speed = 1.0
#啟動(dòng)測試
start data >>
#啟動(dòng)應(yīng)用的Activity
LaunchActivity(com.tencent.mobileqq,com.tencent.mobileqq.activity.LoginActivity)
UserWait(2000)
Tap(379,822,200) #點(diǎn)擊登錄按鈕
UserWait(2000)
Tap(249,278,1000) #點(diǎn)擊用戶名文本框
DispatchString(2878228288)#輸入qq號(hào)碼
UserWait(2000)
Tap(228,381,1000) #點(diǎn)擊用戶名輸入框
DispatchString(zxw1234)
UserWait(2000)
Tap(248,539,1000) #點(diǎn)擊登錄按鈕
UserWait(2000)
4携悯、執(zhí)行腳本
先將腳本文件發(fā)送到移動(dòng)設(shè)備上祭芦,然后調(diào)用adb shell monkey指令執(zhí)行腳本
#將編寫好的腳本push到模擬器的/data目錄下
adb push e:\qq.txt /data
#調(diào)用monkey指令執(zhí)行腳本
adb -s 127.0.0.1:56001 shell monkey -f /data/qq.txt -v 1
5、查看執(zhí)行結(jié)果
C:\Users\chang>adb -s 127.0.0.1:56001 shell monkey -f /data/qq.txt -v 1
:Monkey: seed=1588719579198 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.tencent.mobileqq/.activity.LoginActivity;end
? ? // Allowing start of Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] cmp=com.tencent.mobileqq/.activity.LoginActivity } in package com.tencent.mobileqq
:Sending Touch (ACTION_DOWN): 0:(379.0,822.0)
:Sending Touch (ACTION_UP): 0:(379.0,822.0)
:Sending Touch (ACTION_DOWN): 0:(249.0,278.0)
:Sending Touch (ACTION_UP): 0:(249.0,278.0)
// Shell command input text zxw1234 status was 0
:Sending Touch (ACTION_DOWN): 0:(228.0,381.0)
:Sending Touch (ACTION_UP): 0:(228.0,381.0)
// Shell command input text zxw1234 status was 0
:Sending Touch (ACTION_DOWN): 0:(248.0,539.0)
:Sending Touch (ACTION_UP): 0:(248.0,539.0)
Events injected: 20
:Sending rotation degree=0, persist=false
:Dropped: keys=0 pointers=0 trackballs=0 flips=0 rotations=0
## Network stats: elapsed time=13890ms (0ms mobile, 0ms wifi, 13890ms not connected)
// Monkey finished
歡迎關(guān)注作者憔鬼,如果覺得寫的還不錯(cuò)龟劲,就給點(diǎn)個(gè)贊同胃夏、喜歡、收藏(后續(xù)持續(xù)更新)昌跌。
【全文手打 如需轉(zhuǎn)載 請(qǐng)標(biāo)注此出處】