Monkey Test ===android Test Tool Monkey Usage introduction (reprint)

Source: Internet
Author: User
Tags log log

Monkey is a command-line tool in Android that can be run in the emulator or in the actual device. It sends pseudo-random user event streams (such as key input, touch input, gesture input, etc.) to the system, enabling stress testing of the application under development. Monkey testing is a fast and effective method to test the stability and robustness of the software.

Monkey Test Parameter recommendations

    • Interval time: 500 milliseconds;
    • Number of seeds: random;
    • Error encountered: Do not stop
    • Execution time: No less than 12 hours per model or number of clicks: 1 million times;
    • Model coverage recommendations: Cover high-end low-level models, different chip platforms, different resolutions, different versions of Android;
    • Reference command:
      adb shell monkey -p com.package.xxx --throttle 500 --ignore-crashes --ignore-timeouts --ignore-security-exceptions --ignore-native-crashes --monitor-native-crashes -v -v -v 1000000>G:\MonkeyTest.log 2>&1 &

Monkey Test Solution

    • Single APK test:
      monkey –p <options> -c <options> -s <seed> <限制语句> --throttle <milliseconds> -v 执行次数> G:\MonkeyTest.log

    • Set APK test:

      • Outside the blacklist apk:
        monkey –pkg-blacklist-file /data/local/tmp/blacklist.txt -c <options> -s <seed> <限制语句> --throttle <milliseconds> -v 执行次数>G:\MonkeyTest.log

      • Whitelist APK:
        monkey –pkg-whitelist-file /data/local/tmp/whitelist.txt -c <options> -s <seed> <限制语句> --throttle <milliseconds> -v 执行次数 > G:\MonkeyTest.log

Monkey Test Scripts

