monkeyrunner实例

几点注意:

脚本放到android/sdk/tools/目录下去运行

使用HierarchyViewer获得空间的id

monkeyrunner recorder可以录制简单的操作,然后导出成脚本回放

代码如下:


from com.android.monkeyrunner import MonkeyRunner, MonkeyDevice
from com.android.monkeyrunner import MonkeyImage
from com.android.monkeyrunner.easy import EasyMonkeyDevice
from com.android.monkeyrunner.easy import By
from com.android.chimpchat.hierarchyviewer import HierarchyViewer
# from com.android.hierarchyviewerlib.device import ViewNode

print "Start test"


MonkeyRunner.sleep(3)

device = MonkeyRunner.waitForConnection()
easy_device = EasyMonkeyDevice(device)
device.installPackage("./ContactManager.apk")
print "Install APK done."


device.startActivity(component="com.example.android.contactmanager/.ContactManager")
MonkeyRunner.sleep(2)

addButton = easy_device.visible(By.id('id/addContactButton'))
easy_device.touch(By.id('id/addContactButton'), MonkeyDevice.DOWN_AND_UP)
MonkeyRunner.sleep(2)
easy_device.type(By.id('id/contactNameEditText'), "Hello\n")
easy_device.touch(By.id('id/contactPhoneEditText'), MonkeyDevice.DOWN_AND_UP)

# width = device.getSystemProperty('width')
# print width
# device.press('KEYCODE_HOME', MonkeyDevice.DOWN_AND_UP)
# device.press('KEYCODE_MENU', MonkeyDevice.DOWN_AND_UP)
# device.press('KEYCODE_BACK', MonkeyDevice.DOWN_AND_UP)
# MonkeyRunner.alert("Start Touch", "INFO", "OK")
# print "menu pressed"

result = device.takeSnapshot()
result.writeToFile('./shot1.png', 'png')
print "Test done."