Android测试工具Monkey用法简介

Monkey是Android中的一个命令行工具,可以运行在模拟器里或实际设备中。它向系统发送伪随机的用户事件流(如按键输入、触摸屏输入、手势输入等),实现对正在开发的应用程序进行压力测试。Monkey测试是一种为了测试软件的稳定性、健壮性的快速有效的方法。

Monkey测试参数建议

  • 间隔时间:500毫秒;
  • 种子数:随机;
  • 遇到错误:不停止
  • 执行时长:每机型不小于12小时 或 点击次数:100万次;
  • 机型覆盖建议:覆盖高中低端机型,不同芯片平台,不同分辨率,不同安卓版本;
  • 参考命令:
    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测试方案

  • 单一apk测试:
    monkey –p <options> -c <options> -s <seed> <限制语句> --throttle <milliseconds> -v 执行次数> G:\MonkeyTest.log

  • 集合apk测试:

    • 黑名单以外的APK:
      monkey –pkg-blacklist-file /data/local/tmp/blacklist.txt -c <options> -s <seed> <限制语句> --throttle <milliseconds> -v 执行次数>G:\MonkeyTest.log

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


Monkey测试脚本

Android 的 monkey test 工具提供了 -f scriptfile 参数,可以指定 test 脚本

  • 什么是monkey script
    Monkey script是按照一定的语法规则编写有序的用户事件流并适用于monkey命令工具的脚本
    adb shell monkey -f <script file> <运行脚本的次数>

    • 例:adb shell monkey -f /sdcard/monkey.script 10,那么这个脚本里面指定的动作就会被执行10次
      说明:脚本位置可以是绝对位置,也可以是相对位置(cd进入相应的目录
  • development/cmds/monkey/src/com/android/commands/monkey/MonkeySourceScript.java源码下有一段注释规定了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 >> 
         captureDispatchPointer(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) 
          ... 
    */  
  • 脚本的内容示例(源码修正版)

    //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)
    ...

    说明:
    1)、前3行是脚本头
    2)、后3行是脚本内容,每行为一个函数

  • monkey中提供的函数如下:

    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)    轨迹球
    3..DispatchKey(long downTime, long eventTime, int action, int code, int repeat, int metaState, int device, int scancode)     
    4..DispatchFlip(boolean keyboardOpen)    实体键盘关闭与打开
    5..DispatchPress(int keyCode)    
    6..LaunchActivity(String pkg_name, String cl_name)  
    7..UserWait(long sleeptime)    
    8..LongPress(int keyCode)
    • 键盘按下/弹起
      key [down|up] keycode
      这个命令模拟一次键盘输入。
      keycode参数值详见KeyEvent类的KEYCODE列表。这个参数的格式很灵活,例如模拟菜单按钮可以使用82(菜单按钮的键值),也可以使用 KEYCODE_MENU(键值的名称,必须严格都是大写字母),还可以使用menu(Monkey程序会自动添加KEYCODE部分,此时并不区分大小 写)。
      注意一次完整的敲击(press)操作是一个按下(key down)和弹起(key up)的组合
    • 触摸按下/弹起/移动
      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.
    • 滚动轨迹球
      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)
    • 打开/关闭实体键盘
      flip [open|close]
      模拟一次实体键盘的打开/关闭。
      在没有实体键盘的手机上此命令无效。
    • 唤醒设备(点亮屏幕)
      wake
      这个命令将唤醒设备,并允许用户输入。
      如果设备键盘已锁,这个命令并不能解锁键盘
    • 屏幕点击
      tap x y
      这个命令模拟一次屏幕点击。
      这个命令就是touch down和touch up命令的一次组合。
    • 敲击键盘
      press keycode
      这个命令模拟一次键盘敲击。
      这个命令就是key down和key up命令的一次组合。
    • 键入字符串
      type string
      这个命令模拟键入一个字符串
      该命令会完全模拟每一个字符的键盘事件
      列出环境变量
    • listvar
      该命令将列出Monkey的所有环境变量。
      返回值为空格分隔的字符串。
    • 获取环境变量值
      getvar varname
      该命令用于获取指定的环境变量的值。
      通过listvar命令获取支持的环境变量列表。
    • 退出Monkey
      quit
      完全退出Monkey
      Monkey不再接受新的连接。
    • 结束当前会话
      done
      结束当前会话。
      Monkey还可以接受新的连接。
    • 休眠
      sleep ms
      使Monkey进入休眠一段时间,参数为整数,单位毫秒
    • 注释
      #
      以#开头的语句会被Monkey当做注释。发送这样的命令Monkey不会返回错误也不会返回OK。
  • 常用函数介绍
    以下列表提炼于源码MonkeySourceScript.java
    DispatchKey(downTime,eventTime,action,code,repeat,metaState,device,scancode)
    @param long downTime //键最初被按下的时间
    @param long eventTime //事件发生的时间
    @param int action //(ACTION_DOWN=0,ACTION_UP=1,ACTION_MULTIPLE=2)
    @param int code //键值,比如KEYCODE_DPAD_DOWN(20)
    @param int repeat //
    @param int metaState //当前按下的meta键的标识
    @param int device //事件发生的设备id
    @param int scancode //
    DispatchPointer(downTime, eventTime,action, x, y, pressure, size, metaState, xPrecision,yPrecision,device, edgeFlags)
    @param long downTime //键最初被按下的时间
    @param long eventTime //事件发生的时间
    @param int action //(ACTION_DOWN=0,ACTION_MOVE=1,ACTION_UP=2,ACTION_CANCEL=3)
    @param float x //x坐标
    @param float y //y坐标
    @param float pressure //当前事件的压力,范围0-1
    @param float size //触摸的近似值,范围0-1
    @param int metaState //当前按下的meta键的标识
    @param float xPrecision //x坐标精确值
    @param float yPrecision //y坐标精确值
    @param int device //事件来源,范围0-x,0表示不来自物理设备
    @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()

