Android 的monkey test 工具提供了 -f scriptfile 参数,可以指定 test 脚本。在 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)
- * ...
- */
monkey中提供的函数如下:
- 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(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(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(int keyCode)
首先本地编写需要的测试的事件 命名为 monkey.script (文件格式无要求),将文件push到手机或模拟器的sdcard中
如: adb push lujing/monkey.script /sdcard/
然后执行脚本:
adb shell monkey -v -f /sdcard/monkey.script
附 Example:
type= user
count= 49
speed= 1.0
start data >>
LaunchActivity(com.example.android.notepad, com.example.android.notepad.NotesList)
DispatchPress(KEYCODE_DPAD_DOWN)
LongPress(KEYCODE_DOWN)
DispatchPress(KEYCODE_BACK)
其中 type值可以任意,源码中没有对该值做任何处理。
count值,在此无效,还是需要在命令行输入需要执行的次数。因为命令行的count值是必填项
另外,如何通过LaunchActivity启动带参数的activity,还需要继续研究。有收获的朋友一定要通知哦^_^