Android monkey test 脚本的编写


Android 的 monkey test 工具提供了 -f scriptfile 参数,可以指定 test 脚本,然而翻遍了 Android 的网站也没有找到这个脚本的文档,最终只在 monkey 的源码 MonkeySourceScript.java 中找到了一小段注释,里面给了一个不到 10 行例子:

  1. /** 
  2.  * monkey event queue. It takes a script to produce events 
  3.  *  
  4.  * sample script format: 
  5.  *      type= raw events 
  6.  *      count= 10 
  7.  *      speed= 1.0 
  8.  *      start data >> 
  9.  *      captureDispatchPointer(5109520,5109520,0,230.75429,458.1814,0.20784314, 
  10.  *          0.06666667,0,0.0,0.0,65539,0) 
  11.  *      captureDispatchKey(5113146,5113146,0,20,0,0,0,0) 
  12.  *      captureDispatchFlip(true) 
  13.  *      ... 
  14.  */  
有了这个例子之后稍微好办了些,至少有搜索的关键词了,于是用 captureDispatchPointer 很快就找到了一篇自动化测试的文章,(以下是引用)
  1.  "DispatchPointer";"DispatchTrackball";"DispatchKey";"DispatchFlip";这几个最主要的函数  
  2. 按键事件:参见android.view KeyEvent.java  
  3. DispatchKey(downTime, //@param: The time (in {@link android.os.SystemClock#uptimeMillis}) at which this key code originally went down.毫秒  
  4. eventTime,     //at which this event happened.                     
  5. action,        //Action code: either {@link #ACTION_DOWN=0}, {@link #ACTION_UP=1}, or {@link #ACTION_MULTIPLE=2}.  
  6. code,    //The key code. 见附录1, 比如KEYCODE_DPAD_DOWN(20)KEYCODE_DPAD_UP(19)  
  7. repeat, //A repeat count for down events (> 0 if this is after the initial down) or event count for multiple events.  
  8. metaState, //Flags indicating which meta keys are currently pressed.  
  9. device, //The device ID that generated the key event.  
  10. scancode) //Raw device scan code of the event.  
  11.   
  12. DispatchPointer,DispatchTrackball(downTime, eventTime,  
  13.                         action, x, y, pressure, size, metaState, xPrecision, yPrecision,  
  14.                         device, edgeFlags);  
  15. @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}.  
  16. @param x The X coordinate of this event.  
  17. @param y The Y coordinate of this event.  
  18. @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.  
  19. @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.  
  20. @param metaState The state of any meta / modifier keys that were in effect when the event was generated.  
  21. @param xPrecision The precision of the X coordinate being reported.  
  22. @param yPrecision The precision of the Y coordinate being reported.  
  23. @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.  
  24. @param edgeFlags A bitfield indicating which edges, if any, where touched by this MotionEvent  
  25. captureDispatchFlip(true)是否打开滑盖,true是打开  
剩下具体每个函数的用法,就只能在源码里面慢慢发掘了。另外搜索到一个例子:
  1. # Start of Script  
  2. type= user  
  3. count= 49  
  4. speed= 1.0  
  5. start data >>  
  6. LaunchActivity(com.mpowerlabs.coin.android, com.mpowerlabs.coin.android.LoginActivity)  
  7. # 3120021258  
  8. DispatchPress(KEYCODE_3)  
  9. UserWait(200)  
  10. DispatchPress(KEYCODE_1)  
  11. UserWait(200)  
  12. DispatchPress(KEYCODE_3)  
  13. UserWait(200)  
  14. DispatchPress(KEYCODE_5)  
  15. UserWait(200)  
  16. DispatchPress(KEYCODE_0)  
  17. UserWait(200)  
  18. DispatchPress(KEYCODE_2)  
  19. UserWait(200)  
  20. DispatchPress(KEYCODE_1)  
  21. UserWait(200)  
  22. DispatchPress(KEYCODE_2)  
  23. UserWait(200)  
  24. DispatchPress(KEYCODE_5)  
  25. UserWait(200)  
  26. DispatchPress(KEYCODE_8)  
  27. UserWait(200)  
  28. # Pin 12345  
  29. DispatchPress(KEYCODE_DPAD_DOWN)  
  30. UserWait(250)  
  31. DispatchPress(KEYCODE_1)  
  32. UserWait(200)  
  33. DispatchPress(KEYCODE_2)  
  34. UserWait(200)  
  35. DispatchPress(KEYCODE_3)  
  36. UserWait(200)  
  37. DispatchPress(KEYCODE_4)  
  38. UserWait(200)  
  39. DispatchPress(KEYCODE_5)  
  40. UserWait(200)  
  41. # Down and enter  
  42. DispatchPress(KEYCODE_DPAD_DOWN)  
  43. UserWait(250)  
  44. DispatchPress(KEYCODE_ENTER)  
用法:

adb shell monkey -f <script file> <运行脚本的次数>

例如,我们放一个��本到 /sdcard/monkey.script,然后运行:

adb shell monkey -f /sdcard/monkey.script 10,那么这个脚本里面指定的动作就会被执行10次。

整理的脚本函数列表:

  1. DispatchPointer(long downTime,  long eventTime, int action,  
  2.   
  3.     float x, float y, float pressure, float size, int metaState,  
  4.   
  5.     float xPrecision, float yPrecision, int device, int edgeFlags)  
  6.   
  7. DispatchTrackball(long downTime,  long eventTime, int action,  
  8.   
  9.     float x, float y, float pressure, float size, int metaState,  
  10.   
  11.     float xPrecision, float yPrecision, int device, int edgeFlags)  
  12.   
  13. DispatchKey(long downTime, long eventTime, int action, int code,  
  14.   
  15.     int repeat, int metaState, int device, int scancode)  
  16.   
  17. DispatchFlip(boolean keyboardOpen)  
  18.   
  19. DispatchPress(int keyCode)  
  20.   
  21. LaunchActivity(String pkg_name, String cl_name)  
  22.   
  23. UserWait(long sleeptime)  
  24.   
  25. LongPress()  
键值列表参见 http://developer.android.com/reference/android/view/KeyEvent.html。

相关内容