[Test practical tips] the combined use of adb command and monkey command

Preface

In mobile automated testing, the combined use of adb and monkey commands is usually needed for testing. There are so many adb commands and there are also many monkey commands. The combination is a headache. This article lists some commonly used commands to help you more Mobile automated testing well

Introduction to Monkey

Monkey means "monkey" in English. There is a corresponding term in the testing industry called "monkey test", so what is "monkey test"

  • Android comes with command line tools, which run in the emulator or actual device
  • Send a pseudo-random event stream to the application under test (such as keystrokes, touch screens, gestures, etc.)
  • Use monkey to test the stability and robustness of the application in a random and repeated manner

Monkey principle

  • Use socket communication (Android client and server use TCP/UDP mode)
  • The event stream data stream used is random, and the monkey cannot be customized
  • It is written in Java language and named Monkey.jar
  • Start execution through a shell script named monkey. The storage path of the shell script is: /system/bin/monkey

Monkey command

adb shell monkey [+ 命令参数] [指令数]

The simplest example:

adb shell monkey 100  # 发送 100 个随机指令

The monkey command above sent 100 random commands because no specific application package name was specified. Will be executed randomly throughout the system.

Note: The number of instructions, that is, the value of the random number sent must be placed at the end of all parameters.

Monkey parameters

  • -p is used to restrict restrictions. Use this parameter to specify one or more packages. After specifying the package, all random instructions will be operated on the application. (How to find the package name, please check the end of the article

Example:

adb shell monkey -p com.huomaotv.mobile 100  
# 对“火猫 TV”发送 100 个随机指令

The next 100 commands will be carried out in the Huomao TV application. Note, however, that 100 instructions will be completed in a very fast time .

  • -v is used to specify the level of feedback information (the information level is the level of detail in the log). There are 3 levels in total. The level is determined by the number of the -v parameter in the command. The more the -v parameter, the more detailed the log.

Level 1 : The default value, only provides a small amount of information such as startup prompt, test completion, final result and Crash log

adb shell monkey -p com.huomaotv.mobile -v 100

Level 2 : Provide a more detailed log, including each event information sent to the Activity

adb shell monkey -p com.huomaotv.mobile -v -v 100

Level 3 : The most detailed log, including selected/unselected activity information in the test

adb shell monkey -p com.huomaotv.mobile -v -v -v 100
  • -Throttle The interval time after each event ends. Used to reduce system pressure. If not specified, the system will send the sequence of events as soon as possible
adb shell monkey -p com.huomaotv.mobile --throttle 300 100 
# 每个指令间延时 300 毫秒
  • -s specifies the seed value (number) of the test. If the seed value is the same two times, the random instructions for the two tests are exactly the same. Mainly used to return and reproduce bugs that have appeared. It's a little bit hard to understand here. The instructions of the monkey command are completely random every time, which makes it impossible to reproduce if a problem is found during the test. This also caused us to be unable to determine whether the previous problem was fixed after the development and repair of the problem. In order to ensure that the problem can be reproduced, a seed value is added when the monkey instruction is run. As long as the subsequent instruction carries the seed value, the random commands for the two runs are exactly the same.
adb shell monkey -p com.huomaotv.mobile -s 5555 -v 100

You can modify other parameters of the command. Keep -s 5555 to see that the random commands are the same. If the quantity is modified, the percentage should be consistent with the previous instruction. For example, if the quantity is changed from 100 to 200, the previous percentage is consistent with the previous random instruction

Debug parameters

The following commands are mainly used to control some debugging options during the execution of the monkey command. Among them, --ignore-crashes and --ignoretimeouts are running for a long time (such as running for a few hours after get off work), which can ensure that the remaining instructions can still be executed when problems such as crashes are encountered during operation. Avoid encountering Crash and ANR at the beginning of execution, causing subsequent instructions not to be executed.

parameter Description Remarks
–ignorecrashes Used to specify whether the Monkey stops running when the application crashes. If you use this parameter, even if the application crashes, Monkey will still send events until the event count is completed. Necessary for long-term operation
–ignoretimeouts Used to specify whether the Monkey will stop running when an ANR (Application No Responding) error occurs in the application. If this parameter is used, even if an ANR error occurs in the application, the Monkey will still send events until the event count is completed. Necessary for long-term operation
–ignoresecurityexceptions Used to specify whether the Monkey will stop running when a license error occurs in the application (such as certificate license, network license, etc.). If you use this parameter, even if a license error occurs in the application, Monkey will still send events until the event count is completed
–killprocessafter-erro 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 is only in the state when the error occurs, and the system does not end the process of the application).
-monitornativecrashes Native code used to specify whether to monitor and report application crashes.
-wait-dbg Stop the Monkey in execution until a debugger is connected to it.
-dbg-noevents Set this option, Monkey will perform the initial startup, enter a test Activity, and then no further events will be generated. For the best results, combine it with -v, one or several package constraints, and a non-zero value that keeps the Monkey running for 30 seconds or more to provide an environment to monitor the packages called by the application Conversion between

