monkey专题系列:
1.monkey命令详解
2.android monkey 测试方法实践
上一篇文章monkey测试方法实践中,已经说过monkey隐藏的两个命令,–pck-blacklist-file<黑名单文件>,–pck-whitelist-file<白名单文件>,monkey还有一个隐藏的命令那就是:
–f<脚本文件>:可以指定monkey的自定义脚本
一般monkey测试都是随机的测试事件,假如我们需要测试某一个特定的场景,普通的命令很难办到,这是就是需要自定义脚本这个命令了,它的使用方式也是很简单方便的,只要我们按照monkey的脚本规范写好脚本,之后将它保存到手机中,然后运行monkey的时候使用-f 命令指定脚本就行。
monkey自定义脚本格式:
monkey的源码MonkeySourceScript.java类中有一段关于monkey脚本规则的描述MonkeySourceScript.java有兴趣的同学可以去看一下这个源码,我们看一下源码对脚本规则的描述:
/*** monkey event queue. It takes a script to produce events sample script format:** <pre>* type= raw events* count= 10* speed= 1.0* start data >>* captureDispatchPointer(5109520,5109520,0,230.75429,458.1814,0.20784314,0.06666667,0,0.0,0.0,65539,0)* captureDispatchKey(5113146,5113146,0,20,0,0,0,0)* captureDispatchFlip(true)* ...* </pre>*/复制代码
解释他们的意思
#声明脚本的类型type= raw events#脚本的执行次数,由于monkey运行次数在命令设置的,这里的设置是无效的count= 10#事件执行时间间隔,这个也是通过命令来设置,这里不起作用speed= 1.0#脚本的入口,说明脚本从下面开始执行start data >>#下全是monkey命令captureDispatchPointer(5109520,5109520,0,230.75429,458.1814,0.20784314,0.06666667,0,0.0,0.0,65539,0)captureDispatchKey(5113146,5113146,0,20,0,0,0,0)captureDispatchFlip(true)复制代码
知道了monkey脚本书写的基本格式,在来看一眼monkey常见脚本API
1.LaunchActivity(包名,Activity名称)
启动被测应用的某个Activity
2.TAp(x,y,TapDuration)
一次单击事件
x,y:是坐标
TapDuration:持续的时间,单位毫秒
3.DispatchPress(keyName)
模拟一个按键
keyName:按键的名称
4.RotateScreen(RotationDegree,peresist)
旋转屏幕
RotationDegree:0~3分别表示顺时针的四个方向
peresist:是否存留
5.DispatchFiip(true/false)
打开或关闭软键盘
6.LongPress()长按2秒
7.PressAndHold(x,y,PressDuration)
模拟长按事件
x,y:点击的坐标
PressDuration:持续时常,单位毫秒
8.DispatchString(input)
输入字符串
input:输入的内容
9.Drag(xStart,yStart,xEnd,yEnd,pCount)
模拟拖动操作
xStart,yStart:开始的坐标值
xEnd,yEnd:结束的坐标值
pCount:滑动事件的次数
10.PinchZoom(pt1xStart,pt1yStart,pt1xEnd,pt1yEnd,pt2xStart,pt2ystart,pt2xEnd,pt2yEnd)
手势缩放事件
pt1xStart,pt1yStart:第一个手指起始的坐标位置
pt1xEnd,pt1yEnd:第一个手指结束的坐标位置
pt2xStart,pt2ystart:第二个手指开始的坐标位置
pt2xEnd,pt2yEnd:第二个手指结束的坐标位置
11.UserWait(sleepTime)
设置睡眠时间
sleepTime:等待时间,单位毫秒
12.DeviceWake()
唤醒设备屏幕monkey 脚本的事例
type = usercount = 1speed = 500start data >>#启动应用程序LaunchActivity(com.michael.words,com.michael.words.activity.SplashActivity)UserWait(5000)#点击下步按钮Tap(615,609)UserWait(1000)#点击编辑框Tap(200,85,1000)#输入sgDispatchString(sg)复制代码
将文件保存为monkey.script文件,之后将脚本push到手机里,然后执行命令
monkey -f /sdcard/monkey.script -v 1
效果截图
执行后打印的log信息如下:
shell@mako:/ $ monkey -f /sdcard/monkey.script -v 1monkey -f /sdcard/monkey.script -v 1WARNING: linker: app_process has text relocations. This is wasting memory and isa security risk. Please fix.WARNING: linker: app_process has text relocations. This is wasting memory and isa security risk. Please fix.:Monkey: seed=1496304348065 count=1:IncludeCategory: android.intent.category.LAUNCHER:IncludeCategory: android.intent.category.MONKEYReplaying 0 events with speed 1.0:Switch: #Intent;action=android.intent.action.MAIN;category=android.intent.category.LAUNCHER;launchFlags=0x10200000;component=com.michael.words/.activity.SplashActivity;end// Allowing start of Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] cmp=com.michael.words/.activity.SplashActivity } in package com.michael.words// Allowing start of Intent { cmp=com.michael.words/.activity.ConfigActivity} in package com.michael.words// activityResuming(com.michael.words):Sending Touch (ACTION_DOWN): 0:(615.0,516.0):Sending Touch (ACTION_UP): 0:(615.0,516.0)// Allowing start of Intent { cmp=com.michael.words/.activity.MainActivity }in package com.michael.words// activityResuming(com.michael.words):Sending Touch (ACTION_DOWN): 0:(200.0,85.0):Sending Touch (ACTION_UP): 0:(200.0,85.0)// Shell command input text sg status was 0Events injected: 9:Sending rotation degree=0, persist=false:Dropped: keys=0 pointers=0 trackballs=0 flips=0 rotations=0## Network stats: elapsed time=8030ms (0ms mobile, 0ms wifi, 8030ms not connected)// Monkey finished复制代码