The Monkey Test tool for Android provides the-F scriptfile parameter, which allows you to specify the test script

  • What is Monkey script
    Monkey script is an ordered stream of user events that is written according to certain grammatical rules and is applicable to the Monkey command tool.
    adb shell monkey -f <script file> <运行脚本的次数>

    • Example: adb shell monkey-f/sdcard/monkey.script 10, then the action specified in this script will be executed 10 times
      Description: The script location can be either an absolute position or a relative position (the CD enters the appropriate directory
  • development/cmds/monkey/src/com/android/commands/monkey/MonkeySourceScript.javaThere is a note under the source code that stipulates the basic rules for Monkey script:

    Monkey event queue. It takes a script to produce events sample script Format:type= Raw Events count=10 speed= 1.0 start data >> capturedispatchpointe R (5109520,5109520,0, 230.75429,458.1814,0.20784314, 0.06666667, 0,0.0,0.0,65539,0" Capturedispatchkey (5113146,5113146,0, 20,0,0,0, 0) Capturedispatchflip (true)  ... */    
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
      • 10
      • 11
      • 12
      • 13
  • Script Content Example (source code revision)

    //header 
    type= raw events 
    count= 10 
    speed= 1.0 
    //line at the end of the header means that below it is the context of script 
    start data >> 
    DispatchPointer(5109520,5109520,0,230.75429,458.1814,0.20784314, 
    0.06666667,0,0.0,0.0,65539,0) 
    DispatchKey(5113146,5113146,0,20,0,0,0,0) 
    DispatchFlip(true) 
    ... 

    Description
    1), the first 3 lines is the script header
    2), after 3 lines is the script content, each behavior a function

  • The functions provided in Monkey are as follows:

    1..DispatchPointer (Long Downtime,Long Eventtime,int action,float x,Float y,float pressure,Float size,int MetaState,Float xprecision,Float yprecision,int device,int edgeflags)2..DispatchTrackball (Long Downtime,Long Eventtime,int action,float x,Float y,float pressure,Float size,int MetaState,Float xprecision,Float yprecision,int device,int edgeflags) trackball 3..dispatchkey ( long downtime, long eventtime,  int action, int Code, int Repeat,  int metaState, int device, int Scancode) 4..dispatchflip (boolean Keyboardopen) entity keyboard off with open 5..dispatchpress (int keycode) 6. Launchactivity (String pkg_name, String cl_name) 7..userwait ( Long sleeptime) 8..longpress (int keycode)    
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
    • The keyboard presses/bounces up
      key [down|up] keycode
      This command simulates keyboard input once.
      The KeyCode parameter values are described in the KeyCode list of the KeyEvent class. The format of this parameter is very flexible, for example, the Simulation menu button can use 82 (the key value of the menu button), you can use Keycode_menu (the name of the key value, must be strictly uppercase letters), you can also use the menu (Monkey program will automatically add KeyCode parts, It is not case-sensitive at this time).
      Note that a full strike (press) operation is a combination of pressing (key down) and bouncing (key up)
    • Touch Press/Bounce/move
      touch [down|up|move] x y 
      This command injects a MotionEvent into the input system that simulates a user touching the touchscreen (or a pointer event). x and y specify coordinates on the display (0 0 being the upper left) for the touch event to happen. Just like key events, touch events at a single location require both a down and an up. To simulate dragging, send a “touch down”, then a series of “touch move” events (to simulate the drag), followed by a “touch up” at the final location.
    • Rolling Track Ball
      trackball dx dy 
      This command injects a MotionEvent into the input system that simulates a user using the trackball. dx and dy indicates the amount of change in the trackball location (as opposed to exact coordinates that the touch events use)
    • Turn on/off the physical keyboard
      flip [open|close]
      Simulates the turn on/off of an entity keyboard at once.
      This command is not valid on a phone that does not have a physical keyboard.
    • Wake-up device (lit screen)
      wake
      This command wakes the device and allows the user to enter it.
      If the device keyboard is locked, this command does not unlock the keyboard
    • Screen click
      tap x y
      This command simulates a screen click.
      This command is a combination of the touch down and touch up commands.
    • Tapping the keyboard
      press keycode
      This command simulates a keyboard strike.
      This command is a combination of the key down and the key up command.
    • Type a string
      type string
      This command simulates typing a string
      This command fully simulates each character's keyboard event
      List environment variables
    • Listvar
      This command lists all environment variables for monkey.
      The return value is a space-delimited string.
    • Get environment variable Value
      getvar varname
      This command is used to get the value of the specified environment variable.
      Gets a list of supported environment variables through the Listvar command.
    • Exit Monkey
      quit
      Exit Monkey completely
      The monkey no longer accepts new connections.
    • End Current session
      done
      Ends the current session.
      The monkey can also accept new connections.
    • Dormancy
      sleep ms
      Causes the monkey to hibernate for a period of time, the parameter is an integer, the unit milliseconds
    • Comments
      #
      Statements beginning with # are monkey as comments. Sending such a command monkey will not return an error and will not return OK.
  • Introduction to Common functions
    The following list is used to extract the source codeMonkeySourceScript.java
    Dispatchkey (Downtime,eventtime,action,code,repeat,metastate,device,scancode)
    @param long downtime//key was initially pressed
    @param long Eventtime//event occurred
    @param int action//(action_down=0,action_up=1,action_multiple=2)
    @param int code//key value, e.g. Keycode_dpad_down (20)
    @param int repeat//
    @param int metaState//identification of the current pressed meta key
    Device ID for the @param int device//Event
    @param int Scancode//
    Dispatchpointer (Downtime, eventtime,action, x, y, pressure, size, metaState, Xprecision,yprecision,device, EdgeFlags)
    @param long downtime//key was initially pressed
    @param long Eventtime//event occurred
    @param int action//(ACTION_DOWN=0,ACTION_MOVE=1,ACTION_UP=2,ACTION_CANCEL=3)
    @param float x//x coordinates
    @param float y//y coordinates
    @param float pressure//Current event pressure, range 0-1
    @param the approximate value of float size//touch, Range 0-1
    @param int metaState//identification of the current pressed meta key
    @param float xprecision//x coordinate precise values
    @param float yprecision//y coordinate precise values
    @param int device//event source, range 0-x,0 means not from physical device
    @param int Sedgeflags//
    Dispatchtrackball (Downtime, eventtime,action, x, y, pressure, size, metaState, Xprecision,yprecision,device, EdgeFlags )
    Tap (x,y,duration);
    Dispatchpress (String key_name)
    Dispatchflip (Boolean Keyboardopen)
    Userwait (Long Sleeptime)
    Launchactivity (String pkg_name, String Cl_name,long alarmtime)
    Userwait (Long Sleeptime)
    Longpress ()

