简介
Monkeyrunner 是android 2。3的SDK中新添加一个自动化测试工具,位于SDK tools目录下。
通过它可以在PC端运行python脚本来操作模拟器或设备。
通过其提供的API可以实现如下功能:
1, 点击
2, 按键
3, 滑动
4, 发送shell命令
5, 安装与卸载APK
6, 启动activity
7, 发送广播
8, 截图
9, 比图
10,弹出警告框
11,弹出下拉选择框
12, 弹出输入框
13, 获取设备属性值
14, 敲入字符串
1,获取monkeyrunner源码
monkeyrunner位于android2.3以后的源码下的的sdk/monkeyrunner中。
在eclipse中可以使用New Project -> Java Project -> Create projet from existing source 将源码导入eclipse中
查看src目录下的Android.mk文件,编译该程序需要加载名为ddmlib 、jython、guavalib 、jsilver 、sdklib的五个jar包。这些包在SDK的tools/lib目录下都可以找到。 将这些包导入之后项目无错误可以正常编译、调试并运行。
2,几个重要的类
MonkeyRunner
MonkeyRunner提供了几个控制脚本运行进程的方法,包括:
Waits for the workstation to connect to the device.
Args
- timeout - The timeout in seconds to wait. The default is to wait indefinitely.
- deviceId - A regular expression that specifies the device name. See the documentation for 'adb' in the Developer Guide to learn more about device names.
Returns
A MonkeyDevice object representing the connected device.
Pause the currently running program for the specified number of seconds.
Args
- seconds - The number of seconds to pause.
Returns
returns nothing.
Display a dialog that accepts input. The dialog is ,modal, so the script stops until the user clicks one of the two dialog buttons. To enter a value, the user enters the value and clicks the 'OK' button. To quit the dialog without entering a value, the user clicks the 'Cancel' button. Use the supplied arguments for this method to customize the text for these buttons.
Args
- message - The prompt message to display in the dialog.
- initialValue - The initial value to supply to the user. The default is an empty string)
- title - The dialog's title. The default is 'Input'
- okTitle - The text to use in the dialog's confirmation button. The default is 'OK'.The text to use in the dialog's 'cancel' button. The default is 'Cancel'.
- cancelTitle -
Returns
Display a choice dialog that allows the user to select a single item from a list of items.
Args
- message - The prompt message to display in the dialog.
- choices - An iterable Python type containing a list of choices to display
- title - The dialog's title. The default is 'Input'
Returns
Display an alert dialog to the process running the current script. The dialog is modal, so the script stops until the user dismisses the dialog.
Args
- message - The message to display in the dialog.
- title - The dialog's title. The default value is 'Alert'.
- okTitle - The text to use in the dialog button. The default value is 'OK'.
Returns
returns nothing.
MonkeyDevice
MonkeyDevice提供了与android设备交互的所有接口,包括:
String getSystemProperty(String key)
void touch(x,y, "type");
void drag(start, end , duration, steps);
void press("name","type")
void type();
String shell(cmd);
void reboot() bootloader, recovery, or None
boolean installPackage(path)
boolean removePackage(package);
void startActivity()
void startActivity(uri, action, data, mimetype, categories, extras, component flags)
PyDictionary instrument(className, args);
void wake();
MonkeyImage takeSnapshot()
其中与发送触屏与按键事件有关的方法的实现都是通过处于12345端口的socket 向位于android设备上的Monkey Server端发送命令的方式来完成的。命令形式是字符串,格式为: action x y
Monkey的server端源码位于android源码的development/cmds/monkey目录下。MonkeyRunner应该是与Monkey共用一个server。
其他都是通过ddmslib库中提供的API间接使用adb工具实现的。
MonkeyImage
MonkeyImage包含从