官方文档网址是: file:///D:/tool/new_androidsdk/docs/tools/help/monkey.html
The basic syntax is:
$ adb shell monkey [options]
With no options specified, the Monkey will launch in a quiet (non-verbose) mode, and will send events to any (and all) packages installed on your target. Here is a more typical command line, which will launch your application and send 500 pseudo-random events to it:
$ adb shell monkey -p your.package.name -v 500
例1: 在shell中输入命令(以测试com.hskj.memo为例):monkey -p com.hskj.memo -s 100 -v -v -v 500(测试com.hskj.memo包,以seed为100的随机排序<设置seed方便下次进行同样事件的测试,不
过要求测试的起始位置相同,比如起始位置为桌面或者为某一个Activity的某一个状态>,测试结果为第三级别的显示,即为最详细的显示)
例2: Monkey测试出现错误后一般查错步骤:
1。找到是哪个monkey的哪个动作出的错:
2。查看monkey出错前的一些事件动作,并手动执行该动作。
3。若还不能找出,可以使用命令monkey -p com.hskj.memo -s 100 -v -v -v --throttle 2000 500
例3:
1.设定每一个动作执行后停顿5秒钟或者别的一个时间段,直到出错,此时可以有更充分的时间来查看每一个动作的执行过程
2。找到错误后,进行源代码更改,更改错误后进行 相同的seed值 及 相同的起始位置 再进行一次相同事件的测试。
3。若错误不再发生,则进行别的SEED值的测试,直到经过千锤百炼都没有错再发生它才算是一个稳定性足够的程序。
--------------------------------monkey 测试工具 --------------------------------------------------------------
monkey 测试工具
来源:
一、Monkey测试简介
Monkey测试是Android平台自动化测试的一种手段,通过Monkey程序模拟用户触摸屏幕、滑动Trackball、按键等操作来对设备上的程序进行压
力测试,检测程序多久的时间会发生异常。
二、Monkey程序介绍
1) Monkey程序由Android系统自带,使用Java语言写成,在Android文件系统中的存放路径是:/system/framework/monkey.jar;
2) Monkey.jar程序是由一个名为“monkey”的Shell脚本来启动执行,shell脚本在Android文件系统中的存放路径是:/system/bin/monkey;
这样就可以通过在CMD窗口中执行: adb shell monkey {+命令参数}来进行Monkey测试了。
三、Monkey命令的简单帮助
要获取Monkey命令自带的简单帮助,在CMD中执行命令:
adb shell monkey –help
四、Monkey命令参数介绍
1) 参数: -p
参数-p用于约束限制,用此参数指定一个或多个包(Package,即App)。指定
包之后,Monkey将只允许系统启动指定的APP。如果不指定包,Monkey将允许系统启动设备中的所有APP。
* 指定一个包: adb shell monkey -p com.htc.Weather 100
说明:com.htc.Weather为包名,100是事件计数(即让Monkey程序模拟100次随机用户事件)。
* 指定多个包:adb shell monkey -p com.htc.Weather –p com.htc.pdfreader -p com.htc.photo.widgets 100
* 不指定包:adb shell monkey 100
说明:Monkey随机启动APP并发送100个随机事件。
* 要查看设备中所有的包,在CMD窗口中执行以下命令:
>adb shell
#cd data/data
#ls
2) 参数: -v
用于指定反馈信息级别(信息级别就是日志的详细程度),总共分3个级别,分别对应的参数如下表所示:
日志级别 Level 0
示例 adb shell monkey -p com.htc.Weather –v 100
说明 缺省值,仅提供启动提示、测试完成和最终结果等少量信息
日志级别 Level 1
示例 adb shell monkey -p com.htc.Weather –v -v 100
说明 提供较为详细的日志,包括每个发送到Activity的事件信息
日志级别 Level 2
示例 adb shell monkey -p com.htc.Weather –v -v –v 100
说明 最详细的日志,包括了测试中选中/未选中的Activity信息
3)参数: -s
用于指定伪随机数生成器的seed值,如果seed相同,则两次Monkey测试所产生的事件序列也相同的。
* 示例:
Monkey测试1:adb shell monkey -p com.htc.Weather –s 10 100
Monkey 测试2:adb shell monkey -p com.htc.Weather –s 10 100
两次测试的效果是相同的,因为模拟的用户操作序列(每次操作按照一定的先后顺序所组成的一系列操作,即一个序列)是一样的。操作序
列虽 然是随机生成的,但是只要我们指定了相同的Seed值,就可以保证两次测试产生的随机操作序列是完全相同的,所以这个操作序列伪随
机的;
4) 参数: --throttle <毫秒>
用于指定用户操作(即事件)间的时延,单位是毫秒;
* 示例:adb shell monkey -p com.htc.Weather –throttle 3000 100
5) 参数: --ignore-crashes
用于指定当应用程序崩溃时(Force & Close错误),Monkey是否停止运行。如果使用此参数,即使应用程序崩溃,Monkey依然会发送事件,直
到事件计数完成。
* 示例1:adb shell monkey -p com.htc.Weather --ignore-crashes 1000
测试过程中即使Weather程序崩溃,Monkey依然会继续发送事件直到事件数目达到1000为止;
* 示例2:adb shell monkey -p com.htc.Weather 1000
测试过程中,如果Weather程序崩溃,Monkey将会停止运行。
6) 参数: --ignore-timeouts
用于指定当应用程序发生ANR(Application No Responding)错误时,Monkey是否停止运行。如果使用此参数,即使应用程序发生ANR错误,
Monkey依然会发送事件,直到事件计数完成。
7) 参数: --ignore-security-exceptions
用于指定当应用程序发生许可错误时(如证书许可,网络许可等),Monkey是否停止运行。如果使用此参数,即使应用程序发生许可错误,
Monkey依然会发送事件,直到事件计数完成。
8) 参数: --kill-process-after-error
用于指定当应用程序发生错误时,是否停止其运行。如果指定此参数,当应用程序发生错误时,应用程序停止运行并保持在当前状态(注意:
应用程序仅是静止在发生错误时的状态,系统并不会结束该应用程序的进程)。
9) 参数: --monitor-native-crashes
用于指定是否监视并报告应用程序发生崩溃的本地代码。
10) 参数: --pct-{+事件类别} {+事件类别百分比}
用于指定每种类别事件的数目百分比(在Monkey事件序列中,该类事件数目占总事件数目的百分比)
参数:
使用说明:
示例:
--pct-touch {+百分比}
调整触摸事件的百分比(触摸事件是一个down-up事件,它发生在屏幕上的某单一位置)
adb shell monkey -p com.htc.Weather --pct-touch 10 1000
--pct-motion {+百分比}
调整动作事件的百分比(动作事件由屏幕上某处的一个down事件、一系列的伪随机事件和一个up事件组成)adb shell monkey -p
com.htc.Weather --pct-motion 20 1000
--pct-trackball {+百分比}
调整轨迹事件的百分比(轨迹事件由一个或几个随机的移动组成,有时还伴随有点击)
adb shell monkey -p com.htc.Weather --pct-trackball 30 1000
--pct-nav {+百分比}
调整“基本”导航事件的百分比(导航事件由来自方向输入设备的up/down/left/right组成)
adb shell monkey -p com.htc.Weather --pct-nav 40 1000
--pct-majornav {+百分比}
调整“主要”导航事件的百分比(这些导航事件通常引发图形界面中的动作,如:5-way键盘的中间按键、回退按键、菜单按键)
adb shell monkey -p com.htc.Weather --pct-majornav 50 1000
--pct-syskeys {+百分比}
调整“系统”按键事件的百分比(这些按键通常被保留,由系统使用,如Home、Back、Start Call、End Call及音量控制键)
adb shell monkey -p com.htc.Weather --pct-syskeys 60 1000
--pct-appswitch {+百分比}
调整启动Activity的百分比。在随机间隔里,Monkey将执行一个startActivity()调用,作为最大程度覆盖包中全部Activity的一种方法
adb shell monkey -p com.htc.Weather --pct-appswitch 70 1000
--pct-anyevent {+百分比}
调整其它类型事件的百分比。它包罗了所有其它类型的事件,如:按键、其它不常用的设备按钮、等等
adb shell monkey -p com.htc.Weather
--pct -anyevent 100 1000* 指定多个类型事件的百分比:
adb shell monkey -p com.htc.Weather --pct-anyevent 50 --pct-appswitch 50 1000
注意:各事件类型的百分比总数不能超过100%;
--------------------------------------------这个就是官方文档------------------------------------------------------
UI/Application Exerciser Monkey
The Monkey is a program that runs on your emulator or device and generates pseudo-random streams of user events such as clicks, touches, or gestures, as well as a number of system-level events. You can use the Monkey to stress-test applications that you are developing, in a random yet repeatable manner.
Overview
The Monkey is a command-line tool that that you can run on any emulator instance or on a device. It sends a pseudo-random stream of user events into the system, which acts as a stress test on the application software you are developing.
The Monkey includes a number of options, but they break down into four primary categories:
- Basic configuration options, such as setting the number of events to attempt.
- Operational constraints, such as restricting the test to a single package.
- Event types and frequencies.
- Debugging options.
When the Monkey runs, it generates events and sends them to the system. It also watches the system under test and looks for three conditions, which it treats specially:
- If you have constrained the Monkey to run in one or more specific packages, it watches for attempts to navigate to any other packages, and blocks them.
- If your application crashes or receives any sort of unhandled exception, the Monkey will stop and report the error.
- If your application generates an application not responding error, the Monkey will stop and report the error.
Depending on the verbosity level you have selected, you will also see reports on the progress of the Monkey and the events being generated.
Basic Use of the Monkey
You can launch the Monkey using a command line on your development machine or from a script. Because the Monkey runs in the emulator/device environment, you must launch it from a shell in that environment. You can do this by prefacing adb shell
to each command, or by entering the shell and entering Monkey commands directly.
The basic syntax is:
$ adb shell monkey [options]
With no options specified, the Monkey will launch in a quiet (non-verbose) mode, and will send events to any (and all) packages installed on your target. Here is a more typical command line, which will launch your application and send 500 pseudo-random events to it:
$ adb shell monkey -p your.package.name -v 500
Command Options Reference
The table below lists all options you can include on the Monkey command line.
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 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. |