参考:https://developer.android.com/tools/help/monkey.html

一、Monkey简介

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

Android SDK中的android调试桥(adb)shell里,有一个用于测试的工具——monkey。不知道最早monkey这个名字的来历,不过他确实如同一只调皮的猴子,在android app里各种乱按、乱晃、乱摸。

Monkey包目录:/system/framework/monkey.jar

Monkey的shell脚本:/system/bin/monkey

二、Monkey特性

Monkey包括许多选项,它们大致分为四大类:

· 基本配置选项,如设置尝试的事件数量。

· 运行约束选项,如设置只对单独的一个包进行测试。

· 事件类型和频率。

· 调试选项。

在Monkey运行的时候,它生成事件,并把它们发给系统。同时,Monkey还对测试中的系统进行监测,对下列三种情况进行特殊处理:

· 如果限定了Monkey运行在一个或几个特定的包上,那么它会监测试图转到其它包的操作,并对其进行阻止。

· 如果应用程序崩溃或接收到任何失控异常,Monkey将停止并报错。

· 如果应用程序产生了应用程序不响应(application not responding)的错误,Monkey将会停止并报错。

三、Monkey的基本操作:

1.基本方法

可以通过开发机器上的命令行或脚本来启动Monkey。由于Monkey运行在模拟器/设备环境中,所以必须用其环境中的shell来进行启动。可以通过在每条命令前加上adb shell来达到目的,也可以进入Shell后直接输入Monkey命令。基本语法如下:

$ adb shell monkey [options] 

如果不指定options,Monkey将以无反馈模式启动,并把事件任意发送到安装在目标环境中的全部包。

下面是一个更为典型的命令行示例,它启动指定的应用程序,并向其发送500个伪随机事件:

$ adb shell monkey -p your.package.name -v 500

注:如果有root权限,可以查看Monekey的测试包:/data/data/

android手机跑monkey android monkey脚本_Monkey

2.脚本方法


1、脚本使用方法


#adb  
 shell 

 

  #monkey  -f  **.script  [-v -v -v] times


说明:


1)、脚本位置可以是绝对位置,也可以是相对位置(cd进入相应的目录)


2)、times指脚本执行的次数


)、v (verbose output mode? 1= verbose, 2=very verbose)--set verbose to allow different level of log



2、脚本的内容示例(源码修正版)


//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行是脚本内容,每行为一个函数


eg:
 
#start script
 
type=user
 
count=10  #这个值要>=10
 
speed=1.0
 
start data >>
 
LauncherActivity(com.android.browser,com.android.browser.BrowerActivity)
 
DispatchPress(KEYCODE_BACK)
 
UserWait(3000)
 
#启动浏览器,返回,休息3秒



3、常用函数介绍


以下列表提炼于源码MonkeySourceScript.java


DispatchKey(downTime,eventTime,action,code,repeat,metaState,device,scancode)
 
    
 

        @param long eventTime  //事件发生的时间                    

 

        @param int action  //(ACTION_DOWN=0,ACTION_UP=1,ACTION_MULTIPLE=2) 

 

        @param int code  //键值,比如KEYCODE_DPAD_DOWN(20)//参考:http://developer.android.com/reference/android/view/KeyEvent.html 

 

        @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 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(float x,float y,duration); ok 
 
 

   DispatchPress(String key_name) ok  
 
 
 

   DispatchFlip(boolean keyboardOpen)      
 
 
 

   UserWait(long sleeptime)   ok 
 
 
 

   LaunchActivity(String pkg_name, String cl_name) ok     
 
 
 

   LongPress() 
 
 
 

   RotateScreen(int rotationDegree,int persist)  ok  //rotationDegress参考:ROTATION_0 ROTATION_90 ROTATION_180 ROTATION_270//http://developer.android.com/reference/android/view/Surface.html


4、解析



ACTION_DOWN:the key has been pressed down.
 
  
the key has been released.
 
   
multiple duplicate key events have occurred in a row, or a complex string is being delivered. If the key code is not {#link KEYCODE_UNKNOWN then the {#link getRepeatCount() method returns the number of times the given key code should be executed. Otherwise, if the key code is KEYCODE_UNKNOWN, then this is a sequence of characters as returned by getCharacters().


3.远程方法


有待完善·····

4.测试结果分析

5.系统源码

development\cmds\monkey

6.案例

adb shell monkey -p com.android.settings --port 1080 -s 8888 --bugreport --pct-touch 50 --pct-motion 50  --pct-syskeys 0 --ignore-crashes --ignore-timeouts --ignore-native-crashes --ignore-security-exceptions --pct-trackball 0 --throttle 300 -v -v -v 300000 > D:/monkey.txt