MonkeyRunner学习(译)

monkeyRunner学习

monkeyrunner是Android系统提供的用于控制Android模拟器的工具。你可以使用monkeyrunner写一个python脚本来安装一个App或者测试安装包,运行,发送事件,截图并保存等功能。monkeyrunner主要是用来测试App和设备,执行单元测试。

monkeyrunner工具与monkey工具无关。(monkey的使用情况)

monkeyrunner有以下三个优点:

1. 多设备控制: monkeyrunner API可以控制多个跨模拟器的测试用例。你可以勇士启动多个设备,逐一访问这些设备。你也可以启动一个模拟器,执行测试,关闭模拟器。

2. 功能性测试:monkeyrunner可以自动地执行开始-结束操作。输入键盘或点击事件,截图得到结果。

3. 回归测试: monkeyrunner将得到的 结果与预期的结果匹配。

4. 可扩展的自动化: monkeyrunner是一个工具包,结合python的包设计适合自己的测试工具。

monkeyrunner使用的语言是Jython,是python语言的java实现。Jypthon令monkeyrunner与Android系统更好地交互。

monkeyrunner例子

下面的例子展示了如何连接设备,创建MonkeyDevice对象。MonkeyDevice对象安装App,启动活动,发送键盘事件。执行的结果以png的图片格式保存。

# Imports the monkeyrunner modules used by this program
from com.android.monkeyrunner import MonkeyRunner, MonkeyDevice

# Connects to the current device, returning a MonkeyDevice object
device = MonkeyRunner.waitForConnection()

# Installs the Android package. Notice that this method returns a boolean, so you can test
# to see if the installation worked.
device.installPackage('myproject/bin/MyApplication.apk')

# sets a variable with the package's internal name
package = 'com.example.android.myapplication'

# sets a variable with the name of an Activity in the package
activity = 'com.example.android.myapplication.MainActivity'

# sets the name of the component to start
runComponent = package + '/' + activity

# Runs the component
device.startActivity(component=runComponent)

# Presses the Menu button
device.press('KEYCODE_MENU', MonkeyDevice.DOWN_AND_UP)

# Takes a screenshot
result = device.takeSnapshot()

# Writes the screenshot to a file
result.writeToFile('myproject/shot1.png','png')

 monkeyrunner API

 monkeyrunner API由三个模块构成:

MonkeyRunner: 连接设备

MonkeyDevice: 执行操作

MonkeyImage: 保存结果

 运行monkeyrunner 

两种运行方式:1. 使用monkeyrunner.bat运行文件。 2. 在命令行中交互的输入语句。

 monkeyrunner -plugin <plugin_jar> <program_filename> <program_options>

 

posted on 2015-05-04 15:25  青崖绿舟  阅读(184)  评论(0)    收藏  举报

导航