认识Monkey

官方文档:https://developer.android.com/studio/test/monkey.html

什么是Monkey?

Monkey是Android中的一个命令行工具,可以运行在模拟器或实际设备中,它向系统发送随机的用户事件流(按键输入,触摸屏输入,手势输入等),实现对应用程序随机,快速疯狂操作的效果。Monkey测试是一种为了测试软件的稳定性,健壮性的快速有效的方法。

 Monkey测试的目的是什么?

  • Android应用的Crash
  • Android应用的ANR

一、安卓adb环境搭建

略,android开发环境:使用Android Studio搭建Android集成开发环境(图文教程)

二、Monkey使用步骤

1.ADB连接Android测试设备,在命令行输入 adb devices 查看设备连接情况

如果是真机,直接使用USB线连接真机和PC机,adb devices即可看到真机已经连接上了,如

C:\Users\Administrator>adb devices
List of devices attached
611AKBPP22HR5 device

如果使用模拟器,如夜神模拟器,则需要手工连接才可以看到模拟器

C:\Users\Administrator>adb connect 127.0.0.1:
connected to 127.0.0.1: C:\Users\Administrator>adb devices
List of devices attached
127.0.0.1: device

2.确定待测试应用的PackageName

方法一:直接问开发即可

方法二:使用一些查包工具可以获得PacakgeName

方法三:通过重签名软件可以获得,参照《使用re-sign.jar对apk进行重签名

3.使用Monkey发一条测试命令

示例:

adb shell monkey -p com.ssjj.recorder -v 

包名:com.ssjj.recorder

-p 指定测试包名

-v 显示默认程度的信息

事件数设定为50

三、Monkey 命令常用参数详解

Monkey基础语法

$ adb shell monkey [options] <event-count>
Category Option Description
General --help Prints a simple usage guide.
-v Each -v on the command line will increment the verbosity level. Level 0 (the default) provides little information beyond startup notification, test completion, and final results. Level 1 provides more details about the test as it runs, such as individual events being sent to your activities. Level 2 provides more detailed setup information such as activities selected or not selected for testing.
Events -s <seed>

Seed value for pseudo-random number generator. If you re-run the Monkey with the same seed value, it will generate the same sequence of events.

--throttle <milliseconds>

Inserts a fixed delay between events. You can use this option to slow down the Monkey. If not specified, there is no delay and the events are generated as rapidly as possible.

--pct-touch <percent> Adjust percentage of touch events. (Touch events are a down-up event in a single place on the screen.)
--pct-motion <percent> Adjust percentage of motion events. (Motion events consist of a down event somewhere on the screen, a series of pseudo-random movements, and an up event.)
--pct-trackball <percent> Adjust percentage of trackball events. (Trackball events consist of one or more random movements, sometimes followed by a click.)
--pct-nav <percent> Adjust percentage of "basic" navigation events. (Navigation events consist of up/down/left/right, as input from a directional input device.)
--pct-majornav <percent> Adjust percentage of "major" navigation events. (These are navigation events that will typically cause actions within your UI, such as the center button in a 5-way pad, the back key, or the menu key.)
--pct-syskeys <percent> Adjust percentage of "system" key events. (These are keys that are generally reserved for use by the system, such as Home, Back, Start Call, End Call, or Volume controls.)
--pct-appswitch <percent> Adjust percentage of activity launches. At random intervals, the Monkey will issue a startActivity() call, as a way of maximizing coverage of all activities within your package.
--pct-anyevent <percent> Adjust percentage of other types of events. This is a catch-all for all other types of events such as keypresses, other less-used buttons on the device, and so forth.
Constraints -p <allowed-package-name> If you specify one or more packages this way, the Monkey will only allow the system to visit activities within those packages. If your application requires access to activities in other packages (e.g. to select a contact) you'll need to specify those packages as well. If you don't specify any packages, the Monkey will allow the system to launch activities in all packages. To specify multiple packages, use the -p option multiple times — one -p option per package.
-c <main-category> If you specify one or more categories this way, the Monkey will only allow the system to visit activities that are listed with one of the specified categories. If you don't specify any categories, the Monkey will select activities listed with the category Intent.CATEGORY_LAUNCHER or Intent.CATEGORY_MONKEY. To specify multiple categories, use the -c option multiple times — one -c option per category.
Debugging --dbg-no-events When specified, the Monkey will perform the initial launch into a test activity, but will not generate any further events. For best results, combine with -v, one or more package constraints, and a non-zero throttle to keep the Monkey running for 30 seconds or more. This provides an environment in which you can monitor package transitions invoked by your application.
--hprof If set, this option will generate profiling reports immediately before and after the Monkey event sequence. This will generate large (~5Mb) files in data/misc, so use with care. See Traceview for more information on trace files.
--ignore-crashes Normally, the Monkey will stop when the application crashes or experiences any type of unhandled exception. If you specify this option, the Monkey will continue to send events to the system, until the count is completed.
--ignore-timeouts Normally, the Monkey will stop when the application experiences any type of timeout error such as a "Application Not Responding" dialog. If you specify this option, the Monkey will continue to send events to the system, until the count is completed.
--ignore-security-exceptions Normally, the Monkey will stop when the application experiences any type of permissions error, for example if it attempts to launch an activity that requires certain permissions. If you specify this option, the Monkey will continue to send events to the system, until the count is completed.
--kill-process-after-error Normally, when the Monkey stops due to an error, the application that failed will be left running. When this option is set, it will signal the system to stop the process in which the error occurred. Note, under a normal (successful) completion, the launched process(es) are not stopped, and the device is simply left in the last state after the final event.
--monitor-native-crashes Watches for and reports crashes occurring in the Android system native code. If --kill-process-after-error is set, the system will stop.
--wait-dbg Stops the Monkey from executing until a debugger is attached to it.

 Monkey组合命令举例

