Android monkey test 指令碼的編寫

來源:互聯網
上載者:User

Android 的 monkey test 工具提供了 -f scriptfile 參數,可以指定 test 指令碼,然而翻遍了 Android 的網站也沒有找到這個指令碼的文檔,最終只在 monkey 的源碼 MonkeySourceScript.java 中找到了一小段注釋,裡面給了一個不到 10 行例子:

/** * monkey event queue. It takes a script to produce events *  * sample script format: *      type= raw events *      count= 10 *      speed= 1.0 *      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) *      ... */

有了這個例子之後稍微好辦了些,至少有搜尋的關鍵詞了,於是用 captureDispatchPointer 很快就找到了一篇自動化測試的文章:

http://qatesttech.wordpress.com/category/android-monkey-script/

(以下是引用)

 "DispatchPointer";"DispatchTrackball";"DispatchKey";"DispatchFlip";這幾個最主要的函數按鍵事件:參見android.view KeyEvent.javaDispatchKey(downTime, //@param: The time (in {@link android.os.SystemClock#uptimeMillis}) at which this key code originally went down.毫秒eventTime,     //at which this event happened.                   action,        //Action code: either {@link #ACTION_DOWN=0}, {@link #ACTION_UP=1}, or {@link #ACTION_MULTIPLE=2}.code,    //The key code. 見附錄1, 比如KEYCODE_DPAD_DOWN(20)KEYCODE_DPAD_UP(19)repeat, //A repeat count for down events (> 0 if this is after the initial down) or event count for multiple events.metaState, //Flags indicating which meta keys are currently pressed.device, //The device ID that generated the key event.scancode) //Raw device scan code of the event.DispatchPointer,DispatchTrackball(downTime, eventTime,                        action, x, y, pressure, size, metaState, xPrecision, yPrecision,                        device, edgeFlags);@action The kind of action being performed -- one of either{@link #ACTION_DOWN=0}, {@link #ACTION_MOVE=1}, {@link #ACTION_UP=2}, or{@link #ACTION_CANCEL=3}.@param x The X coordinate of this event.@param y The Y coordinate of this event.@param 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.@param 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.@param metaState The state of any meta / modifier keys that were in effect when the event was generated.@param xPrecision The precision of the X coordinate being reported.@param yPrecision The precision of the Y coordinate being reported.@param 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.@param edgeFlags A bitfield indicating which edges, if any, where touched by this MotionEventcaptureDispatchFlip(true)是否開啟滑蓋,true是開啟

剩下具體每個函數的用法,就只能在源碼裡面慢慢發掘了。另外搜尋到一個例子:

http://qatesttech.wordpress.com/2011/05/05/android-my-monkey-script/

# Start of Scripttype= usercount= 49speed= 1.0start data >>LaunchActivity(com.mpowerlabs.coin.android, com.mpowerlabs.coin.android.LoginActivity)# 3120021258DispatchPress(KEYCODE_3)UserWait(200)DispatchPress(KEYCODE_1)UserWait(200)DispatchPress(KEYCODE_3)UserWait(200)DispatchPress(KEYCODE_5)UserWait(200)DispatchPress(KEYCODE_0)UserWait(200)DispatchPress(KEYCODE_2)UserWait(200)DispatchPress(KEYCODE_1)UserWait(200)DispatchPress(KEYCODE_2)UserWait(200)DispatchPress(KEYCODE_5)UserWait(200)DispatchPress(KEYCODE_8)UserWait(200)# Pin 12345DispatchPress(KEYCODE_DPAD_DOWN)UserWait(250)DispatchPress(KEYCODE_1)UserWait(200)DispatchPress(KEYCODE_2)UserWait(200)DispatchPress(KEYCODE_3)UserWait(200)DispatchPress(KEYCODE_4)UserWait(200)DispatchPress(KEYCODE_5)UserWait(200)# Down and enterDispatchPress(KEYCODE_DPAD_DOWN)UserWait(250)DispatchPress(KEYCODE_ENTER)

用法:

adb shell monkey -f <script file> <運行指令碼的次數>

例如,我們放一個指令碼到 /sdcard/monkey.script,然後運行:

adb shell monkey -f /sdcard/monkey.script 10,那麼這個指令碼裡面指定的動作就會被執行10次。

整理的指令碼函數列表:

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()

索引值列表參見 http://developer.android.com/reference/android/view/KeyEvent.html。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.