本文共 3381 字,大约阅读时间需要 11 分钟。
我们对Android的monkey test的认识,很多情况只是用它做随机压力测试,实际上Android的monkey test 工具提供了 -f scriptfile -v times参数,可以通过特定api调用模拟滑屏、点击、缩放等操作定制test 脚本。但官方文档没有对这有相关说明,结合百度的一些文档和通过阅读它的源码对于它的使用说明梳理如下:
monkey 的源码 MonkeySourceScript.java 中有一小段注释,里面给了简单使用例子:
源码地址:
实例:
使用说明:
1.脚本为txt脚本
2.脚本编写如范例,type 为脚本的说明,count为默认脚本执行次数,speed为间隔时间
3.运行脚本方法,把脚本push到手机,然后 ”adb shell monkey -f 脚本地址 -v 测试次数“ 即可运行脚本
Example: 将附件,放到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)
单点事件
DispatchPointer(downTime,eventTime,action,x,y,pressure,size,metaStat,xPrecision,yPrecision,device,edgeFlags)- downTime, //touchdown的时间
- eventTime, //touch时间发生的时间
- action, //Action code: either {@link #ACTION_DOWN=0}, {@link #ACTION_UP=1}, or {@link #ACTION_MULTIPLE=2}. 如果时间是0,2,1可以模拟滑屏
- x, //The X coordinate of this event.
- y, //The Y coordinate of this event.
- 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.
- 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.
- metaStatex //The state of any meta / modifier keys that were in effect when the event was generated.
- xPrecision, //The precision of the X coordinate being reported.
- yPrecision, //The precision of the Y coordinate being reported.
- 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.
- 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:
双指缩放
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的值可以根据对指定keycode模拟press事件
DispatchPress(KeyCode) 以前可以滑动出现键盘的手机模拟滑盖操作 DispatchFlip(keyboardOpen) 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转载地址:https://blog.csdn.net/Danlinan/article/details/40859101 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!