欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

[Android]Monkeyrunner得id查找方法

程序员文章站 2022-05-11 12:17:37
...
该方法是莹莹同学发现得,先感谢。

实现方式如下:
from com.android.monkeyrunner import MonkeyRunner as MR  
from com.android.monkeyrunner import MonkeyDevice as MD  
from com.android.monkeyrunner import MonkeyImage as MI  
from com.android.monkeyrunner.easy import EasyMonkeyDevice,By  

device=MR.waitForConnection(10)  
if device:  
 print("Connect device successful!")  
else:  
 print("Connect device failed!")  
device=EasyMonkeyDevice(device)  
device.installPackage("D:\\MonkeyRunnerDemo\\Apps\\estore.apk")  
device.startActivity(component="com.eshore.ezone/.StartActivity")  
MR.sleep(3)  
device.touch(By.id("id/btn_disagree"),device.DOWN_AND_UP)  


注意点:
1.device需要用easymonkeydevice再次封装
2.touch方法id的如何使用
EasyMonkeyDevice类里面还有很多方法,包括exists(By)、getText(By)、type(By,String)、visible(By)等等。

再来个长点的脚本:
from com.android.monkeyrunner import MonkeyRunner as MR  
from com.android.monkeyrunner import MonkeyDevice as MD  
from com.android.monkeyrunner import MonkeyImage as MI  
from com.android.monkeyrunner.easy import EasyMonkeyDevice,By  
from com.android.chimpchat.hierarchyviewer import HierarchyViewer as HV  
from com.android.hierarchyviewerlib.device import ViewNode as VN  
  
T528d=MR.waitForConnection(10)  
if T528d:  
    print("Connect device successful!")  
else:  
    print("Connect device failed!")  
eT528d=EasyMonkeyDevice(T528d)  
hvT528d=T528d.getHierarchyViewer()  
T528d.installPackage("D:\\MonkeyRunnerDemo\\Apps\\estore.apk")  
T528d.startActivity(component="com.eshore.ezone/.StartActivity")  
MR.sleep(3)  
vnT528d=hvT528d.findViewById("id/btn_agree")  
print("Width: "+str(vnT528d.width))  
print("Height: "+str(vnT528d.height))  
vnT528dCoordinate1=hvT528d.getAbsoluteCenterOfView(vnT528d)  
print("vnT528dCoordinate1X: "+str(vnT528dCoordinate1.x))  
print("vnT528dCoordinate1Y: "+str(vnT528dCoordinate1.y))  
vnT528dCoordinate2=hvT528d.getAbsolutePositionOfView(vnT528d)  
print("vnT528dCoordinate2X: "+str(vnT528dCoordinate2.x))  
print("vnT528dCoordinate2Y: "+str(vnT528dCoordinate2.y)) 


        附上运行结果:
Width: 165
Height: 49
vnT528dCoordinate1X: 127
vnT528dCoordinate1Y: 680
vnT528dCoordinate2X: 45
vnT528dCoordinate2Y: 656

        ViewNode类里面还有很多属性,包括parent(获取父控件)、children(获取子控件)、left、top等等,这里就不一个个挨着讲了,自己看吧,呵呵。

另外一篇比较全,比较好的monkeyrunner文章