Example:

    • start script 
      type = user 
      count = 49 
      Speed = 1.0 
      Start Data >>&n Bsp
      Launchactivity (com.android.browser,com.android.browser.browseractivity)  
      Userwait ()
      &NBSP
      #back to Home  
      Capturedispatchpointer ( 5109520,5109520,0,1150,330,0,0,0,0,0,0,0);  
      Capturedispatchpointer ( 5109521,5109521,1,1150,330,0,0,0,0,0,0,0);  
      Userwait (+)
       
      #close Browser  
      capturedispatchpointer (5109520,5109520,0,205,31,0,0,0,0,0,0,0);  
      Capturedispatchpointer (5109521,5109521,1,205,31,0,0,0,0,0,0,0);  
      Userwait (+)
       
      The above is a sample script.  
      The specific steps are:  
      1. Copy the above script to Script.txt
      2. place script.txt in the SD card root
      3. adb shel L monkey–v–v–v–f/sdcard/script.txt–throttle > Monkey.txt  
        Script executes every 1.5s of instruction sequence sent by script.txt An instruction that executes 100 times and the log exists in the Monkey.txt file

Monkey Test Results Analysis

  • Android platform applications may produce the following four types of crash:

    • App Layer:
      1. Force Close Crash
      2. ANR Crash
    • Native Layer:
      3, Tombstone Crash (Native Crash)
    • Kernel layer:
      4, Kernel Panic
  • Main Monkey Log

    • ANR Directory: Logs exported from the mobile phone/DATA/ANR to save information on the occurrence of ANR crash;
    • Dontpanic directory: From the mobile phone/data/dontpanic/exported logs, save the occurrence of kernel panic when the relevant information;
    • Tombstone directory:/data/tombstones/exported from the mobile phone log, save the error message when tombstone crash occurred;
    • Dropbox directory: The logs exported from the mobile phone/data/system/dropbox/, some tombstones error messages intercepted by the Dropbox service;
    • BugReportLog.log: Various information of the system extracted through adb shell bugreport command;
    • MonkeyTest.Log: Save Monkey Test process, Application layer error message, when native crash occurs, this file will also be recorded;
  • General test Results Analysis:
    After the monkey test error occurs, the general error steps are the following:

    • Find out which part of the monkey is wrong.
    • See some of the event actions before the error in monkey, and perform the action manually
    • If the above steps cannot be found, you can do it again using the previously executed Monkey command, noting that the seed value is the same
    • In the MonkeyTest.Log log file search keyword "Fatal", "Crash", "ANR" to locate the detailed stack information that occurred Crash, or to analyze the log events before and after the Crash;
    • Check the Dropbox directory for relevant crash log information, focusing on the following 4 types of crash error messages:
      data_app_wtf,,, data_app_anr data_app_crashsystem_server_watchdog
    • Check that the tombstone directory has a build log, and some words indicate that native has occurred crash
    • Further analysis of the cause of the problem through log files or BugReport.log log files in the ANR directory;

Extension of the Monkey test

The Monkey tool does not implement a specified module test, and the following records two ways to implement a specified module test

Method 1:
from com.android.monkeyrunner import MonkeyRunner, MonkeyDevice 
from random import randint 
print("get device") 
device = MonkeyRunner.waitForConnection() 
package = ‘com.swl.a1vod‘ 
activity = ‘org.coolx.videoframework.vod.VodDetailsActivity‘ 
runComponent = package + ‘/‘ + activity 
device.startActivity(component=runComponent)

#use commands like device.touch and device.drag to simulate a navigation and open my activity
#with your activity opened start your monkey test
print("start monkey test") 
for i in range(1, 1000): 
#here i go emulate only simple touchs, but i can emulate swiper keyevents and more... :D 
device.touch(randint(0, 1000), randint(0, 800), ‘DOWN_AND_UP‘) 
print("end monkey test")

#执行monkeyrunner G:\script\monkeytest.py

Method 2:

<activity android:name="MonkeyActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.MONKEY" />
</intent-filter>
</activity>

adb shell monkey -p my.package -c android.intent.category.MONKEY -v 500

More Resources
1, Monkey
2. –log analysis of common Android crash problem
3, Analysis Bugreport
4. How to analyze and solve Android ANR
5. Log analysis and location of Android Tombstone crash
6. log file Analysis in Android
7. Watchdog kills system service in System_server
8. Android ANR exception and how to solve it

Monkey Test ===android Test Tool Monkey Usage introduction (reprint)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.