示例:

  • Start Script
    type = user
    count = 49
    speed = 1.0
    start data >>
    LaunchActivity(com.android.browser,com.android.browser.BrowserActivity)
    UserWait(5000)

    #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(3000)

    #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(2000)

    以上是一个示例脚本。
    具体的操作步骤为:
    1. 将上述脚本复制到script.txt中
    2. 将script.txt放到sd卡根目录
    3. adb shell monkey –v –v –v –f /sdcard/script.txt –throttle 1500 100 > monkey.txt
      脚本会按照script.txt发送的指令序列每隔1.5s执行一个指令,执行100遍,并将log存在monkey.txt文件中

Monkey测试结果分析

  • Android平台应用程序可能产生以下四种Crash:

    • App层:
      1、Force Close Crash
      2、ANR Crash
    • Native层:
      3、Tombstone Crash(Native Crash)
    • Kernel层:
      4、Kernel Panic
  • 主要Monkey日志

    • anr目录:从手机/data/anr导出的日志,保存发生anr crash 时的相关信息;
    • dontpanic目录:从手机/data/dontpanic/导出的日志,保存发生Kernel Panic时的相关信息;
    • Tombstone目录:从手机/data/tombstones/导出的日志,保存发生Tombstone Crash时的错误信息;
    • dropbox目录:从手机/data/system/dropbox/导出的日志,经过dropbox服务截取的部分tombstones错误信息;
    • BugReportLog.log:通过adb shell bugreport命令提取的系统各种信息;
    • MonkeyTest.Log:保存Monkey测试过程、应用层错误信息,发生Native Crash时,在此文件也会有记录;
  • 一般的测试结果分析:
    Monkey测试出现错误后,一般的差错步骤为以下几步:

    • 找到是monkey里面的哪个地方出错
    • 查看Monkey里面出错前的一些事件动作,并手动执行该动作
    • 若以上步骤还不能找出,可以使用之前执行的monkey命令再执行一遍,注意seed值要一样
    • 在MonkeyTest.Log日志文件搜索关键词“Fatal”、“Crash”、“ANR”定位到发生Crash的详细堆栈信息,或分析发生Crash前后的日志事件;
    • 检查dropbox目录下是否有相关crash日志信息,主要关注是否有以下4类crash错误信息:
      data_app_wtfdata_app_anrdata_app_crashsystem_server_watchdog
    • 检查tombstone目录是否有生成日志,有的话说明发生过native crash
    • 通过anr目录中的日志文件或BugReport.log日志文件,进一步分析问题原因;

Monkey测试的延伸

monkey工具不能实现指定模块测试,以下记录两种实现指定模块测试的方法

方法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

方法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


更多参考资料
1、Monkey
2、android 常见死机问题–log分析
3、分析bugreport
4、如何分析解决Android ANR
5、Android Tombstone Crash的log分析和定位
6、Android中对Log日志文件的分析
7、Watchdog kills system service in system_server
8、android ANR异常及其解决方式

  • 4
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值