Example:

adb shell monkey -p com.huomaotv.mobile --ignore-crashes --ignore-timeouts --monitornative-crashes -v 100000

Specify the time type percentage parameter

The commands sent randomly by monkey contain various random events, and various types of events are generated in a certain proportion. If you want to specify more events of a certain type, you can specify the percentage of the event.
For example, the main supported operations in the currently tested application are conventional operations such as touch and slide, so the ratio of these two types of random events can be increased.

Event category Adjustment event description
–pct-touch Adjust the percentage of touch events (touch event is a down-up event, it occurs in a single location on the screen)
–pctmotion Adjust the percentage of sliding events (the action event consists of a down event, a series of pseudo-random events and an up event somewhere on the screen)
–pctpinchzoom Adjust the percentage of zoom event (zoom event is the zoom in and zoom out gesture on the smartphone)
–pcttrackball Adjust the percentage of track events (track events consist of one or several random movements, sometimes accompanied by clicks)
–pctrotation Adjust the ratio of horizontal and vertical screen switching events
–pct-nav Adjust the percentage of "basic" navigation events (navigation events are composed of up/down/left/right from the direction input device)
–pctmajornav Adjust the percentage of "main" navigation events (these navigation events usually trigger actions in the graphical interface, such as: back button, menu button)
–pct-flip Adjust the percentage of keyboard events (keyboard events such as clicking the input box, keyboard popping up, clicking outside the input box, keyboard retracting, etc.)
–Pctsyskeys Adjust the percentage of "system" key events (these keys are usually reserved and used by the system, such as Home, Back, Start Call, End Call and volume control keys)
–pctappswitch Adjust the percentage of starting Activity. At random intervals, Monkey will perform a startActivity() call as a way to maximize coverage of all activities in the package
–pctanyevent Adjust the percentage of other types of events. It includes all other types of events, such as: keystrokes, other infrequently used device buttons, etc.

Example:

# 单个参数设置百分比 
adb shell monkey -p com.huomaotv.mobile --pct-appswitch 20 10000
# 多个参数设置百分比 
adb shell monkey -p com.huomaotv.mobile --pct-appswitch 20 --pct-rotation 20 --pcttouch 30 --pct-motion 20 10000
注意: 参数后面紧接百分比 ,百分比的总量不能大于 100% 

Log analysis

At the end of the command, add> to redirect to a local file. d:/monkey.log is placed in the root directory of drive d

Example:

adb shell monkey -p com.huomaotv.mobile --pct-appswitch 20 --pct-rotation 20 --pcttouch 30 --pct-motion 20 --ignore-crashes --ignore-timeouts --monitor-native-crashes 10000 > d:/monkey.log

So how do we confirm whether there is a bug after the monkey runs? The most intuitive and simple way is to search for the following keywords:

  1. When the program does not respond: search for the keyword "ANR in"
  2. Crash situation 1: Search for the keyword "CRASH"
  3. Crash situation 2: Search for the keyword "Exception"
  4. Memory overflow: search for the keyword "OOM"
  5. Successful test: search for the keyword "Monkey finished"
  6. If there is a NullPointerException, it must be a bug

Conclusion

If you need more information, please pay attention to contact me, and welcome everyone to join the group to exchange problems or experiences encountered in automated testing. Click and enter the password: CSDN

Guess you like

Origin blog.csdn.net/Chaqian/article/details/108736058