Monkey测试流程及方法

 

 

 

Monkey测试流程及方法

 

 

 

 

 

 

密级:机密

生效日期:

 

状态:内部使用

总页数

 

正文

 

附录

 

编制:

审核:

批准:

 

 

 

本公司对本文件资料享受著作权及其它专属权利,未经书面许可,不得将该等文件资料(其全部或任何部分)披露予任何第三方,或进行修改后使用。

 

 

 

 

 

 

 

 

 

 

文件更改摘要

 

修订日期

版本号

修订说明及页码

修订人

审核人

批准人

2012-4-29

1.0

初始版本

徐小东

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

                                目   录

一、 Monkey介绍 4

二、 Monkey目的 4

三、 Monkey使用参数及说明 4

四、 Monkey使用举例及说明 9

五、 Monkey脚本的参考资料 9

 

 

 

 

 

 

 

 

 

 

  • Monkey介绍

Monkey就是能模拟黑盒测试的手法进行按量按时自动随机测试。当然也可以按动作及固定坐标点进行测试,需要编写monkey测试脚本;这里的模拟动作主要包括触摸事件、滑轮事件、按键事件、翻屏事件及长按事件,可以完成上千次甚至上万次的测试并且保证其事件速度、错误信息、间隔时间皆可以控制。

 

  • Monkey目的
  1. 完成程序的健壮性测试
  2. 容易找到程序的错误且可按参数值重复执行的测试
  3. Monkey测试容易理解并且方便执行
  4. 良好的log信息方便跟踪查询
  5. 全自动化的测试方法
  6. 适用于Android开发的任意开发阶段的测试
  • Monkey使用参数及说明

1.Monkey命令列表:

monkey [-p ALLOWED_PACKAGE [-p ALLOWED_PACKAGE] ...]
[-c MAIN_CATEGORY [-c MAIN_CATEGORY] ...]
[--ignore-crashes] [--ignore-timeouts]
[--ignore-security-exceptions] [--monitor-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]
[--wait-dbg] [--dbg-no-events] [-f scriptfile]
[--port port]
[-s SEED] [-v [-v] ...] [--throttle MILLISEC]
COUNT

2.Monkey命令解释

--help
列出简单的用法。
-v
命令行的每一个 -v 将增加反馈信息的级别。 Level 0( 缺省值 ) 除启动提示、测试完成和最终结果之外,提供较少信息。 Level 1 提供较为详细的测试信息,如逐个发送到 ActivITy 的事件。 Level 2 提供更加详细的设置信息,如测试中被选中的或未被选中的 Activity 。
事件
-s <seed>
伪随机数生成器的 seed 值。如果用相同的 seed 值再次运行 Monkey ,它将生成相同的事件序列
--throttle <milliseconds>
在事件之间插入固定延迟。通过这个选项可以减缓 Monkey 的执行速度。如果不指定该选项, Monkey 将不会被延迟,事件将尽可能快地被产成。
--pct-touch <percent>
调整触摸事件的百分比(触摸事件是一个down-up事件,它发生在屏幕上的某单一位置)。
--pct-motion <percent>
调整动作事件的百分比(动作事件由屏幕上某处的一个down事件、一系列的伪随机事件和一个up事件组成)。
--pct-trackball <percent>
调整轨迹事件的百分比(轨迹事件由一个或几个随机的移动组成,有时还伴随有点击)。
--pct-nav <percent>
调整“基本”导航事件的百分比(导航事件由来自方向输入设备的up/down/left/right组成)。
--pct-majornav <percent>
调整“主要”导航事件的百分比(这些导航事件通常引发图形界面中的动作,如:5-way键盘的中间按键、回退按键、菜单按键)
--pct-syskeys <percent>
调整“系统”按键事件的百分比(这些按键通常被保留,由系统使用,如Home、Back、Start Call、End Call及音量控制键)。
--pct-appswitch <percent>
调整启动Activity的百分比。在随机间隔里,Monkey将执行一个startActivity()调用,作为最大程度覆盖包中全部Activity的一种方法。
--pct-anyevent <percent>
调整其它类型事件的百分比。它包罗了所有其它类型的事件,如:按键、其它不常用的设备按钮、等等。

约束限制
-p <allowed-package-name>
如果用此参数指定了一个或几个包,Monkey将只允许系统启动这些包里的Activity。如果你的应用程序还需要访问其它包里的Activity(如选择取一个联系人),那些包也需要在此同时指定。如果不指定任何包,Monkey将允许系统启动全部包里的Activity。要指定多个包,需要使用多个 -p选项,每个-p选项只能用于一个包。
-c <main-category>
如果用此参数指定了一个或几个类别,Monkey将只允许系统启动被这些类别中的某个类别列出的Activity。如果不指定任何类别,Monkey将选择下列类别中列出的Activity: Intent.CATEGORY_LAUNCHER或Intent.CATEGORY_MONKEY。要指定多个类别,需要使用多个-c选项,每个-c选项只能用于一个类别。
调试
--dbg-no-events
设置此选项,Monkey将执行初始启动,进入到一个测试Activity,然后不会再进一步生成事件。为了得到最佳结果,把它与-v、一个或几个包约束、以及一个保持Monkey运行30秒或更长时间的非零值联合起来,从而提供一个环境,可以监视应用程序所调用的包之间的转换。
--hprof
设置此选项,将在Monkey事件序列之前和之后立即生成profiling报告。这将会在data/misc中生成大文件(~5Mb),所以要小心使用它。
--ignore-crashes
通常,当应用程序崩溃或发生任何失控异常时,Monkey将停止运行。如果设置此选项,Monkey将继续向系统发送事件,直到计数完成。
--ignore-timeouts
通常,当应用程序发生任何超时错误(如“Application Not Responding”对话框)时,Monkey将停止运行。如果设置此选项,Monkey将继续向系统发送事件,直到计数完成。
--ignore-security-exceptions
通常,当应用程序发生许可错误(如启动一个需要某些许可的Activity)时,Monkey将停止运行。如果设置了此选项,Monkey将继续向系统发送事件,直到计数完成。
--kill-process-after-error
通常,当Monkey由于一个错误而停止时,出错的应用程序将继续处于运行状态。当设置了此选项时,将会通知系统停止发生错误的进程。注意,正常的(成功的)结束,并没有停止启动的进程,设备只是在结束事件之后,简单地保持在最后的状态。
--monitor-native-crashes
监视并报告Android系统中本地代码的崩溃事件。如果设置了--kill-process-after-error,系统将停止运行。
--wait-dbg
停止执行中的Monkey,直到有调试器和它相连接。

使用说明

 

 

 

 

  • Monkey使用举例及说明
  1. 默认monkey随机值的方法

测试music 500次随机事件

Monkey -p com.android.music -v 500 

2.随机产生200种事件,其中syskeys占有比例90%

Monkey -p com.android.calculator2 -pct-syskeys 90 -v 200

3. Monkey -p com.android.calculator2 --throttle 1000 -v 500 (间隔时间为1000ms,执行500次随机动作)

4.Monkey -p com.android.music -p com.android.gallery --throttle 100 1000(次数)--程序不能提示有错误 ---是串行执行而不是并行执行(?)

 

备注:另外一种写xp dos执行方法未试验

Adb shell monkey -s 0 -v -v -v -p com..android.music -p com.android.gallery --pct-trackball 0 --throttle 300 10000 e

控制事件值

Monkey -p com.zhonghong.android -v -v 5000 --throtle 1000 --pct-touch 20% --pct-traceball 1%

  • 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)
DispatchTrackball same as DispatchPointer
DispatchKey(long downTime, long eventTime, int action, int code,
    int repeat, int metaState, int device, int scancode)
DispatchFlip(boolean keyboardOpen)
DispatchPress(int keyCode)
LaunchActivity(String pkg_name, String cl_name)
UserWait(long sleeptime)
LongPress()

 

