monkey script相关试用说明

我们对Android的monkey test的认识,很多情况只是用它做随机压力测试,实际上Android的monkey test 工具提供了 -f scriptfile -v times参数,可以通过特定api调用模拟滑屏、点击、缩放等操作定制test 脚本。但官方文档没有对这有相关说明,结合百度的一些文档和通过阅读它的源码对于它的使用说明梳理如下:

 monkey 的源码 MonkeySourceScript.java 中有一小段注释,里面给了简单使用例子:

源码地址:https://android.googlesource.com/platform/development/+/android-4.1.1_r3/cmds/monkey/src/com/android/commands/monkey/

实例:

 

使用说明:

1.脚本为txt脚本

2.脚本编写如范例,type 为脚本的说明,count为默认脚本执行次数,speed为间隔时间

3.运行脚本方法,把脚本push到手机,然后 ”adb shell monkey -f 脚本地址 -v 测试次数“ 即可运行脚本

  Example: 将附件test.txt,放到sdcard下,adb shell monkey -f /sdcard/test.txt -v 1 脚本会运行一次,做指定坐标的touch事件,长按和缩放 ,定制这样的脚本可以在diy_monkey脚本中插入,就可以模拟访问特定页面后点击二级链接,缩放页面等操作.

 

功能优势:

可以做一些特殊操作的定制,实现如滑屏,缩放,点击,长按等操作,同时也支持shell命令运行,更好的命中我们的测试场景。

 

常用的api整理说明如下:

LaunchActivity(pkg_name,cl_name)/LaunchActivity(pkg_name,cl_name,alarmTime)

//启动应用,alarmTime定时器

 

UserWait(sleeptime)
sleeptime:ms为单位的时间

 

单点事件
DispatchPointer(downTime,eventTime,action,x,y,pressure,size,metaStat,xPrecision,yPrecision,device,edgeFlags)

  1. downTime,       //touchdown的时间
     
  2. eventTime,     //touch时间发生的时间                
  3. action,        //Action code: either {@link #ACTION_DOWN=0}, {@link #ACTION_UP=1}, or {@link #ACTION_MULTIPLE=2}.  如果时间是0,2,1可以模拟滑屏
  4.  x,  //The X coordinate of this event.  
  5.  y,  //The Y coordinate of this event.  
  6. 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.  
  7.  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.  
  8. metaStatex //The state of any meta / modifier keys that were in effect when the event was generated.  
  9.  xPrecision,  //The precision of the X coordinate being reported.  
  10.  yPrecision,  //The precision of the Y coordinate being reported.  
  11.  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.  
  12.  edgeFlags,  // A bitfield indicating which edges, if any, where touched by this MotionEvent 

example:

DispatchPointer(0,0,0,830,1000,0,0,0,0,0,0,0); touchDown

DispatchPointer(0,0,1,830,1000,0,0,0,0,0,0,0);touchUp


Drag(x1,y1,x2,y2,stepCount)

//拖动

example:

快速下滑:Drag(300,500,300,300,15)          快速上滑:Drag(300,300,300,500,15)


双指缩放

PinchZoom(xstart1,ystart1,xstart2,ystart2,xend2,yend2,xend1,yend1,step)

example:

放大:PinchZoom(400,400,200,300,550,550,700,700,3);


RunCmd(cmd)

//cmd: shell命令

example:RunCmd(monkey -v 1000)


Tap(x,y,tapDuration)

//发送tap事件(一个touchdown和touch up事件),时间长可以模拟长按,时间单位为ms


ProfileWait()

//等待5S


DeviceWakeUp()

//启动唤醒设备com.google.android.powerutil/com.google.android.powerutil.WakeUpScreen


LongPress()

//获取最后一个touch时间的点做长按


PressAndHold(x,y,duration)

//按住duration时长


//屏幕旋转
RotateScreen(rotationDegree,persist)
rotationDegree只能支持0,90,180,270
persist 0/1


//对指定keycode模拟touch事件 ,基本所有android内置的按键都有对应的keycode,如音量键啊,音频啊。。。。这个是个很好用的东西
DispatchKey(downTime,eventTime,action,code,repeat,metaState,device,scancode)
code的值可以根据http://developer.android.com/reference/android/view/KeyEvent.html查找


对指定keycode模拟press事件
DispatchPress(KeyCode)

以前可以滑动出现键盘的手机模拟滑盖操作
DispatchFlip(keyboardOpen)
keyboardOpen:0/1

LaunchInstrumentation(test_name,runner_name)
//调起Instrumentation


模拟轨迹球事件

DispatchTrackball(downTime,eventTime,action,x,y,pressure,size,metaStat,xPrecision,yPrecision,device,edgeFlags)


PowerLog(power_log_type)/PoweLog(power_log_type,test_case_status)
//待研究

WriteLog()
//将电池日志写到sdcard



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值