monkey自定義指令碼實踐

maweiliang發表於2017-05-19

monkey專題系列:
1.monkey命令詳解
2.android monkey 測試方法實踐
上一篇文章monkey測試方法實踐中,已經說過monkey隱藏的兩個命令,–pck-blacklist-file<黑名單檔案>,–pck-whitelist-file<白名單檔案>,monkey還有一個隱藏的命令那就是:
–f<指令碼檔案>:可以指定monkey的自定義指令碼
一般monkey測試都是隨機的測試事件,假如我們需要測試某一個特定的場景,普通的命令很難辦到,這是就是需要自定義指令碼這個命令了,它的使用方式也是很簡單方便的,只要我們按照monkey的指令碼規範寫好指令碼,之後將它儲存到手機中,然後執行monkey的時候使用-f 命令指定指令碼就行。

  • monkey自定義指令碼格式:
    monkey的原始碼MonkeySourceScript.java類中有一段關於monkey指令碼規則的描述MonkeySourceScript.java有興趣的同學可以去看一下這個原始碼,我們看一下原始碼對指令碼規則的描述:
    /**
    * monkey event queue. It takes a script to produce events sample script format:
    *
    * <pre>
    * type= raw events
    * count= 10
    * speed= 1.0
    * start data &gt;&gt;
    * 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)
    * ...
    * </pre>
    */複製程式碼
    解釋他們的意思
    #宣告指令碼的型別
    type= raw events
    #指令碼的執行次數,由於monkey執行次數在命令設定的,這裡的設定是無效的
    count= 10
    #事件執行時間間隔,這個也是通過命令來設定,這裡不起作用
    speed= 1.0
    #指令碼的入口,說明指令碼從下面開始執行
    start data &gt;&gt;
    #下全是monkey命令
    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)複製程式碼
    知道了monkey指令碼書寫的基本格式,在來看一眼monkey常見指令碼API
    1.LaunchActivity(包名,Activity名稱)
    啟動被測應用的某個Activity
    2.TAp(x,y,TapDuration)
    一次單擊事件
    x,y:是座標
    TapDuration:持續的時間,單位毫秒
    3.DispatchPress(keyName)
    模擬一個按鍵
    keyName:按鍵的名稱
    4.RotateScreen(RotationDegree,peresist)
    旋轉螢幕
    RotationDegree:0~3分別表示順時針的四個方向
    peresist:是否存留
    5.DispatchFiip(true/false)
    開啟或關閉軟鍵盤
    6.LongPress()長按2秒
    7.PressAndHold(x,y,PressDuration)
    模擬長按事件
    x,y:點選的座標
    PressDuration:持續時常,單位毫秒
    8.DispatchString(input)
    輸入字串
    input:輸入的內容
    9.Drag(xStart,yStart,xEnd,yEnd,pCount)
    模擬拖動操作
    xStart,yStart:開始的座標值
    xEnd,yEnd:結束的座標值
    pCount:滑動事件的次數
    10.PinchZoom(pt1xStart,pt1yStart,pt1xEnd,pt1yEnd,pt2xStart,pt2ystart,pt2xEnd,pt2yEnd)
    手勢縮放事件
    pt1xStart,pt1yStart:第一個手指起始的座標位置
    pt1xEnd,pt1yEnd:第一個手指結束的座標位置
    pt2xStart,pt2ystart:第二個手指開始的座標位置
    pt2xEnd,pt2yEnd:第二個手指結束的座標位置
    11.UserWait(sleepTime)
    設定睡眠時間
    sleepTime:等待時間,單位毫秒
    12.DeviceWake()
    喚醒裝置螢幕
  • monkey 指令碼的事例
type = user
count = 1
speed = 500
start data >>
#啟動應用程式
LaunchActivity(com.michael.words,com.michael.words.activity.SplashActivity)
UserWait(5000)
#點選下步按鈕
Tap(615,609)
UserWait(1000)
#點選編輯框
Tap(200,85,1000)
#輸入sg
DispatchString(sg)複製程式碼

將檔案儲存為monkey.script檔案,之後將指令碼push到手機裡,然後執行命令
monkey -f /sdcard/monkey.script -v 1
效果截圖

monkey自定義指令碼實踐
monkey自定義指令碼實踐

執行後列印的log資訊如下:

shell@mako:/ $ monkey -f /sdcard/monkey.script -v 1
monkey -f /sdcard/monkey.script -v 1
WARNING: linker: app_process has text relocations. This is wasting memory and is
 a security risk. Please fix.
WARNING: linker: app_process has text relocations. This is wasting memory and is
 a security risk. Please fix.
:Monkey: seed=1496304348065 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.categ
ory.LAUNCHER;launchFlags=0x10200000;component=com.michael.words/.activity.Splash
Activity;end
    // Allowing start of Intent { act=android.intent.action.MAIN cat=[android.in
tent.category.LAUNCHER] cmp=com.michael.words/.activity.SplashActivity } in pack
age com.michael.words
    // Allowing start of Intent { cmp=com.michael.words/.activity.ConfigActivity
 } in package com.michael.words
    // activityResuming(com.michael.words)
:Sending Touch (ACTION_DOWN): 0:(615.0,516.0)
:Sending Touch (ACTION_UP): 0:(615.0,516.0)
    // Allowing start of Intent { cmp=com.michael.words/.activity.MainActivity }
 in package com.michael.words
    // activityResuming(com.michael.words)
:Sending Touch (ACTION_DOWN): 0:(200.0,85.0)
:Sending Touch (ACTION_UP): 0:(200.0,85.0)
// Shell command input text sg status was 0
Events injected: 9
:Sending rotation degree=0, persist=false
:Dropped: keys=0 pointers=0 trackballs=0 flips=0 rotations=0
## Network stats: elapsed time=8030ms (0ms mobile, 0ms wifi, 8030ms not connecte
d)
// Monkey finished複製程式碼

相關文章