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
    评论
Monkey工具是一个用于Android系统的压力测试工具,它可以模拟用户的随机来测试应用程序的稳定性和性能。以下是使用Monkey工具的一般步骤: 1. 安装Android SDK和配置环境变量: - 下载并安装Android SDK。 - 配置环境变量,确保可以在命令行中访问到Android SDK的工具和平台工具。 2. 连接设备或启动模拟器: - 使用USB连接一台Android设备,或者启动Android模拟器。 - 确保设备或模拟器已成功连接,并可以通过adb命令进行通信。 3. 执行Monkey命令: - 打开命令行终端,并切换到Android SDK的工具目录。 - 使用以下命令执行Monkey测试: ``` adb shell monkey [options] <event_count> ``` 其中,`<event_count>`表示要执行的随机事件数量,`[options]`表示可选的参数,如设置随机数种子、设置事件百分比等。 4. 分析测试结果: - Monkey工具会自动执行一系列随机事件,如点击、滑动、按键等。 - 在测试完成后,可以根据Monkey工具的输出结果来评估应用程序的稳定性和性能。 - 分析日志和错误报告,查找应用程序崩溃、ANR(Application Not Responding)等问题。 Monkey工具的使用需要注意以下几点: - 由于Monkey工具是随机操作,可能会导致意外情况,建议在开发环境下使用,避免对正式发布的应用进行测试。 - 在执行Monkey测试前,最好先备份应用程序数据,以免测试过程中造成数据丢失。 - 可以根据具体需求调整Monkey工具的参数,如事件百分比、随机数种子等,以模拟更符合实际使用情况的场景。 请注意,在实际使用Monkey工具之前,请确保已充分了解其使用方法和潜在影响,并谨慎操作以避免不必要的损失。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值