Monkey是Android平台自动化测试的一种手段,能够在模拟器里或实际设备中运行,它向系统发送伪随机的用户事件流,如按键输入、触摸屏输入、手势输入等,实现对正在开发的应用程序进行压力测试。shell
固然对于移动APP测试,monkey测试出了压力测试外,也能够作作简单的机型配适,就是测试App是否会出现crash等异常,以此来判读其释放与机型适配,能够发现程序无响应问题和崩溃问题。app
接下来简单说说:测试
Monkey程序由Android系统自带,使用Java语言写成,在Android文件系统中的存放路径是:/system/framework/monkey.jar;spa
Monkey.jar程序是由一个名为“monkey”的Shell脚原本启动执行,shell脚本在Android文件系统中的存放路径是:/system/bin/monkey;调试
特征
一、 测试的对象仅为应用程序包,有必定的局限性。
二、 Monky测试使用的事件流数据流是随机的,不能进行自定义。
三、 可对MonkeyTest的对象,事件数量,类型,频率等进行设置。日志
这样就能够经过在CMD窗口中执行: adb shell monkey {+命令参数}来进行Monkey测试了。code
基本使用对象
进入monkey进程
命令:adb shell/system/bin/monkey事件
运行monkey
命令:adb shell monkey <event-count> ,<event-count>是指随机发送事件数
adb shell monkey 100,运行monkey并执行了100件随机事件。
monkey常规类命令
adb shell monkey -h 显示monkey参数帮助信息usage
adb shell monkey -v 打印出日志信息,每一个-v将增长反馈信息的级别,最多支持3个-v
monkey事件类命令
adb shell monkey -f <scriptfile> <event-count> 让monkey运行指定monkey脚本,<scriptfile> 为测试脚本名,<event-count>是指循环次数。
adb shell monkey -s <seed> <event-count> 能够重复以前的随机操做,-s 后接随机数的生成器的seed值。
adb shell monkey --throttle <milliseconds> 表示每一个事件之间的固定延迟事件,时间的单位为ms,若后面不接时间,monkey将不会延迟
事件百分比
adb shell monkey --ptc-touch <percent> 设定触摸事件的百分比
adb shell monkey --ptc-motion <percent> 设定动做事件的百分比
adb shell monkey --ptc-trackball <percent> 设定轨迹球事件的百分比
adb shell monkey --ptc-nav <percent> 设定基本导航事件的百分比
adb shell monkey --ptc-majornav <percent> 设定主要导航事件的百分比
adb shell monkey --ptc-syskeys <percent> 设定按键事件的百分比
adb shell monkey --ptc-appswitch <percent> 设定启动事件的百分比
adb shell monkey --ptc-anyevent <percent> 其余类型事件的百分比,包括keypress、不经常使用button以及其余未说起事件
monkey约束类命令
adb shell monkey -p <allowed-package-name> <event-count>, 测试一个或多个包名,每一个-p对应一个包。
adb shell monkey -p <main-category> <event-count>, 测试一个或多个类别。
monkey调试类命令
adb shell monkey --dbg-no-events <event-count> 监视应用程序所调用的包之间的转换
adb shell monkey --hprof <event-count> 在事件序列先后当即生成profiling report
adb shell monkey --ignore-crashes <event-count> 让monkey在应用崩溃或异常后继续发送事件直到计数完成
adb shell monkey --ignore-timeouts <event-count> 让monkey在任何超时错误发生后继续发送事件直到计数完成
adb shell monkey --ignore-security-exceptions <event-count> 让monkey在应用权限错误发生后继续发送事件直到计数完成
adb shell monkey --kill-process-after-error <event-count> 让monkey由于应用发生错误而中止时,将会通知系统中止发送错误进程
adb shell monkey --monitor-native-crashes <event-count> 让监视并报告monkey运行时Android系统native code的崩溃事件
adb shell monkey --wait-dbg <event-count> 暂停执行中的monkey,直到有调试器与它链接
monkey API
轨迹球事件DispatchTrackball(long downTime, long eventTime, int action, float x, float y, float pressure, float size, int metaState, float xPrecision, float yPrecision, int device, int edgeFlags)输入字符串事件DispatchString(String text)点击事件DispatchPointer(long downTime, long eventTime, int action, float x, float y, float pressure, float size, int metaState, float xPrecision, float yPrecision, int device, int edgeFlags)启动应用LaunchActivity(String pkg_name, String cl_name)等待事件UserWait(long sleeptime)按下按键DispatchPress(int keyCode)长按按键LongPress(int keyCode)发送按键DispatchKey(long downTime, long eventTime, int action, int code, int repeat, int metaState, int device, int scancode)