[Android]Monkeyrunner得id查詢方法

大搜車-自娛發表於2013-12-18
該方法是瑩瑩同學發現得,先感謝。

實現方式如下:
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等等,這裡就不一個個挨著講了,自己看吧,呵呵。

[url=http://blog.csdn.net/tzh2009/article/details/8152459]另外一篇比較全,比較好的monkeyrunner文章[/url]

相關文章