Monkey Test 2(脚本方式)

本文深入解析Android的Monkey测试工具及其使用方法,包括参数解释、脚本编写、执行流程和示例应用,并提供实际操作指导。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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,还需要继续研究。有收获的朋友一定要通知哦^_^

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值