本文章转载于搜狗测试
作为移动端测试必须掌握的初级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_search10000
运行效果:
附件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 -v1000)
#发送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按键Home3
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_ESCAPEESC键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_TABTab键61
KEYCODE_NUM_LOCK小键盘锁143
KEYCODE_CAPS_LOCK大写锁定键115
KEYCODE_BREAKBreak/Pause键121
KEYCODE_SCROLL_LOCK滚动锁定键116
KEYCODE_ZOOM_IN放大键168
KEYCODE_ZOOM_OUT缩小键169