您的位置:首页 > 移动开发 > Android开发

Android Monkey测试脚本

2015-09-28 11:04 411 查看
转载于:http://blog.csdn.net/quaful/article/details/6863914。

Android 的 monkey test 工具提供了 -f scriptfile 参数,可以指定 test 脚本,然而翻遍了 Android 的网站也没有找到这个脚本的文档,最终只在 monkey 的源码 MonkeySourceScript.java 中找到了一小段注释,里面给了一个不到 10 行例子:
/**
* 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)
* ...
*/
1)前三行是脚本头。count为脚本中包含的指令数目。speed为执行速度。
2)start data后面是执行的命令。

用法:

adb shell monkey -f <script file> <运行脚本的次数>
例如,我们放一个脚本到 /sdcard/monkey.script,然后运行:
adb shell monkey -f /sdcard/monkey.script 10,那么这个脚本里面指定的动作就会被执行10次。
有了这个例子之后稍微好办了些,至少有搜索的关键词了,于是用 captureDispatchPointer 很快就找到了一篇自动化测试的文章:
http://qatesttech.wordpress.com/category/android-monkey-script/ (以下是引用)
"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是打开
剩下具体每个函数的用法,就只能在源码里面慢慢发掘了。另外搜索到一个例子:
http://qatesttech.wordpress.com/2011/05/05/android-my-monkey-script/ # 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)
整理的脚本函数列表:
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_UP=1,ACTION_MOVE=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(long downTime,  long eventTime, int action,

float x, float y, float pressure, float size, int metaState,

float xPrecision, float yPrecision, int device, int edgeFlags)
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  //

DispatchFlip(boolean keyboardOpen)

DispatchPress(int keyCode)

LaunchActivity(String pkg_name, String cl_name)

UserWait(long sleeptime)

LongPress()
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: