Monkey test of App

As a novice in the testing industry, it is necessary for Xiaobai to understand some Monkey tests. In this small compilation, we have sorted out the basis of Monkey tests in App and the compilation of Monkey scripts. I hope it can help you.

1. Monkey concept

Monkey test, also known as "monkey test", means that people without testing experience randomly click on the product. Monkey is a command-line tool of Android system. It can send pseudo-random user event flow (such as keys, touch screen, gestures, etc.) to the tested application to test the application. Monkey is usually used for stress test and stability test.

When is it appropriate to use monkey test?

A: generally, the product is relatively stable. When there are not many bug s, monkey is usually used to test the stability and robustness of the application to be tested (whether flash back, crash and ANR problems will occur)

2. Use monkey

The first part introduces the basic concept of Monkey, so how to use Monkey for testing Xiaobai?
Step 1: you need to install the adb tool on your computer. If you don't know how to install it, you can refer to another blog in Xiaobian Introduction to ADB command
Step 2: find a real machine or simulator. You'd better use the real machine directly. It's more convenient. You need to turn on the USB debugging mode on the mobile phone and connect the computer with the data cable.
Step 3: open the command line and enter the command:
adb shell monkey
perhaps
adb shell monkey --help
After that, you will see a lot of contents in the command line. The following contents are Monkey's help information, mainly the use of parameters in Monkey tool

2.1 use of monkey parameter

First, let's take a look at the event parameter table, which contains some common parameters. Next, let's pick some to introduce the basic usage

parameterexplain
-sFor the seed generated by pseudo-random number, if the same Monkey is run again with the same random seed value, the same event sequence will be generated after two executions
–throttleInserting a fixed delay between two events can slow down the execution speed of Monkey.
–pct-touchSet the percentage of touch screen events generated. A touch screen event is a gesture with a finger pressing and lifting the event
–pct-motionSets the percentage of slip events generated
–pct-trackballSets the percentage of trackball events generated, which are events that contain a series of random move and click events
–pct-navSet the percentage of basic navigation events. Basic navigation events are events that simulate directional device input up, down, left and right
–pct-majornavSet the percentage of main navigation events. Main navigation events usually lead to feedback events in the UI, such as clicking back event and MENU
–pct-syskeysSet the percentage of system key events. System keys mean that these keys are usually reserved and used by the system, such as HOME, BACK, dial, hang up and volume control keys
–pct-appswitchSets the percentage of events that initiate activity. After a random interval, Monkey will execute a start Activity() function to cover all activities in the package as much as possible
–pct-anyeventSet the percentage of other types of events, such as ordinary key messages, infrequent device button events, etc
-pSet one or more package names. Each package name needs to be preceded by a - p
–ignore-crashesGenerally, Monkey will stop running after the application to be tested crashes or any exceptions occur. If this parameter is specified, Monkey will continue to send events to the system after exceptions occur until all the specified event messages are completed.
–ignore-timeoutsGenerally, when any timeout error occurs in the application, monkey will stop running. If this parameter is specified, monkey will continue to send events to the system after generating error messages until all the specified event messages are completed
  1. -p
    Refers to the package that specifies the application to be operated, that is, the apk package.

    adb shell monkey -p packagename 1000
    -p Parameter specifies the package to run,
    1000 Indicates the number of events
     When multiple packages exist
    adb shell monkey -p packagename1 -p packagename2 1000
    
  2. -s
    Random seed is a highly recommended method in the process of testing. If an exception occurs during the first run, you can reproduce the previous exception through random seed,

    adb shell monkey -p packagename -s 10 1000
    
  3. -v
    You can view the process performed by monkey through - v. The Monkey log is divided into three levels: 0, 1 and 2, which correspond to a - v, - v -v and - v -v; If nothing is written in the monkey command, it defaults to level 0.

    0 Level: adb shell monkey -p packagename -v 1000
    1 Level: adb shell monkey -p packagename -v -v 1000
    2 Level: adb shell monkey -p packagename -v -v -v 1000		
    

    Specific case analysis:
    Enter the command: ADB shell monkey - P packagename - V - V 1000
    A lot of contents will be output in the command window, and a section is intercepted below:

    ~ ~ adb shell monkey -p com.qiniu.pili.droid.shortvideo.demo -v -v -v 100
    
      bash arg: -p
      bash arg: com.qiniu.pili.droid.shortvideo.demo
      bash arg: -v
      bash arg: -v
      bash arg: -v
      bash arg: 100
    args: [-p, com.qiniu.pili.droid.shortvideo.demo, -v, -v, -v, 100]
     arg: "-p"
     arg: "com.qiniu.pili.droid.shortvideo.demo"
     arg: "-v"
     arg: "-v"
     arg: "-v"
     arg: "100"
    data="com.qiniu.pili.droid.shortvideo.demo"
    :Monkey: seed=1633995691825 count=100
    :AllowPackage: com.qiniu.pili.droid.shortvideo.demo
    :IncludeCategory: android.intent.category.LAUNCHER
    :IncludeCategory: android.intent.category.MONKEY
    // Selecting main activities from category android.intent.category.LAUNCHER
    //   - NOT USING main activity com.android.browser.launch.SplashActivity (from package com.android.browser)
    //   - NOT USING main activity com.android.calendar.homepage.AllInOneActivity 	(from package com.android.calendar)	
    // Seeded: 1633995691825
    // Event percentages:
    //   0: 15.0%
    //   1: 10.0%
    //   2: 2.0%
    //   3: 15.0%
    //   4: -0.0%
    //   5: -0.0%
    //   6: 25.0%
    //   7: 15.0%
    //   8: 2.0%
    //   9: 2.0%
    //   10: 1.0%
    //   11: 13.0%
    :Switch: #Intent;action=android.intent.action.MAIN;category=android.intent.category.LAUNCHER;launchFlags=0x10200000;component=com.qiniu.pili.droid.shortvideo.demo/.activity.MainActivity;end
        // Allowing start of Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] cmp=com.qiniu.pili.droid.shortvideo.demo/.activity.MainActivity } in package com.qiniu.pili.droid.shortvideo.demo
        // Allowing start of Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] cmp=com.qiniu.pili.droid.shortvideo.demo/.activity.MainActivity } in package com.qiniu.pili.droid.shortvideo.demo
    Sleeping for 0 milliseconds
    	:Sending Touch (ACTION_MOVE): 0:(417.50476,1845.2583)
    	:Sending Touch (ACTION_MOVE): 0:(416.0715,1845.2562)
    	:Sending Touch (ACTION_MOVE): 0:(408.3729,1843.4117)
    	:Sending Touch (ACTION_MOVE): 0:(404.40262,1840.556)
    	:Sending Touch (ACTION_MOVE): 0:(378.433,1833.2344)
    	:Sending Touch (ACTION_UP): 0:(369.19046,1832.6873)
    	Sleeping for 0 milliseconds
    	:Sending Key (ACTION_DOWN): 23    // KEYCODE_DPAD_CENTER
    	:Sending Key (ACTION_UP): 23    // KEYCODE_DPAD_CENTER
    	:Sending Key (ACTION_UP): 82    // KEYCODE_MENU
    	Sleeping for 0 milliseconds
    	:Sending Touch (ACTION_DOWN): 0:(391.0,92.0)
    	:Sending Touch (ACTION_UP): 0:(403.20142,94.02997)
    	Events injected: 100
    	:Sending rotation degree=0, persist=false
    	:Dropped: keys=0 pointers=0 trackballs=0 flips=1 rotations=0
    	## Network stats: elapsed time=601ms (0ms mobile, 0ms wifi, 601ms not connected)
    	// Monkey finished
    ➜  ~ 
    

    Next, analyze the above logs

    :Monkey: seed=1633995691825 count=100
    Monkey When using pseudo-random numbers seed When generating an event sequence, the system will randomly assign a value when no random seed is specified, count Indicates the number of random events
    
    :AllowPackage: com.qiniu.pili.droid.shortvideo.demo
     Indicates start only com.qiniu.pili.droid.shortvideo.demo In package Activity. 
    
    :IncludeCategory: android.intent.category.LAUNCHER
    :IncludeCategory: android.intent.category.MONKEY
     Indicates the intention to start. The weight is " LAUNCHER"And " MONKEY"Activities.
    
    // Event percentages:
    //   0: 15.0%
    //   1: 10.0%
    //   2: 2.0%
    //   3: 15.0%
    //   4: -0.0%
    //   5: -0.0%
    //   6: 25.0%
    //   7: 15.0%
    //   8: 2.0%
    //   9: 2.0%
    //   10: 1.0%
    //   11: 13.0%
    The proportion of various types of events in this pseudo-random event, such as key press, sliding screen and other events, can also set corresponding parameters
    :Switch: #Intent;action=android.intent.action.MAIN;category=android.intent.category.LAUNCHER;launchFlags=0x10200000;component=com.qiniu.pili.droid.shortvideo.demo/.activity.MainActivity;end
     Indicates jump to“ com.qiniu.pili.droid.shortvideo.demo"In package“.activity.MainActivity"This activity.
    
    
    // Allowing start of Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] cmp=com.qiniu.pili.droid.shortvideo.demo/.activity.MainActivity } in package com.qiniu.pili.droid.shortvideo.demo
     Indicates that startup is allowed“ com.qiniu.pili.droid.shortvideo.demo"In package“.activity.MainActivity"This activity.
    
    Sleeping for 0 milliseconds
     express Monkey A delay is allowed when sending various random events, because we did not specify it on the command line Monkey Event messages can only be sent as quickly as possible.
    
    Events injected: 100
     The above information indicates that 100 injection events have been generated because it is set to 100 on the command line
    
    :Sending rotation degree=0, persist=false
     Indicates that the screen rotates. The rotation angle is 0. Whether to keep the rotation state is false.
    
    :Dropped: keys=0 pointers=0 trackballs=0 flips=1 rotations=0
     Indicates the discarded screen rotation related information. Discard: key=0,Pointer=0,track ball=0,Keyboard flick=1,screen flipping=0.
    // Monkey finished
     If you see this sentence in the log, it means that the execution is successful. There is no problem in this process.
    
  4. Log save

    adb shell monkey -p com.qiniu.pili.droid.shortvideo.demo -v -v -v 100 >/Users/Downloads/monkey.log
    
  5. Comprehensive case display
    We know what the parameters in Monkey mean and how to use them. Next, we will show you a comprehensive case:

    adb shell monkey -v -v -v --ignore-crashes --ignore-timeouts --kill-process-after-error --ignore-security-exceptions -p com.qiniu.pili.droid.shortvideo.demo -s 100 --pct-touch 50 --throttle 3000 1000
     Relevant security, timeout, crash and other exceptions are ignored during the test
    --pct-touch 50 Touch screen events are 50%
    --throttle 3000 The event interval is 3 seconds
    -s 100 The seed is 100
    

