Monkeyrunner的id查找方法

实现方式如下:

Java代码 复制代码  收藏代码
  1. from com.android.monkeyrunner import MonkeyRunner as MR     
  2. from com.android.monkeyrunner import MonkeyDevice as MD     
  3. from com.android.monkeyrunner import MonkeyImage as MI     
  4. from com.android.monkeyrunner.easy import EasyMonkeyDevice,By     
  5.   
  6. device=MR.waitForConnection(10)     
  7. if device:     
  8.  print("Connect device successful!")     
  9. else:     
  10.  print("Connect device failed!")     
  11. device=EasyMonkeyDevice(device)     
  12. device.installPackage("D:\\MonkeyRunnerDemo\\Apps\\estore.apk")     
  13. device.startActivity(component="com.eshore.ezone/.StartActivity")     
  14. MR.sleep(3)     
  15. device.touch(By.id("id/btn_disagree"),device.DOWN_AND_UP)    
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)等等。

再来个长点的脚本:

Java代码 复制代码  收藏代码
  1. from com.android.monkeyrunner import MonkeyRunner as MR     
  2. from com.android.monkeyrunner import MonkeyDevice as MD     
  3. from com.android.monkeyrunner import MonkeyImage as MI     
  4. from com.android.monkeyrunner.easy import EasyMonkeyDevice,By     
  5. from com.android.chimpchat.hierarchyviewer import HierarchyViewer as HV     
  6. from com.android.hierarchyviewerlib.device import ViewNode as VN     
  7.      
  8. T528d=MR.waitForConnection(10)     
  9. if T528d:     
  10.     print("Connect device successful!")     
  11. else:     
  12.     print("Connect device failed!")     
  13. eT528d=EasyMonkeyDevice(T528d)     
  14. hvT528d=T528d.getHierarchyViewer()     
  15. T528d.installPackage("D:\\MonkeyRunnerDemo\\Apps\\estore.apk")     
  16. T528d.startActivity(component="com.eshore.ezone/.StartActivity")     
  17. MR.sleep(3)     
  18. vnT528d=hvT528d.findViewById("id/btn_agree")     
  19. print("Width: "+str(vnT528d.width))     
  20. print("Height: "+str(vnT528d.height))     
  21. vnT528dCoordinate1=hvT528d.getAbsoluteCenterOfView(vnT528d)     
  22. print("vnT528dCoordinate1X: "+str(vnT528dCoordinate1.x))     
  23. print("vnT528dCoordinate1Y: "+str(vnT528dCoordinate1.y))     
  24. vnT528dCoordinate2=hvT528d.getAbsolutePositionOfView(vnT528d)     
  25. print("vnT528dCoordinate2X: "+str(vnT528dCoordinate2.x))     
  26. print("vnT528dCoordinate2Y: "+str(vnT528dCoordinate2.y))   
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等等,这里就不一个个挨着讲了,自己看吧,呵呵。

转自:http://myeyeofjava.iteye.com/blog/1991224

你可能感兴趣的:(MonkeyRunner)