使用script檔去作android monkey 測試

Monkey是Android系统固件自带的性能测试工具,他可以模拟各种按键、触屏、轨迹球、activity等事件。

Android官方正式介紹Monkey說明於此, 但今天比較想著墨於script的使用, 有了script就可以把要讓android作的測試都寫在裡面

—–

先看看指令裡怎麼說明的 (PS: 以下說明為adb使用於Windows作業系統平台)

C:\Windows\System32>adb shell monkey --help
** Error: Unknown option: --help
usage: monkey [-p ALLOWED_PACKAGE [-p ALLOWED_PACKAGE] ...]
              [-c MAIN_CATEGORY [-c MAIN_CATEGORY] ...]
              [--ignore-crashes] [--ignore-timeouts]
              [--ignore-security-exceptions]
              [--monitor-native-crashes] [--ignore-native-crashes]
              [--kill-process-after-error] [--hprof]
              [--pct-touch PERCENT] [--pct-motion PERCENT]
              [--pct-trackball PERCENT] [--pct-syskeys PERCENT]
              [--pct-nav PERCENT] [--pct-majornav PERCENT]
              [--pct-appswitch PERCENT] [--pct-flip PERCENT]
              [--pct-anyevent PERCENT] [--pct-pinchzoom PERCENT]
              [--pkg-blacklist-file PACKAGE_BLACKLIST_FILE]
              [--pkg-whitelist-file PACKAGE_WHITELIST_FILE]
              [--wait-dbg] [--dbg-no-events]
              [--setup scriptfile] [-f scriptfile [-f scriptfile] ...]
              [--port port]
              [-s SEED] [-v [-v] ...]
              [--throttle MILLISEC] [--randomize-throttle]
              [--profile-wait MILLISEC]
              [--device-sleep-time MILLISEC]
              [--randomize-script]
              [--script-log]
              [--bugreport]
              [--periodic-bugreport]
              COUNT

運行monkey可以採用兩種方式:系統默認(Default)方式和script方式

一、默認(Default)運行方式:

# adb shell monkey -p package.name -v 30
其中: 可以繼續添加一個或者兩個-v 參數, -v參數越多,輸出的日誌越詳細; 最後的數字表示,觸發的事件次數

# adb shell monkey -p package.name -v 30 > log.txt
為了更好的查看日誌,可以將輸出的日誌信息重定向到文件中

二、腳本方式

Android 的monkey test 工具提供了-f scriptfile 參數,可以指定test 腳本。
在monkey 的源碼MonkeySourceScript.java 中有一小段註釋,
檔案位置 development/cmds/monkey/src/com/android/commands/monkey/MonkeySourceScript.java

裡面給了一個不到10 行例子:

