当前位置: 首页 > 工具软件 > Monkey > 使用案例 >

Monkey&Monkey Script

东郭自珍
2023-12-01

Android Monkey

一、 Monkey

1.     什么是monkey

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

2.     Monkey用法

基本语法:adb shell monkey [option]

例如:adb shell monkey –v –v –v –p com.android.test 500 (发送500个随机指令对com.android.test这个包进行测试)

option有如下:

[-p ALLOWED_PACKAGE [-p ALLOWED_PACKAGE] ...]

[-c MAIN_CATEGORY [-c MAIN_CATEGORY] ...]

[--ignore-crashes] [--ignore-timeouts]

[--ignore-security-exceptions]

[--monitor-native-crashes] [--ignore-native-crashes]

[--kill-process-after-error] [--hprof]

[--pct-touch PERCENT] [--pct-motion PERCENT]

[--pct-trackball PERCENT] [--pct-syskeys PERCENT]

[--pct-nav PERCENT] [--pct-majornav PERCENT]

[--pct-appswitch PERCENT] [--pct-flip PERCENT]

[--pct-anyevent PERCENT]

[--pkg-blacklist-file PACKAGE_BLACKLIST_FILE]

[--pkg-whitelist-file PACKAGE_WHITELIST_FILE]

[--wait-dbg] [--dbg-no-events]

[--setup scriptfile][-f scriptfile[-f scriptfile] ...]

[--port port]

[-s SEED] [-v [-v] ...]

[--throttle MILLISEC] [--randomize-throttle]

[--profile-wait MILLISEC]

[--device-sleep-time MILLISEC]

[--randomize-script]

[--script-log]

[--bugreport]

其中红色字体标出来的有一种用法为–f scriptfile这里就可以使用写的monkey脚本来实现

 

 

二、 Monkey Script

1.     什么是monkey script

Monkey script是按照一定的语法规则编写有序的用户事件流并适用于monkey命令工具的脚本。

2.     Monkey script编写及运行

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)

* ...

*/

红色部分规定的就是monkey script的编写方法

type指明类型

count说明执行次数,但是这里改成任何值都执行一次,monkey命令可以指定执行次数,这里也就无所谓了

speed命令执行速率。改动无影响,可以通过monkey命令行指定。

以上三个参数改动都没什么影响,一般不做改动。

start data >>相当于一个入口,说明脚本从下面开始执行

 

例:

#Start Script

type = user

count = 49

speed = 1.0

start data >>

LaunchActivity(com.android.browser,com.android.browser.BrowserActivity)

UserWait(5000)

#open renren

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)

 

以上是在k91上浏览器操作的一个脚本。执行步骤为

1.      打开浏览器

2.      打开人人网

3.      关闭浏览器

具体的操作步骤为:

1.      将上述脚本复制到browser.txt

2.      将browser.txt放到sd卡根目录

3.      adb shell monkey –v –v –v –f /sdcard/browser.txt –throttle 1500 100 > monkey.txt

脚本会按照browser.txt发送的指令序列每隔1.5s执行一个指令,执行100遍,并将log存在monkey.txt文件中。

在源码development/cmds/monkey/src/com/android/commands/monkey/MonkeySourceScript.java中规定了如下api可以供用户使用

   privatestaticfinalString EVENT_KEYWORD_POINTER ="DispatchPointer";

 

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)

 

privatestaticfinalString EVENT_KEYWORD_TRACKBALL ="DispatchTrackball";

 

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)

 

 

privatestaticfinalString EVENT_KEYWORD_KEY ="DispatchKey";

 

DispatchKey(long downTime, long eventTime, int action, int code,

   int repeat, int metaState, int device, int scancode)

 

 

privatestaticfinalString EVENT_KEYWORD_FLIP ="DispatchFlip";

 

DispatchFlip(boolean keyboardOpen)

 

 

privatestaticfinalString EVENT_KEYWORD_KEYPRESS ="DispatchPress";

 

DispatchPress(int keyCode)

 

 

   privatestaticfinalString EVENT_KEYWORD_ACTIVITY ="LaunchActivity";

 

LaunchActivity(String pkg_name, String cl_name)

 

 

privatestaticfinalString EVENT_KEYWORD_INSTRUMENTATION ="LaunchInstrumentation";

LaunchInstrumentation(String test_name, String runner_name)

 

 

privatestaticfinalString EVENT_KEYWORD_WAIT ="UserWait";

 

UserWait(long sleeptime)

 

 

privatestaticfinalString EVENT_KEYWORD_LONGPRESS ="LongPress";

 

LongPress(int keyCode)

 

 

privatestaticfinalString EVENT_KEYWORD_POWERLOG ="PowerLog";

 

PowerLog(String power_log_type)

PowerLog(String power_log_type, String test_case_status)

 

privatestaticfinalString EVENT_KEYWORD_WRITEPOWERLOG ="WriteLog";

 

WriteLog: write power log to sdcard

 

privatestaticfinalString EVENT_KEYWORD_RUNCMD ="RunCmd";

 

RunCmd(String cmd):执行cmd命令

 

private


 类似资料: