from com.android.monkeyrunner import MonkeyRunner,MonkeyDevice from com.android.monkeyrunner.easy import EasyMonkeyDevice from com.android.monkeyrunner.easy import By from com.android.chimpchat.hierarchyviewer import HierarchyViewer #1.连接机器 device = MonkeyRunner.waitForConnection() #2.启动activity device.startActivity(component=runComponent) #3.获取当前HierarchyView hierachy_view = device.getHierarchyViewer() #4.通过id获取view对象 view_node = hierachy_view.findViewById(‘id/viewpager‘) #5.查看子对象的id print view_node.children[0].children[0].namedProperties.get(‘mID‘) #6.easydevice对象 easy_device = EasyMonkeyDevice(device) #7.获取view坐标 pos=HierarchyViewer.getAbsoluteCenterOfView(view_node) print pos.x print 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位置解决此问题。
时间: 2024-10-27 21:52:04