monkey &&monkeyrunner在测试中的应用



1、android app压力测试


什么时候开展压力测试?
功能测试稳定后


2、monkey和monkeyrunner的区别?
monkey:在adb shell中,生成用户或系统的伪随机事件
monkeyrunner:通过api定义特定命令和事件控制设备


3、monkeyrunner apis
monkeyrunner:用来连接设备或模拟器
monkeydevice:提供安装、卸载应用,发送模拟事件
monkeyimage:完成图像保存,及对比的操作


4、monkeyruner的测试类型
多设备控制
功能测试
回归测试


5、压力测试结果


1、crash:即崩溃,即应用程序在使用过程中,非正常退出
2、ANR:application not responding


环境准备
android sdk
python


adb shell monkey -v -p com.jm.jiediancabinet -s 100 - --pct-touch 100 --throttle 1000 -ignore-crash --ignore-timeouts 1000
测试过程:
1、android调试勾选
2、确认android设备和电脑已经成功 连接(adb devices)
3、安装测试app
adb install package.apk
4、发送压力指令
adb shell monkey 1000
5、获取app包名
adb logcat|grep START


6、给指定包打压力
adb shell monkey -p package 1000


monkey高级参数的应用


1、throttle参数
指定事件之间的间隔
adb shell monkey --throttle <millseconds>


2、seed参数
指定随机生成数的seed值


adb shell monkey -s <seed> <event-count>


3、触摸事件
设定触摸事件百分比
adb shell monkey --pct-touch <percent>


4、设定动作事件
设定动作事件百分比
adb shell monkey --pct-motion <percent>


5、轨迹球事件
设定轨迹球事件百分比
adb shell monkey --pct-trackball <percent>


6、基本导航事件
设定基本导航事件百分比,输入设备的上下左右
adb shell monkey --pct-nav <percent>
7、主要导航事件
设定主要导航事件百分比,兼容中间键、返回键、菜单按键
adb shell monkey --pct-maornav <percent>
8、系统导航事件
设定系统导航事件百分比,HOME、BACK、拨号及音量键
adb shell monkey --pct-syskeys <percent>


9、启动activity事件
设定启动activity的事件百分比
adb shell monkey --pct-appswitch <percent>


10、不常用事件
设定不常用事件的百分比
adb shell monkey --pct-anyevent <percent>


11、崩溃事件
忽略奔溃和异常
adb shell monkey --ignore-crash <event-count>


12、超时事件
忽略超时事件
adb shell monkey --ignore-timeouts <event-count>


monkey script


执行monkey脚本的命令:
adb shell monkey -f <scriptfile><event-count>




脚本常用命令 
1、DispatchTrackball命令
轨迹球事件


DispatchTrackball(long downtime,long eventide,int action,float x,float y,float pressure,float size,int metastate,float xprecision,float yprecision,int device,int edgeflags)


action 0代表按下,1代表弹起,x和y代表的坐标点
2、DispatchPointer命令
点击事件
DispatchPointer(long downtime,long eventide,int action,float x,float y,float pressure,float size,int metastate,float xprecision,float yprecision,int device,int edgeflags)


action 0代表按下,1代表弹起,x和y代表的坐标点


3、DispatchString命令
输入字符串事件
DispatchString(String text)


4、LaunchActivity命令
启动应用
LaunchActivity(package,Activity)


5、UserWait命令
等待事件
UserWait(1000)


6、DispatchPress命令
按下键值
DispatchPress(int keycode)#keycode 66 回车键


monkeyRunner



1、monkeyrunner api--alert
警告框
void alert(string message,string title,string okTitle)


2、monkeyrunner api--waitForConnection
等待设备连接,有多个device id,需要指明具体哪个设备
waitForConnection(float imeout,string deviceid)


3、monkeydevice api--drag
拖动
drag(tuple start,tuple end,float duration,integer steps)
start起点位置,
end终点位置
duration手势持续的时间
steps插值点的步数,默认是10


4、monkeydevice api--press
按键
press(string keycode,dictionary type)
keycode名:down,up ,down_and_up


5、monkeydevice api--startActivity
启动应用
startActivity(package+'/'+activity)


6、monkeydevice api--touch
点击
touch(integer x,integer y,integer type)


x坐标值,y坐标值
type:down ,up ,down_and_up




7、monkeydevice api--type
输入
type(string message)


8、monkeydevice api--takeSnapshot
截屏
MonkeyImage takeSnapshot()


9、MonkeyImage API--sameAs
图像对比
boolean sameAs(MonkeyImage other,float percent)




10、MonkeyImage API--writetOFile
保存图像文件
void writeToFile(string path,string format)



monkeyruner应用

1、通过MonkeyRecord获取坐标

MonkeyRecorder是一个比较好用的获取坐标的工具,它是用来获取真机或模拟器上坐标的工具,当我们点击真机或模拟器上的空间时,就能显示真机或模拟器上的点击点的坐标。

MonkeyRecorder的启动 
首先安卓手机连接上电脑,并保证以下两个条件成立: 
a.终端USB调成开发者模式 
b.电脑安装手机驱动 


手机连接成功后,打开cmd窗口,输入adb devices查看已连接真机或模拟器设备的名称,我们这里仍以模拟器为代表。 
之后,在cmd窗口,输入monkeyrunner后,启动Monkeyrunner。做以下操作:导入MonkeyRecorder包、连接模拟器设备、以MonkeyRecorder方式启动模拟器,并依次输入 
如下命令:

from com.android.monkeyrunner import MonkeyRunner,MonkeyDevice

from com.android.monkeyrunner.recorder import MonkeyRecorder as recorder

device=MonkeyRunner.waitForConnection()

recorder.start(device)



3、monkeyrunner脚本演示

回放脚本,recorder_playback.py

import sys

from com.android.monkeyrunner import MonkeyRunner

CMD_MAP = {

'TOUCH': lambda dev, arg: dev.touch(**arg),

'DRAG': lambda dev, arg: dev.drag(**arg),

'PRESS': lambda dev, arg: dev.press(**arg),

'TYPE': lambda dev, arg: dev.type(**arg),

'WAIT': lambda dev, arg: MonkeyRunner.sleep(**arg)

}

# Process a single file for the specified device.

def process_file(fp, device):

for line in fp:

(cmd, rest) = line.split('|')

try:

# Parse the pydict

rest = eval(rest)

except:

print 'unable to parse options'

continue

if cmd not in CMD_MAP:

print 'unknown command: ' + cmd

continue

CMD_MAP[cmd](device, rest)

def main():

file = sys.argv[1]

fp = open(file, 'r')

device = MonkeyRunner.waitForConnection()

process_file(fp, device)

fp.close()

if __name__ == '__main__':

main()














评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值