from com.android.monkeyrunner import MonkeyRunner,MonkeyDevicefrom com.android.monkeyrunner.easy import EasyMonkeyDevicefrom com.android.monkeyrunner.easy import Byfrom com.android.chimpchat.hierarchyviewer import HierarchyViewer#1.连接机器device = MonkeyRunner.waitForConnection()#2.启动activitydevice.startActivity(component=runComponent)#3.获取当前HierarchyViewhierachy_view = device.getHierarchyViewer()#4.通过id获取view对象view_node = hierachy_view.findViewById('id/viewpager')#5.查看子对象的idprint view_node.children[0].children[0].namedProperties.get('mID')#6.easydevice对象easy_device = EasyMonkeyDevice(device)#7.获取view坐标pos=HierarchyViewer.getAbsoluteCenterOfView(view_node)print pos.xprint pos.y#8.EasyMonkeyDevice的touch方式easy_device.touch(By.id('id/mul'),MonkeyDevice.DOWN_AND_UP)#9.monkeyrunner原始的touch方式device.touch(pos.x,pos.y,MonkeyDevice.DOWN_AND_UP)
实际情况中有很对view是没有id或者id重复的,直接通过id touch会没有响应,使用原始的touch方式(代码#9),配合获取view位置解决此问题。