3. Monkey script

First, let's show you the Monkey script. The name of the script is monkey.txt. The content is as follows:
For the coordinate problem in tap (960274), we can use the uiautomatorviewer tool to solve it. Of course, the simplest way is to set the mobile phone - Developer mode - pointer position, so that we can obtain the coordinates of the button.

type = raw events # Represents the type of script
count = 1 # It is used to describe the number of times of execution, but no matter what is changed, it is only executed once??
speed = 1.0 # Used to adjust the sending frequency of two random events.
start data >> # Start or start data & gt; & gt;

LaunchActivity(com.qiniu.pili.droid.shortvideo.demo) # Package name, interface name
Tap(960,274) ## button coordinates (click)
UserWait(5000) # waiting time
Tap(390,1538)
UserWait(13000)
Tap(780,1583)
UserWait(5000)
Tap(777,1183)
UserWait(5000)
Tap(30,1335)
UserWait(5000)
Tap(30,1456)
UserWait(5000)
Tap(945,81)
UserWait(5000)
DispatchPress(KEYCODE_BACK) # Click the back button

Step 1: write a monkey script and save it as monkey.txt (any name)
Step 2: Push the script to a file on the phone

adb push monkey.txt  /sdcard/

Step 3: run the script

adb shell monkey -f /sdcard/monkey.txt -v -v 1

The above is all about Monkey. I hope it can help you.

Posted by jimmy patel on Thu, 14 Oct 2021 16:13:04 -0700