# 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(’/sdcard/qq.apk’)
sets a variable with the package’s internal name
package = ‘com.tencent.mobileqq’
sets a variable with the name of an Activity in the package
activity = ‘com.tencent.mobileqq.activity.SplashActivity’
sets the name of the component to start
runComponent = package + ‘/’ + activity
Runs the component
device.startActivity(component=runComponent)
Presses the Menu button
MonkeyRunner.sleep(2)
device.press(‘KEYCODE_MENU’, MonkeyDevice.DOWN_AND_UP)
Takes a screenshot
MonkeyRunner.sleep(2)
result = device.takeSnapshot()
Writes the screenshot to a file
MonkeyRunner.sleep(2)
result.writeToFile(‘c:\shot1.png’,‘png’)
本文介绍如何使用MonkeyRunner进行Android应用的自动化测试,包括连接设备、安装应用、启动应用、触发菜单按钮及截图等操作。通过示例代码详细展示了自动化测试流程。
9832

被折叠的 条评论
为什么被折叠?



