Introduction to Monkey

1. The Monkey is introduced

Monkey is a command line tool that can run in emulators or on real devices. It sends a pseudo-random stream of user events to the system to stress test the application under development.

Monkey tests are also called Monkey tests. Is to use some bizarre operation to test the system being tested, to test the stability of the system.


The Monkey test is a tool for Android automation testing. The Monkey test itself is very simple. It simulates the user’s key input, touch screen input, gesture input, etc., to see how long the device will fail.


When the Monkey program is running on an emulator or device, it generates random pulses when the user triggers a tap, touch, gesture, or system-level event, so use the Monkey to load your software with random repetition.


Monkey includes many options, which fall into four broad categories:


  • Basic configuration options, such as setting the number of events attempted.
  • Run constraint options, such as setting only a single package to be tested.
  • Event type and frequency.
  • Debug options.

As the Monkey runs, it generates events and sends them to the system. The Monkey also monitors the system under test for special treatment for three conditions:


  • If you limit the Monkey to run on one or more specific packages, it will detect attempts to switch to other packages and block them.
  • If the application crashes or receives any out-of-control exceptions, the Monkey stops and reports an error.
  • If your application generates an application Not responding error, the Monkey will stop and report an error.

Depending on the level of feedback selected, you can also see reports of its execution and generated events in the Monkey.


Speaking of Monkey, we must introduce ADB:


ADB is a tool in the Android SDK that allows you to directly manage Android emulators or real Android devices. Its main functions are:


  • Running the device’s shell(command line)
  • Manages port mappings for emulators or devices
  • Upload/download files between computer and device
  • Install the native APK software into the emulator or Android device
  • ADB is a client-server program where the client is the computer you’re working on and the server is an Android device.

2. Adb common commands:

Adb Devices Gets all emulators or real machines connected to ADB


Adb install c:/ XXX


Adb uninstall APK Package name To uninstall apK from the device


Adb-s Emulator-5556 uninstall APK Package name Specifies the name of the package to uninstall apK


Adb start-server Restarts ADB


Adb kill-server Kills ADB


Adb shell PM List Packages gets all application package names


3.Monkey:

Adb shell monkey — help (get help)


Adb Shell Monkey 1000 (execute 1000 simulated events randomly)


Adb shell Monkey -p XXXX 1000 adb shell Monkey -p XXXX 1000


Adb Shell Monkey (parameter) 100 (execute 100 simulated events with parameter)





Monkey parameters

1. Basic parameters:

-v: indicates the log detail level.


Note: Each -v increases the level of feedback. Level 0(the default) provides little information beyond startup prompts, test completion, and final results. Level 1 provides more detailed test information, such as events sent to the Activity one by one. Level 2 provides more detailed setup information, such as selected or unselected activities in tests.


-s: indicates the seed value of the pseudorandom number generator.


Note: If you run the Monkey again with the same seed value, both Monkey tests will generate the same sequence of events.


–throttle: Specifies the time interval between events, in milliseconds


Note: You can slow down the Monkey with this option. If this option is not specified, the Monkey will not be delayed and events will be produced as quickly as possible.


2. Type of the sent event

— pcT-touch <percent>: Specifies the percentage of touch events that are pressed and lifted at one point.


— pcT-motion <percent>: Specifies the percentage of sliding events. Press first, slide a distance, and then lift.


— PCT-trackball <percent>: Trackball events, a series of random moves and clicks.


— pcT-nav <percent>: Percentage of basic navigation events (hardware) : Sets the percentage of basic navigation events (up, down, left, and right navigation keys) generated.


— pcT-majornav <percent>: Indicates the percentage of major navigation events that will result in feedback from the UI, such as clicking the middle button of the five ARROW keys, clicking the back button, or clicking the menu key.


–pct-syskeys <percent>: specifies the percentage of system key events (Home, Back, startCall, endCall, and volumeControl).


— pct-appSwitch <percent>: Specifies the percentage of activities that are started. At random intervals, the Monkey will execute a startActivity() call as a way to maximize coverage of all activities in the package.


–pct-anyevent <percent>: Specifies other events, common button messages, and button events that are not commonly used on the device.


3. Constraints

-p <allowed-package-name>: If one or more packages are specified with this parameter, the Monkey will only allow the system to start activities in those packages. If your application also needs to access activities in other packages (such as selecting a contact), those packages need to be specified at the same time. If no package is specified, the Monkey will allow the system to start activities in all packages.


Note: To specify multiple packages, you need to use multiple -p options, each of which can only be used for one package.


-c <main-category>: If you specify one or more categories with this parameter, the Monkey will only allow the system to start activities listed by one of those categories. If no category is specified, the Monkey selects one of the activities listed in the following categories: Intent.category_launcher or intent.category_monkey.


Note: To specify multiple categories, you need to use multiple -c options, each of which can be used for only one category.


4. Debug options

–dbg-no-events: If you specify this option, the monkey starts the application under test but does not send any messages. You are advised to use these options with -v,-p, and -throttle.


–hprof: if you specify this option, the monkey generates a performance report (a snapshot of memory file) before and after the event. Typically, the monkey generates a 5M file in the /data/misc directory of the device.


— Ignore-crashes: This option allows the application under test to continue sending system messages when crashes or anomalies occur until a specified number of messages have been sent, or stop running.


— Ignore-timeouts: If this option is specified, the application under test stops sending system messages (for example, the Application Does not respond dialog box is displayed). If the application under test stops sending system messages until a specified number of messages are sent, the application will stop running.


–ignore-security-exceptions: If this option is specified, the application under test will continue to send system messages when it encounters permissions errors until the specified number of messages are sent, otherwise it will stop running.


–kill-process-after-error: Normally, when the Monkey specifies the runtime for an error, the offending application is left on the system. This option tells the system to kill the process if an error occurs.


Note: When the Monkey completes its normal execution, it does not close the application it started, leaving the device in the last state to receive the message.


— Ignore-native-crashes: Monitors crashes caused by the Android C/C++ code part (CPU computing part), and the whole system shuts down if “–kill-process-after-error” is set.


–wait-dbg: Stop the Monkey in execution until a debugger is connected to it.





Monkey test commands

1. Do not enter logs:

adb shell monkey -v –throttle 300 –pct-touch 30 –pct-motion 20 –pct-nav 20 –pct-majornav 15 –pct-appswitch 5 –pct-anyevent 5 –pct-trackball 0 –pct-syskeys 0 -p <package-name> 1000


2. Enter logs:

adb shell monkey -v –throttle 300 –pct-touch 30 –pct-motion 20 –pct-nav 20 –pct-majornav 15 –pct-appswitch 5 –pct-anyevent 5 –pct-trackball 0 –pct-syskeys 0 -p <package-name> 1000>D:\Android\monkey\log.txt