Monkey Test 2(脚本方式)

来源:互联网 发布:及时生成字幕软件 编辑:程序博客网 时间:2024/06/17 15:09

Android 的monkey test 工具提供了 -f scriptfile 参数,可以指定 test 脚本。在 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.  */  

monkey中提供的函数如下:

  1. 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)  
  2. 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)    
  3. DispatchKey(long downTime, long eventTime, int action, int code, int repeat, int metaState, int device, int scancode)     
  4. DispatchFlip(boolean keyboardOpen)    
  5. DispatchPress(int keyCode)    
  6. LaunchActivity(String pkg_name, String cl_name)  
  7. UserWait(long sleeptime)    
  8. LongPress(int keyCode) 

首先本地编写需要的测试的事件 命名为 monkey.script (文件格式无要求),将文件push到手机或模拟器的sdcard中

如: adb push  lujing/monkey.script  /sdcard/

然后执行脚本:

       adb shell monkey -v -f /sdcard/monkey.script

 

附 Example:

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值是必填项

 

另外,如何通过LaunchActivity启动带参数的activity,还需要继续研究。有收获的朋友一定要通知哦^_^

0 0
原创粉丝点击