入门monkeyrunner7-monkeyrunner demo3 EasyMonkeyDevice+hierarchyviewer +monkeyrunner+截图对比

 1 #该demo为monkeyrunner测试安卓系统自带的计计算器
 2 #测试图片对比,测试EasyMonkeyDevice对象,一些他的元素操作
 3 #作者:Mads Spiral QQ:79523822 如有疑问留言或者加qq,验证wyx
 4 #coding=utf-8
 5 import sys
 6 import time
 7 from com.android.monkeyrunner import MonkeyRunner as mr
 8 from com.android.monkeyrunner import MonkeyDevice as md
 9 from com.android.monkeyrunner import MonkeyImage as mi
10 from com.android.chimpchat.hierarchyviewer import HierarchyViewer,ViewNode
11 from com.android.monkeyrunner.easy import EasyMonkeyDevice,By  
12 
13 #图片对比
14 def img():
15     result1 = device.takeSnapshot()
16     result1.writeToFile(path+"第一张截图".decode('utf-8')+now+'.png','png')
17     #点击了按钮7,105.514
18     device.touch(105,514,'DOWN_AND_UP')
19     #输入8
20     device.type("8")
21     #拖动,坐标开始到坐标结束,0.1为所用时间,1,从9(461.490)到7(63,493)
22     #device.drag((461.490),(63,493),0.1,1)
23     result2 = device.takeSnapshot()
24     result2.writeToFile(path+"第二张截图".decode('utf-8')+now+'.png','png')
25     if(result1.sameAs(result2,0.9)):
26         #这里需要help,为什么控制台显示为乱码,知道的朋友请留言
27         #str1="搜索框图片对比成功"
28         #str1.decode('unicode').encode('gb18030')  
29         #print str1
30         print "img success"
31         log.write("搜索框图片对比成功\n”)
32     else:         
33         #str2="搜索框图片对比失败"
34         #str2.decode('unicode').encode('gb18030')  
35         #print str2
36         print "img falied"
37         log.write("搜索框图片对比失败”)
38     #如果要从文件夹读取指定的图片
39     #result=MonkeyRunner.loadImageFromFile('D:/picture2/demo.png')
40 
41 '''测试EasyMonkeyDevice的一些方法,一些方法请看
42 http://blog.csdn.net/zhubaitian/article/details/39992597'''
43 def easyMonkeyDevice():
44     easydevice=EasyMonkeyDevice(device)
45 
46 def easyMonkeyDevicetest():
47     mr.sleep(5.0)
48     #触摸,下面两句话可以合成一句话easydevice.touch(By.id("id/mul"),MonkeyDevice.DOWN_AND_UP)
49     DisagreeButton=By.id("id/digit9")
50     easydevice.touch(DisagreeButton,MonkeyDevice.DOWN_AND_UP)
51     #输入 easydevice.type(By.id(""),'new')
52     #是否存在 easydevice.exist(By.id("")),返回一个bool值
53     #是否可见 easydevice.visible(By.id(""))
54     #获取该id控件的文本 easydevice.getText(By.id(""))
55     #获取该id控件的坐标 easydevice.locate(By.id(""))
56 
57 #HierarchyViewer和ViewNode,获取控件的width、height、坐标、文本
58 def hieview():
59     easyMonkeyDevice()
60     hv=easydevice.getHierarchyViewer() 
61     hvid=hv.findViewById("com.android.calculator2:id/digit9")  
62     print("Width: "+str(hvid.width))  
63     print("Height: "+str(hvid.height))
64     hvview=hv.getAbsoluteCenterOfView(hvid)  
65     print("vnT528dCoordinate1X: "+str(hvview.x))  
66     print("vnT528dCoordinate1Y: "+str(hvview.y))  
67     hvview2=hvT528d.getAbsolutePositionOfView(hvid)  
68     print("vnT528dCoordinate2X: "+str(hvview2.x))  
69     print("vnT528dCoordinate2Y: "+str(hvview2.y)) 
70 
71 
72 logpath='D:\\'
73 path='D:\\'
74 device=mr.waitForConnection()
75 if device:
76     print "connect device successed"
77     #若安装淘宝apk。参数是apk文件的位置,因为python不支持中文输入,所以在后面用了.decode('utf-8')这个方法转码。
78     #device.installPackage ('D:\\apk\\淘宝.apk'.decode('utf-8'))
79     now=time.strftime("%Y-%m-%d-%H-%M-%S")
80     log=open(logpath+"log"+now+".txt",'w')
81     activity='com.android.calculator2/com.android.calculator2.Calculator'
82     device.startActivity(activity)
83     mr.sleep(5.0)
84     #img()
85     easyMonkeyDevice()
86     easyMonkeyDevicetest()
87     hieview()
88     
89 else:
90     print "connect device failed"

 

posted @ 2014-12-28 14:32  Mr.Dantes  阅读(582)  评论(2编辑  收藏  举报