/**
 * monkey event queue. It takes a script to produce events sample script format:
 *
 * type= raw events
 * count= 10
 * speed0
 * start data >>
 * 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中提供的函數如下:

DispatchPointer(long downTime, long eventTime, int action, float x, float y, float pressure, float size, int metaState, float xPrecision, float yPrecision, int device, int edgeFlags)
DispatchTrackball(long downTime, long eventTime, int action, float x, float y, float pressure, float size, int metaState, float xPrecision, float yPrecision, int device, int edgeFlags)
DispatchKey(long downTime, long eventTime, int action, int code, int repeat, int metaState, int device, int scancode)
DispatchFlip(boolean keyboardOpen)
DispatchPress(int keyCode)
LaunchActivity(String pkg_name, String cl_name)
UserWait(long sleeptime)
LongPress(int keyCode)

首先本地編寫需要的測試的事件命名為monkey.script (文件格式無要求)

將文件push到手機或模擬器的sdcard中
# adb push monkey.script /sdcard/
然後執行腳本:
# adb shell monkey -v -f /sdcard/monkey.script

附 Example1:

type= user
count= 49
speed= 1.0
start data >>
LaunchActivity(com.example.android.notepad, com.example.android.notepad.NotesList)
DispatchPress(KEYCODE_DPAD_DOWN)
LongPress(KEYCODE_DOWN)
DispatchPress(KEYCODE_BACK)

其中type值可以任意,源碼中沒有對該值做任何處理。
count值,在此無效,還是需要在命令行輸入需要執行的次數。因為命令行的count值是必填項

附 Example2: OnOffWlan.script
要開關wifi的觸控步驟..

type=raw events
count= 1
speed= 1.0
start data >>
DispatchPress(KEYCODE_HOME)
captureUserWait ( 1000 )
captureDispatchPointer( 459755113000,459755113000, 0, 546, 34,0.20784314,0.06666667,0,0.0,0.0,65539,0)
captureUserWait ( 1000 )
captureDispatchPointer( 459755113000,459755113000, 2, 534, 1481,0.20784314,0.06666667,0,0.0,0.0,65539,0)
captureDispatchPointer( 459755113000,459755113000, 1, 534, 1481,0.20784314,0.06666667,0,0.0,0.0,65539,0)
captureUserWait ( 1000 )
captureDispatchPointer( 11368012663000,11368171949000, 0, 1001, 65,0.20784314,0.06666667,0,0.0,0.0,65539,0)
captureDispatchPointer( 11368012663000,11368171949000, 1, 1001, 65,0.20784314,0.06666667,0,0.0,0.0,65539,0)
captureUserWait ( 1000 )
captureDispatchPointer( 459755113000,459755113000, 0, 181, 695,0.20784314,0.06666667,0,0.0,0.0,65539,0)
captureUserWait ( 2000 )
captureDispatchPointer( 459755113000,459755113000, 1, 181, 695,0.20784314,0.06666667,0,0.0,0.0,65539,0)
captureUserWait ( 3000 )
DispatchPress(KEYCODE_BACK)
captureDispatchPointer( 459755113000,459755113000, 0, 546, 34,0.20784314,0.06666667,0,0.0,0.0,65539,0)
captureUserWait ( 1000 )
captureDispatchPointer( 459755113000,459755113000, 2, 534, 1481,0.20784314,0.06666667,0,0.0,0.0,65539,0)
captureDispatchPointer( 459755113000,459755113000, 1, 534, 1481,0.20784314,0.06666667,0,0.0,0.0,65539,0)
captureUserWait ( 1000 )
captureDispatchPointer( 11368012663000,11368171949000, 0, 1001, 65,0.20784314,0.06666667,0,0.0,0.0,65539,0)
captureDispatchPointer( 11368012663000,11368171949000, 1, 1001, 65,0.20784314,0.06666667,0,0.0,0.0,65539,0)
captureUserWait ( 1000 )
captureDispatchPointer( 459755113000,459755113000, 0, 181, 695,0.20784314,0.06666667,0,0.0,0.0,65539,0)
captureUserWait ( 2000 )
captureDispatchPointer( 459755113000,459755113000, 1, 181, 695,0.20784314,0.06666667,0,0.0,0.0,65539,0)
captureUserWait ( 3000 )
DispatchPress(KEYCODE_BACK)

以下為Example2執行時的 LOG:
看這個LOG 你就可以知道如何設定touch的點, Action是Up/Down/Move
就可以模擬真人使用的步驟, 當然.要算好(x, y)的座標…
指令 #adb shell monkey -v -f /storage/sdcard0/Download/OnOffWlan.script 100
表示要顯示log(-v), 使用script(-f), 運行100次

C:\> adb shell monkey -v -f /storage/sdcard0/Download/OnOffWlan.script  100
:Monkey: seed=1409796926274 count=100
:IncludeCategory: android.intent.category.LAUNCHER
:IncludeCategory: android.intent.category.MONKEY
Replaying 1 events with speed 1.0
    // Allowing start of Intent { act=android.intent.action.MAIN cat=[android.intent.category.HOME] cmp=com.android.launcher/com.android.launcher2.Launcher } in package com.android.launcher
    // Allowing start of Intent { act=android.intent.action.CHOOSER cmp=android/com.android.internal.app.ChooserActivity } in package android
:Sending Touch (ACTION_DOWN): 0:(546.0,34.0)
:Sending Touch (ACTION_MOVE): 0:(534.0,1481.0)
:Sending Touch (ACTION_UP): 0:(534.0,1481.0)
:Sending Touch (ACTION_DOWN): 0:(1001.0,65.0)
:Sending Touch (ACTION_UP): 0:(1001.0,65.0)
:Sending Touch (ACTION_DOWN): 0:(181.0,695.0)
    // Allowing start of Intent { act=android.intent.action.MAIN cat=[android.intent.category.HOME] cmp=com.android.launcher/com.android.launcher2.Launcher } in package com.android.launcher
:Sending Touch (ACTION_UP): 0:(181.0,695.0)
:Sending Touch (ACTION_DOWN): 0:(546.0,34.0)
:Sending Touch (ACTION_MOVE): 0:(534.0,1481.0)
:Sending Touch (ACTION_UP): 0:(534.0,1481.0)
:Sending Touch (ACTION_DOWN): 0:(1001.0,65.0)
:Sending Touch (ACTION_UP): 0:(1001.0,65.0)
:Sending Touch (ACTION_DOWN): 0:(181.0,695.0)
    // Allowing start of Intent { act=android.intent.action.CHOOSER cmp=android/com.android.internal.app.ChooserActivity } in package android
    // activityResuming(com.android.launcher)
:Sending Touch (ACTION_UP): 0:(181.0,695.0)

reference:
Monkey Script
Android自动化测试 monkey 工具学习 系列5

發表迴響