monkeyscript

作为移动端测试必须掌握的初级Android稳定性工具:monkey,提到它时,脑海里一般涌现出两句话:

1.我会用,很简单

就是一行命令,一回车就开始跑起来了

2.使用问题多,不好用

太随机,很多操作没意义 
达到深层页面的概率极低 
虽然加了各种忽略异常,但是monkey进程还是经常死掉 
无法有针对性(指定页面or指定操作)的测试 
等等

带着这些问题,将更智能的monkey介绍给大家

智能monkey之monkeyscript(一)

解决问题:

有针对性的指定操作进行稳定性测试

需求(参考搜狗搜索APP):

启动app,搜索1次,退出。重复1万次

Script:
type=user
count=10
speed=1.0
start data >>
LaunchActivity(com.sogou.activity.src, com.sogou.search.entry.EntryActivity)
UserWait(3000)
captureDispatchPointer(10,10,0,500,500,1,1,-1,1,1,0,0)
captureDispatchPointer(10,10,1,500,500,1,1,-1,1,1,0,0)
UserWait(500)
captureDispatchString(test)
UserWait(500)
captureDispatchPress(66)
UserWait(3000)
Drag(500,1500,500,500,500)
captureDispatchPress(4)
UserWait(1000)
captureDispatchPress(4)
captureDispatchPress(4)

script逐行解释:

type=user
count=10
speed=1.0
start data >>
#固定头部,参数和值均不影响脚本
LaunchActivity(com.sogou.activity.src, com.sogou.search.entry.EntryActivity)
#启动app,参数1:包名,参数2:主activity名
UserWait(1500)
#等待1500毫秒
captureDispatchPointer(10,10,0,500,500,1,1,-1,1,1,0,0)
#按下坐标500,500(搜索框位置)

captureDispatchPointer(10,10,1,500,500,1,1,-1,1,1,0,0)
#抬起坐标500,500(搜索框位置)

UserWait(500)
#等待500毫秒
captureDispatchString(test)
#在搜索框输入“test”

captureDispatchPress(66)
#输入回车进行搜索

UserWait(3000)
#等待3000毫秒
Drag(500,1500,500,500,500)
#上划浏览搜索结果页

captureDispatchPress(4)
#输入back回退至app首页

UserWait(500)
等待500毫秒

captureDispatchPress(4)
captureDispatchPress(4)
#双back退出app
运行:

1.将脚本保存命名(例test_search) 
2.将脚本文件放置测试机中

adb shell mkdir /sdcard/script
#创建脚本文件夹
adb push test_search /sdcard/script
#将脚本文件push到文件夹

3.运行脚本文件1万次

adb shell monkey -f /sdcard/script/test_search 10000
运行效果:

附件1

Monkeyscript API介绍

#单点事件
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:

快速下滑: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);
#cmd: shell命令

RunCmd(cmd)

example

RunCmd(monkey -v 1000)
#发送tap事件(一个touchdown和touch up事件),时间长可以模拟长按,时间单位为ms

Tap(x,y,tapDuration)
#启动唤醒设备

DeviceWakeUp()
#获取最后一个touch时间的点做长按

LongPress()
#按住duration时长

PressAndHold(x,y,duration)
#屏幕旋转
RotateScreen(rotationDegree,persist)
// rotationDegree只能支持0,90,180,270
// persist 0/1
// 对指定keycode模拟touch事件(keycode列表见尾部)
DispatchKey(downTime,eventTime,action,code,repeat,metaState,device,scancode)
// 对指定keycode模拟press事件
DispatchPress(KeyCode)
KEYCODE列表

电话键

键名描述键值
KEYCODE_CALL 拨号键 5
KEYCODE_ENDCALL 挂机键 6
KEYCODE_HOME 按键Home 3
KEYCODE_MENU 菜单键 82
KEYCODE_BACK 返回键 4
KEYCODE_SEARCH 搜索键 84
KEYCODE_CAMERA 拍照键 27
KEYCODE_FOCUS 拍照对焦键 80
KEYCODE_POWER 电源键 26
KEYCODE_NOTIFICATION 通知键 83
KEYCODE_MUTE 话筒静音键 91
KEYCODE_VOLUME_MUTE 扬声器静音键 164
KEYCODE_VOLUME_UP 音量增加键 24
KEYCODE_VOLUME_DOWN 音量减小键 25

控制键

键名描述键值
KEYCODE_ENTER 回车键 66
KEYCODE_ESCAPE ESC键 111
KEYCODE_DPAD_CENTER 导航键 确定键 23
KEYCODE_DPAD_UP 导航键 向上 19
KEYCODE_DPAD_DOWN 导航键 向下 20
KEYCODE_DPAD_LEFT 导航键 向左 21
KEYCODE_DPAD_RIGHT 导航键 向右 22
KEYCODE_MOVE_HOME 光标移动到开始键 122
KEYCODE_MOVE_END 光标移动到末尾键 123
KEYCODE_PAGE_UP 向上翻页键 92
KEYCODE_PAGE_DOWN 向下翻页键 93
KEYCODE_DEL 退格键 67
KEYCODE_FORWARD_DEL 删除键 112
KEYCODE_INSERT 插入键 124
KEYCODE_TAB Tab键 61
KEYCODE_NUM_LOCK 小键盘锁 143
KEYCODE_CAPS_LOCK 大写锁定键 115
KEYCODE_BREAK Break/Pause键 121
KEYCODE_SCROLL_LOCK 滚动锁定键 116
KEYCODE_ZOOM_IN 放大键 168
KEYCODE_ZOOM_OUT 缩小键 169

注:本文转自搜狗测试,如有侵权,请第一时间联系我进行删除。

原文地址:https://www.cnblogs.com/YatHo/p/7205162.html