adb shell monkey -p com.ssjj.recorder --pct-motion  --pct-touch  --pct-nav  --pct-appswitch 10 --ignore-crashes --ignore-timeouts --throttle  -s  -v -v -v 

命令是需要组合的

命令本身是需要根据不同的测试切入点来进行编写:

1.触摸事件和手势事件是用户最常见的操作,所以通过 --pct-touch 和 --pct-motion 将这两个事件的占比调整到40%与25%

2.目标应用包含了多个Activity,为了能覆盖大部分的Activity,所以通过--pct-appswitch将Activity切换的事件调整到10%

3.使用--ignore-crash 和 --ignore-timeouts 参数使Monkey遇到意外是能继续执行

4.使用--throttle参数来控制Monkey每个操作之间的时间间隔

四、Monkey的报告

  • 主要关注CRASH和ANR
  • Monkey并没有完整的报告,可以重定向monkey日志到文本文档
  • 编写脚本正则匹配报告中的关键字

异常日志分析

1.找到monkey里面的哪个地方出差
2.查看Monkey里面出错前的一些事件动作,并手动执行该动作
3.若以上步骤不能找出,可以使用之前执行的monkey命令再执行一次,注意seed值要一样。
4.注意第一个switch以及其他异常信息

常见问题
1.ANR问题:在日志中搜索“ANR”
2.崩溃问题:在日志中搜索"CRASH"

假如运行结束后,结果显示为:
** System appears to have crashed at event 48 of 50 using seed 1499432070709
则说明系统在50个操作的第48个操作里面出现了异常,用的种子是1499432070709。我们下次用相同的操作序列再现这个异常,如
adb shell monkey -p com.ssjj.recorder -s 1499432070709 500


***微信扫一扫,关注“python测试开发圈”,了解更多测试教程!***

