Monkey is a command line tool in Android. The monkey command sends a pseudo-random user event stream (such as key input, touch screen input, gesture input, etc.) to the system to implement stress testing of the application under development. .
The operating environment of this tutorial: Windows 7 system, Dell G3 computer.
Monkey is a tool included in the SDK. Monkey is a command line tool in Android that can run in the emulator or on the actual device. It sends a pseudo-random user event stream (such as key input, touch screen input, gesture input, etc.) to the system to implement stress testing of the application under development. Monkey testing is a fast and effective method to test the stability and robustness of software.
This tool is used for stress testing. Then the developer combines the logs printed by the monkey and the logs printed by the system to analyze the problems in the test
Features of the Monkey test:
Monkey test, all events are randomly generated, without Any human subjectivity.
1. The test object is only the application package, which has certain limitations.
2. The event data stream used by Monky test is random and cannot be customized.
3. You can set the MonkeyTest object, event number, type, frequency, etc.
The basic syntax is as follows:
$ adb shell monkey [options]
If not specified options, Monkey will start in non-feedback mode and send events to all packages installed in the target environment. Here is a more typical command line example that starts a specified application and sends 500 pseudo-random events to it:
$ adb shell monkey -p your.package.name -v 500
When using the android automated testing tool monkeyrunner to start an application, you need to fill in the package name of the program under test and the started Activity. There are two ways to view the application package name and entry activity name:
Method 1: Use aapt //aapt is a tool that comes with sdk, in the sdk\builds-tools\ directory
1. Take the ES file browser as an example, switch to the aapt.exe directory on the command line and execute: aapt dump badging E:\apk\es3.apk
2.
Note: You can find aapt.exe by searching in the android sdk directory. If not, you can download apktool.
Through this example, we can understand the steps of Monkey testing and how to know which applications can be tested with Monkey .
Under Windows (Note: Steps 2-4 are to see which application packages we can test and can be omitted):
1. Start an Android emulator through eclipse
2. Enter: adb devices in the command line to check the device connection status
C:\Documents and Settings\Administrator>adb devices
List of devices attached
emulator-5554 device
3. Under the premise that the device is connected, enter: adb shell in the command line Enter the shell interface
C:\Documents and Settings\Administrator>adb shell
#4. View the application package in the data/data folder. Note: The application packages we can test are all in this directory
C:\Documents and Settings\Administrator>adb shell
# ls data/data
ls data/data
5. Use com.android.calculator2 as the object to perform MonkeyTest
#monkey -p com.android.calculator2 -v 500
where -p represents the object package –v represents the feedback information level
During the running process, the applications in the Emulator are constantly switching picture.
According to the selected different levels of feedback information, you can also see its execution process report and generated events in Monkey.
C:\Users\chenfenping>adb shell monkey
-help
usage: monkey [-p ALLOWED_PACKAGE [-p ALLOWED_PACKAGE] ...]
[-c MAIN_CATEGORY [-c MAIN_CATEGORY] ...]
[--ignore-crashes] [--ignore-timeouts]
[--ignore-security-exceptions]
[--monitor-native-crashes] [--ignore-native-crashes]
[--kill-process-after-error] [--hprof]
[--pct-touch PERCENT] [--pct-motion PERCENT]
[--pct-trackball PERCENT] [--pct-syskeys PERCENT]
[--pct-nav PERCENT] [--pct-majornav PERCENT]
[--pct-appswitch PERCENT] [--pct-flip PERCENT]
[--pct-anyevent PERCENT] [--pct-pinchzoom PERCENT]
[--pkg-blacklist-file PACKAGE_BLACKLIST_FILE]
[--pkg-whitelist-file PACKAGE_WHITELIST_FILE]
[--wait-dbg] [--dbg-no-events]
[--setup scriptfile] [-f scriptfile [-f scriptfile] ...]
[--port port]
[-s SEED] [-v [-v] ...]
[--throttle MILLISEC] [--randomize-throttle]
[--profile-wait MILLISEC]
[--device-sleep-time MILLISEC]
[--randomize-script]
[--script-log]
[--bugreport]
[--periodic-bugreport]
COUNT
1 参数: -p
用于约束限制,用此参数指定一个或多个包(Package,即App)。指定包之后,monkey将只允许系统启动指定的APP,如果不指定包,将允许系统启动设备中的所有APP.
* 指定一个包: adb shell monkey -p cn.emoney.acg 10
* 指定多个包:adb shell monkey -p cn.emoney.acg –p cn.emoney.wea -p cn.emoney.acg 100
* 不指定包:adb shell monkey 100
2 参数: -v
用于指定反馈信息级别(信息级别就是日志的详细程度),总共分3个级别,分别对应的参数如下表所示:
日志级别 Level0
示例 adb shell monkey -p cn.emoney.acg –v 100
说明缺省值,仅提供启动提示、测试完成和最终结果等少量信息
日志级别 Level 1
示例 adb shell monkey -p cn.emoney.acg –v -v 100
说明提供较为详细的日志,包括每个发送到Activity的事件信息
日志级别 Level 2
示例 adb shell monkey -p cn.emoney.acg –v -v –v 100
说明最详细的日志,包括了测试中选中/未选中的Activity信息
3 参数: -s
用于指定伪随机数生成器的seed值,如果seed相同,则两次Monkey测试所产生的事件序列也相同的。
Monkey 测试1:adb shell monkey -p cn.emoney.acg -s 10 100
Monkey 测试2:adb shell monkey -p cn.emoney.acg –s 10 100
两次测试的效果是相同的,因为模拟的用户操作序列(每次操作按照一定的先后顺序所组成的一系列操作,即一个序列)是一样的。
4 参数: --throttle<毫秒>
用于指定用户操作(即事件)间的时延,单位是毫秒;
adb shell monkey -p cn.emoney.acg --throttle 5000 100
5 参数: --ignore-crashes
用于指定当应用程序崩溃时(Force& Close错误),Monkey是否停止运行。如果使用此参数,即使应用程序崩溃,Monkey依然会发送事件,直到事件计数完成。
adb shellmonkey -p cn.emoney.acg --ignore-crashes 1000
测试过程中即使程序崩溃,Monkey依然会继续发送事件直到事件数目达到1000为止
adb shellmonkey -p cn.emoney.acg 1000
测试过程中,如果acg程序崩溃,Monkey将会停止运行
6 参数: --ignore-timeouts
用于指定当应用程序发生ANR(Application No Responding)错误时,Monkey是否停止运行。如果使用此参数,即使应用程序发生ANR错误,Monkey依然会发送事件,直到事件计数完成。
adb shellmonkey -p cn.emoney.acg --ignore-timeouts 1000
7 参数: --ignore-security-exceptions
用于指定当应用程序发生许可错误时(如证书许可,网络许可等),Monkey是否停止运行。如果使用此参数,即使应用程序发生许可错误,Monkey依然会发送事件,直到事件计数完成。
adb shellmonkey -p cn.emoney.acg --ignore-security-exception 1000
8 Parameters: --kill-process-after-error
is used to specify whether to stop the application when an error occurs. If this parameter is specified, when an error occurs in the application, the application stops running and remains in the current state
(Note: The application only remains in the state when the error occurs, and the system does not end the application. process).
adb shellmonkey -p cn.emoney.acg --kill-process-after-error 1000
9 Parameters: --monitor-native-crashes
is used to specify whether to monitor and report local code for application crashes.
adb shellmonkey -p cn.emoney.acg --monitor-native-crashes 1000
10 Parameters: --pct-{event category}{event category percentage}
is used to specify the number and percentage of each category of events (in the Monkey event In the sequence, the number of such events accounts for the percentage of the total number of events)
Example:
--pct-touch{Percent}
Adjust the percentage of touch events (Touch events is a down-up event, which occurs at a single location on the screen)
adb shell monkey -p cn.emoney.acg --pct-touch 10 100
--pct-motion {Percent}
Adjust the percentage of motion events (Action events are determined by the motion event somewhere on the screen Composed of a down event, a series of pseudo-random events and an up event)
adb shell monkey -p cn.emoney.acg --pct-motion 20 1000
--pct-trackball {Percentage}
Adjust the percentage of track events (Track event consists of one or several random consisting of movements, sometimes accompanied by clicks)
adb shell monkey -p cn.emoney.acg --pct-trackball 30 1000
--pct-nav {Percent}
Adjust the percentage of "basic" navigation events (Navigation events Consists of up/down/left/right from directional input device)
adb shell monkey -p cn.emoney.acg --pct-nav 40 1000
--pct-majornav {Percent}
Adjust the percentage of "major" navigation events (these navigation events usually trigger actions in the graphical interface, Such as: middle button, back button, menu button of 5-way keyboard)
adb shell monkey -p cn.emoney.acg --pct-majornav 50 1000
7. Output monkeylog
When running monkey or when you want to capture the program log and export it, sometimes you will be prompted: cannot create D:monkeytest.txt: read-only file system
Why is it sometimes possible and sometimes not?
Later I found that it was different from my usage habits. One time, I would enter the adb shell first and then use the command, and the other time I would enter it directly.
The command will fail after entering the adb shell~
Correct method: Do not enter the shell before exiting the shell or executing the command
C:\Documents and Settings\Administrator>adb shell monkey -p package name
-v 300 >e:\text.txt
After entering the adb shell, it is equivalent to entering the root of Linux, without permission. Create a file inside~
After an error occurs in the Monkey test, the general error checking steps are as follows:
1. Find where the error occurred in monkey
2. Check some event actions in Monkey before the error occurred, and execute the action manually
3. If the above steps still persist If you can't find it, you can use the monkey command executed before and execute it again. Note that the seed value must be the same--reproduction
General test result analysis:
1. ANR problem: in the log Search for "ANR"
2. Crash problem: Search for "Exception" in the log Force Close
PHP Chinese website! !
The above is the detailed content of What is the use of monkey command?. For more information, please follow other related articles on the PHP Chinese website!