The main content
- What is a stability test
- Monkey is introduced
- Automation Monkey
What is a stability test
Click on the screen at random for a while to see if the app crashes and works properly.
Monkey is introduced
Monkey is a command line tool that runs in our emulator or device. It can send a pseudo-random stream of user events, such as clicks, touches, gestures, etc. We can use the Monkey tool to stress test the applications we develop. Monkey testing is a quick and effective way to test the stability and robustness of your software. The basic syntax of Monkey commands is as follows:
adb shell monkey [options] <event-count>
Copy the code
If no arguments are given, the Monkey will start in no-feedback mode and send events arbitrarily to all packages installed in the target environment. Here is a more typical command line example that starts the specified application and sends it 500 pseudo-random events:
adb shell monkey -p your.package.name -v 500
Copy the code
According to the introduction of the Monkey’s official website: https://developer.android.com/studio/test/monkey you can add the corresponding parameters. Monkey parameters are set according to specific requirements, and are divided into general classes, event classes, constraint classes, and debug classes. These commands are explained below:
category | parameter | instructions |
---|---|---|
Regular class | –help | The MoneKY parameter help usage is displayed |
-v | Logs are printed. Each -v increases the level of feedback information. -V Indicates more detailed log information. Currently, a maximum of three -v values are supported. Level0: A -v that provides little information beyond startup prompts, test completion, and final results. Level1: Two -v’s that provide more detailed test information, such as events sent to the Activity one by one. Level2: Three -v’s that provide more detailed setup information, such as selected or unselected activities in the test. | |
Event classes | -s | The seed value of the pseudo-random number generator. If you run the Monkey again with the same seed value, it will generate the same sequence of events. |
–throttle | This is followed by a time, in milliseconds, representing a fixed delay between events (the time between each instruction). If this option is not checked, the Monkey will not delay | |
–pct-touch | Percentage of subsequent touch incidents. (A touch event is a down event that occurs in a single location on the screen.) | |
–pct-motion | This is followed by the percentage of action events. Action events consist of a down event somewhere on the screen, a series of pseudo-random events, and an Up event. | |
–pct-trackball | This is followed by the percentage of track events (track events consist of one or more random movements, sometimes accompanied by clicks). | |
–pct-nav | This is followed by the percentage of “basic” navigation events (navigation events come primarily from up, down, left, and right events on directional input devices) | |
–pct-majornav | Percentage of navigation events followed by “major” navigation events (these navigation events usually trigger actions in the graphical interface, such as the 5-way keyboard middle button, the back button, the menu button) | |
–pct-syskeys | This is followed by the percentage of system key events (these keys are usually reserved for use by the system, such as Home, Back, StartCall, End Call, and volume control keys). | |
–pct-appswitch | This is followed by the percentage that started the Activity. At random intervals, the Monkey will execute a startActivity() call as a way to maximize coverage of all activities in the package. | |
–pct-anyevent | Adjust the percentage of other types of events. It covers all other types of events, such as buttons, other device buttons that are not commonly used, and so on. | |
Constraint class | -p | If you specify one or more packages with this parameter, the Monkey will only allow the system to start activities within 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. To specify multiple packages, you need to use multiple -p options, each of which can be used for only one package. |
-c | If you specify one or more categories with this parameter, the Monkey will only allow the system to start activities listed in 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. To specify multiple categories, you need to use multiple -c options, each of which can be used for only one category. | |
The debug class | –dbg-no-events | With this option, the Monkey performs an initial startup, enters a test Activity, and then generates no further events. For best results, combine this with -v, one or more package constraints, and a non-zero value that keeps the Monkey running for 30 seconds or more to provide an environment for monitoring the transitions between packages invoked by your application. |
–hprof | Setting this option will generate a profiling report immediately before and after the Monkey event sequence. This will generate large files (~5Mb) in data/ misC, so use it with care. | |
–ignore-crashes | Typically, the Monkey will stop running when the application crashes or any out-of-control exceptions occur. If you set this option, the Monkey will continue sending events to the system until the count is complete. | |
–ignore-timeouts | Typically, the Monkey will stop when your application experiences any timeout errors, such as the “ApplicationNot Responding” dialog box. If you set this option, the Monkey will continue sending events to the system until the count is complete. |
A common Monkey command
adb shell monkey -v -v -v -p [PackageName] --ignore-crashes --ignore-timeouts --ignore-security-exceptions --monitor-native-crashes --ignore-native-crashes --throttle 1000 100000 > monkey.txt
Copy the code
The characteristics of the Monkey
To summarize the above description, use the following features of the Monkey:
1. The event stream data stream used by Monky test is random and cannot be customized. 2. MonkeyTest object, event number, type, frequency and so on can be set.
Develop Monkey automation scripts in natural language
Since the command line needs to modify different parameters each time, this operation is not very flexible, we can write a piece of code to drive. To make the script more readable, we use THE BDD approach. The advantage of BDD is that you can modify the parameters in the natural language description to run the program directly, and it can be data-driven. It is especially convenient for team work, or for newcomers to learn about the business. Here’s a tool to do this:
Because the script uses JavaScript, the development tool can use CukeTest(http://cuketest.com) and the execution engine can be implemented with adBKit. The steps are as follows:
-
Environment build configuration SDK (please make your own online access to the machine to install the SDK) installation node. Js library adbkit (https://github.com/openstf/adbkit)
-
Create project Open CukeTest, File New – New project; Project Type Select a basic project, fill in the project information, and complete the creation.
Execute in the root directory of the project
npm install adbkit --save
The ABdKit library has been installed. Making https://github.com/openstf/adbkit about abdkit can reference -
Set the timeout because it takes a long time to run the Monkey script to check the stability of the application, set the timeout to a long time
Edit the support/env. Js
const { setDefaultTimeout } = require('cucumber')
setDefaultTimeout(1000 * 1000)
Copy the code
Edit feaure files
# language: zh-CNCall the Monkey script to check the stability of the mobile app native calculator Scenario: Design and call the Monkey script if the app's package is"com.android.calculator2"I also set the run random value to 10000 when I want the proportion of basic navigation events to be"10"The proportion of simultaneous touch events in all events is"70"At the same time, the interval between each operation step is 1000 milliseconds and the phone serial number is if the program crashes or exceptions are ignored"192.168.181.101:5555"Run the script 1000 timesCopy the code
If you want to change the parameters of the Monkey script, just change the parameters in the feature file.
- Write automated scripts
const { Given, When, Then } = require('cucumber');
var adb = require('adbkit')var client = adb.createClient()
//// Your step definitions /////
var command = ' monkey '
Given(/^app package is "([^"]*)"$/.async function (packagename) {
command = command + " -p " + packagename
});
Given(/^ Set the run random value to (\d+)$/.async function (seed) {
command += " -s " + seed
});
When(/^ I want the occupancy ratio for basic navigation events to be "([^"]*)"$/.async function (nav) {
command += ' --pct-touch ' + nav});
When(/^ The percentage of touch events in all events is "([^"]*)"$/.async function (touch) {
command += " --pct-touch " + touch
});
When(/^ Ignore program crash or exception $/.async function () {
command += " --ignore-crashes "
});
When(/^ The interval between each step is (\d+) ms $/.async function (ms) {
command += " --throttle " + ms
});
Then(/^ Phone serial number is "([^"]*)" run (\d+) script $/.async function (deviceid, num) {
command += "" + num;
return client.shell(deviceid, command)
// Use the readAll() utility to read all the content without
// having to deal with the events. `output` will be a Buffer
// containing all the output.
.then(adb.util.readAll)
.then(function (output) {
console.log('[%s] %s', device.id, JSON.stringify(output.toString().trim()))
})
});
Copy the code
- Click on the Run feature file to see that the monkey automatically operates the emulator’s calculator
If you want to do different Monkey tests on multiple applications, you can simply convert the scene into a scene outline in CukeTest and add multiple lines of data. Real implementation of data-driven Monkey testing.
Welcome to exchange and study together.