Introduction and use of Monkey

1. Introduction

        monkey is a command-line tool in Android that runs in an emulator or on an actual device. By sending pseudo-random user events to the system, such as key input, touch screen input, gesture input, etc., the stress test of the application being developed can be realized. Monkey test is a fast and effective method to test the stability and robustness of the software. It is to use tools to simulate human touch screen and input operations, and make a lot of crazy random points on the mobile APP in a short period of time. Pressing randomly, typing randomly, frantically "tossing" this APP.


2. Detailed explanation of the test process

        Start the Monkey.jar program through a Shell script named monkey, and simulate the user's click, slide, input and other operations on the specified APP to stress test the device program at a very fast speed to detect whether the program is abnormal, and then pass log for troubleshooting.
        The basic syntax is as follows:

#monkey的基本语法如下:这是对整台机器的测试;
adb shell monkey [options]

#针对某个应用的monkey测试, 对淘宝点击500次
adb shell monkey -p com.taobao.taobao 500

        Common commands:       

adb devices  # 查看链接的设备

adb shell monkey -p pkgname            对指定包名进行测试
adb shell monkey --throttle milliseconds         指定事件之间的时间间隔,单位是毫秒
adb shell monkey -s seed         伪随机数生成器的seed值,如果用相同的seed值再次运行monkey,将生成相同的事件序列
adb shell monkey -v [-v -v ]         指定日志级别
adb shell monkey --pct-touch        指定触摸事件的百分比
adb shell monkey --pct-motion        指定动作事件的百分比
adb shell monkey --pct-trackball        指定轨迹球事件百分比
adb shell monkey --pct-nav        指定基本导航事件百分比
adb shell monkey --pct-majornav        设定主要导航事件百分比,兼容中间键,返回键,菜单键
adb shell monkey --pct-syskeys        设定系统事件百分比,比如HOME、BACK,拨号及音量调节等。
adb shell monkey --pct-appswitch        设定启动不同应用程序的事件百分比
adb shell monkey --pct-anyevent        设定不常用事件百分比
adb shell monkey --ignore-crashes        忽略奔溃和异常事件
adb shell monkey --ignore-timeouts        忽略超市事件
adb shell monkey --pkg-blacklist-file        设置不需要进行测试的黑名单应用
adb shell monkey --pkg-whitelist-file        设置需要测试的白名单应用;对白名单的应用都进行测试;

adb shell logcat>e:\\log.txt      抓取日志

3. Results Analysis

        The main exceptions and errors in the Monkey test:
        1. Crash: Indicates that the application under test stops or exits abnormally.
        2. ANR: application no response; indicates that there is no response to input events within 5 seconds (for example: button press, screen touch, etc.) 3.
        Error or Exception: indicates that the application encountered an unknown error or exception during system operation.
        4. aborted: Indicates that the application was not started normally or could not be started normally, and the monkey test was forced to abort.
        Suggestions for the monkey test:
        1. Try to turn off all network switches (WiFi and data connections), and inexplicably consume traffic, unless the test process must require the Internet.
        2. Turn on unknown sources under security settings.
        3. The mobile phone is connected to the power supply for charging, and the emulator is out of the scope of this discussion.
        4. Before the test, run the description of the first prompt on the top of the phone, such as input method, file window, apk prompt, etc.
        5. The monkey is only an auxiliary means of the testing process, so you don't have to rely too much on it. You should focus on important tasks such as designing efficient use cases, designing effective user scenarios, performance testing, interface testing, and automated testing that require more human participation.

        General analysis steps:
        1. First find the location where the error occurred
        2. Check the activity between the two switches before the error occurred
        3. Manually execute the event to reproduce the problem
        4. If the above steps cannot be found, when an error occurs, there is Know the seed value, enter the same seed value, and run monkey according to the previous command

        logcat log level

        Verbose: Show all log messages (default).
        Debug: Displays debug log messages that are only useful during development, and lower message levels in this list.
        Info: Shows expected log messages for general usage, and lower message levels in this list.
        Warn: Shows potential problems that are not yet errors, and the lower message levels in this list.
        Error: Shows the problem that has raised the error, and the lower message level in this list.
        Assert: Displays issues that the developer expects will never occur.

        logcat command syntax : adb logcat [option] [filterspecs]
        

#打印默认日志数据
adb logcat
#需要打印日志详细时间的简单数据
adb logcat -v time
#需要打印级别为Error的信息
adb logcat *:E
#需要打印时间和级别是Error的信息
adb logcat -v time *:E

4. Code example

import os

content=os.system('adb shell monkey -p com.taobao.taobao --throttle 10 -s 12345 200').read()


if 'CRASH' in content or 'crashed' in content:
    print('应用程序运行奔溃!')
elif 'ANR' in content or 'NOT RESPONDING' in content:
    print('应用程序运行时卡壳!')
elif 'Execption' in content or 'exception' in content or 'error' in content:
    print('程序运行出错!')
elif 'aborted' in content:
    print('应用程序未正常运行!')
else:
    print('正常运行!')    

Supongo que te gusta

Origin blog.csdn.net/guanrongl/article/details/124952767
Recomendado
Clasificación