使用Monkey对apk做稳定性测试的更多相关文章

  1. [转贴]LTP--linux稳定性测试 linux性能测试 ltp压力测试 ---IBM 的 linux test project

    https://blog.csdn.net/melody157398/article/details/24354415   LTP--linux稳定性测试 linux性能测试 ltp压力测试 ---I ...

  2. LTP--linux稳定性测试 linux性能测试 ltp压力测试 ---IBM 的 linux test project

    LTP--linux稳定性测试 linux性能测试 ltp压力测试 ---IBM 的 linux test project Peter盼 2014-04-23 11:25:49  20302  收藏  ...

  3. LTP--linux稳定性测试 linux性能测试 ltp压力测试 内核更新 稳定性测试

    LTP--linux稳定性测试 linux性能测试 ltp压力测试 zhangzj1030关注14人评论33721人阅读2011-12-09 12:07:45   说明:在写这篇文章之前,本人也不曾了 ...

  4. LTP--linux稳定性测试 linux性能测试 ltp压力测试 ltp-pan

    LTP--linux稳定性测试 linux性能测试 ltp压力测试 zhangzj1030关注14人评论33710人阅读2011-12-09 12:07:45   说明:在写这篇文章之前,本人也不曾了 ...

  5. Android客户端稳定性测试——Monkey

    修改时间 修改内容 修改人 2016.6.20 创建 刘永志 2016.6.29 完成 刘永志 Monkey简介: Android SDK自带的命令行测试工具,向设备发送伪随机事件流,对应用程序进行进 ...

  6. monkey稳定性测试的步骤及策略

    1.adb的作用是什么?adb的全称:android debug bridge 安卓调试桥梁,包含在 Android SDK 平台工具软件包中.通过该命令与设备进行通信,以便进行调试adb可以同时管理 ...

  7. 【Monkey】Monkey稳定性测试常用命令

    Monkey稳定性测试常用命令: 1.adb shell monkey n 2.adb shell monkey -p  com.android.calculator2 1000 3.adb shel ...

  8. Monkey Android app稳定性测试工具之Monkey使用教程

    Monkey Android app稳定性测试工具之Monkey使用教程 by:授客 QQ:1033553122 由于篇幅问题,仅提供百度网盘下载链接: Android app稳定性测试工具之Monk ...

  9. Monkey 稳定性测试

    学习网址: https://blog.csdn.net/lucytan01/article/details/79958727 https://blog.csdn.net/hebbely/article ...

随机推荐

  1. 转!!Java设置session超时(失效)的时间

    Java设置session超时(失效)的时间   在一般系统登录后,都会设置一个当前session失效的时间,以确保在用户长时间不与服务器交互,自动退出登录,销毁session具体设置的方法有三种:1 ...

  2. gophercloud openstack networking 源码分析

    1.network 部分 // Package networks contains functionality for working with Neutron network resources. ...

  3. 我的Android进阶之旅------>Android Listview跳到指定条目位置的两种实现方法

    前言 今天实现ListView跳转到第一个条目位置时,使用smoothScrollToPosition(int position)方法跳转实现了,但是交互说不需要这样的动画效果,需要直接跳转到第一项, ...

  4. 斯坦福大学Andrew Ng - 机器学习笔记(8) -- 推荐系统 & 大规模机器学习 & 图片文字识别

    大概用了一个月,Andrew Ng老师的机器学习视频断断续续看完了,以下是个人学习笔记,入门级别,权当总结.笔记难免有遗漏和误解,欢迎讨论. 鸣谢:中国海洋大学黄海广博士提供课程视频和个人笔记,在此深 ...

  5. C/C++运算符及其优先级

    1.自增自减 (1)前置运算:"先变后用"   如++i. 后置运算:"先用后变"   如i--. 比如: int i = 5. y1 = ++i: y2 = ...

  6. JAVA三框架工作原理是什么?

    一.struts的工作原理: 1.初始化,读取struts-config.xml.web.xml等配置文件(所有配置文件的初始化) 2.发送HTTP请求,客户端发送以.do结尾的请求 3.填充Form ...

  7. App doesn't auto-start an app when booting the device in Android

    From Android 3.1, newly installed apps are always put into a "stopped" state and the only ...

  8. 算法题 21 findNSum (好未来,LeetCode,牛客网)

    一.三数之和:LeetCode 15 给定一个包含 n 个整数的数组 nums,判断 nums 中是否存在三个元素 a,b,c ,使得 a + b + c = 0 ?找出所有满足条件且不重复的三元组. ...

  9. dobbo 简单框架

  10. 【HTML5校企公益课】第四天

    1.上午考试没去.. 2.下午跟不上.. 变色.html <!DOCTYPE html> <html> <head> <meta charset=" ...