Android自动化测试 - MonkeyRunner

2012-09-01  王涞 

Android自动化测试如今有很多,有收费的也有免费的,包括Google也有很多开源的好的工具可以利用。
Monkeyrunner就是其中之一:下面是一些简单的代码

MonkeyRunner所在的位置是在android sdk\tools下面,支持的是Python脚本。
MonkeyRunner支持用户的2个基础类是:MonkeyRunner,MonkeyDevice
就是说用户只要在python脚本里面import这2个类便可以调用Monkeyrunner提供的所有API了。
下面是个简单的例子,来至于Google:

fromcom.android.monkeyrunnerimportMonkeyRunner,MonkeyDevice
device=MonkeyRunner.waitForConnection()
device
.installPackage('myproject/bin/MyApplication.apk')

package
='com.example.android.myapplication'
activity
='com.example.android.myapplication.MainActivity'
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')

大家可以把改文本copy到自己的test.py文件里面运行monkeyrunner test.py看看效果。

432°/4322 人阅读/0 条评论 发表评论

登录 后发表评论