2.命令解释:

 

"DispatchPointer"; 

"DispatchTrackball"; 

"DispatchKey";

"DispatchFlip";

  

按键事件:参见Android.view KeyEvent.java   

DispatchKey(downTime, //@param: The time (in {@link Android.os.SystemClock#uptimeMillis}) at which this key code originally went down.毫秒   

eventTime,     //at which this event happened.                      

action,        //Action code: either {@link #ACTION_DOWN=0}, {@link #ACTION_UP=1}, or {@link #ACTION_MULTIPLE=2}.   

code,    //The key code. 见附录1, 比如KEYCODE_DPAD_DOWN(20)KEYCODE_DPAD_UP(19)   

repeat, //A repeat count for down events (> 0 if this is after the initial down) or event count for multiple events.   

metaState, //Flags indicating which meta keys are currently pressed.   

device, //The device ID that generated the key event.   

scancode) //Raw device scan code of the event.   

   

DispatchPointer,DispatchTrackball(downTime, eventTime,   

                        action, x, y, pressure, size, metaState, xPrecision, yPrecision,   

                        device, edgeFlags);   

@action The kind of action being performed -- one of either{@link #ACTION_DOWN=0}, {@link #ACTION_MOVE=1}, {@link #ACTION_UP=2}, or{@link #ACTION_CANCEL=3}.   

@param x The X coordinate of this event.   

@param y The Y coordinate of this event.   

@param pressure The current pressure of this event.  The pressure generally ranges from 0 (no pressure at all) to 1 (normal pressure), however values higher than 1 may be generated depending on the calibration of the input device.   

@param size: A scaled value of the approximate size of the area being pressed touched with the finger. The actual value in pixels corresponding to the finger touch is normalized with a device specific range of values and scaled to a value between 0 and 1.   

@param metaState The state of any meta / modifier keys that were in effect when the event was generated.   

@param xPrecision The precision of the X coordinate being reported.   

@param yPrecision The precision of the Y coordinate being reported.   

@param deviceId The id for the device that this event came from.  An id of zero indicates that the event didn't come from a physical device; other numbers are arbitrary and you shouldn't depend on the values.   

@param edgeFlags A bitfield indicating which edges, if any, where touched by this MotionEvent   

captureDispatchFlip(true)是否打开滑盖,true是打开  

 

 

 

2.脚本范例如下

# Start of Script   

type= user   

count= 49   

speed= 1.0   

start data >>   

LaunchActivity(com.mpowerlabs.coin.Android, com.mpowerlabs.coin.android.LoginActivity)   

# 3120021258   

DispatchPress(KEYCODE_3)   

UserWait(200)   

DispatchPress(KEYCODE_1)   

UserWait(200)   

DispatchPress(KEYCODE_3)   

UserWait(200)   

DispatchPress(KEYCODE_5)   

UserWait(200)   

DispatchPress(KEYCODE_0)   

UserWait(200)   

DispatchPress(KEYCODE_2)   

UserWait(200)   

DispatchPress(KEYCODE_1)   

UserWait(200)   

DispatchPress(KEYCODE_2)   

UserWait(200)   

DispatchPress(KEYCODE_5)   

UserWait(200)   

DispatchPress(KEYCODE_8)   

UserWait(200)   

# Pin 12345   

DispatchPress(KEYCODE_DPAD_DOWN)   

UserWait(250)   

DispatchPress(KEYCODE_1)   

UserWait(200)   

DispatchPress(KEYCODE_2)   

UserWait(200)   

DispatchPress(KEYCODE_3)   

UserWait(200)   

DispatchPress(KEYCODE_4)   

UserWait(200)   

DispatchPress(KEYCODE_5)   

UserWait(200)   

# Down and enter   

DispatchPress(KEYCODE_DPAD_DOWN)   

UserWait(250)   

DispatchPress(KEYCODE_ENTER)

 

3.执行方法

Adb shell monkey -f /sdcard/monkey.script

 

 

 

 

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值