• Android自动化测试


    来源于:http://testerhome.com/topics/878

      1 # encoding=utf-8  
      2 
      3 
      4 #导入python中自带的time模块和sys模块,脚本中都要用到它们。
      5 import time
      6 import sys
      7 #MonkeyRunner自带的三个api
      8 from com.android.monkeyrunner import MonkeyRunner ,MonkeyDevice ,MonkeyImage
      9 
     10 
     11 #这个函数时确认年月日时分秒
     12 now=time.strftime("%Y-%m-%d-%H-%M-%S")
     13 #指定我们要保存图片的位置和打印log的位置
     14 path='D:\picture\'
     15 logpath="D:\log\"
     16 
     17 #python中获取当前运行的文件的名字
     18 name=sys.argv[0].split("\")
     19 filename=name[len(name)-1]
     20 
     21 """
     22 可以尝试输入这两句语句就可以明白上面的两个python方法了。
     23 print(name)
     24 print(filename)
     25 """
     26 
     27 #新建一个log文件
     28 log=open(logpath+filename[0:-3]+"-log"+now+".txt",'w')
     29 #连接设备,两个参数分别是等待的时间(这里的时间都是秒为单位),设备的序列号。
     30 device=MonkeyRunner.waitForConnection(5,'b4726a2d')
     31 
     32 #安装锤子便签apk。参数是apk文件的位置,因为python不支持中文输入,所以在后面用了.decode('utf-8')这个方法转码。
     33 device.installPackage ('D:\apk\锤子便签.apk'.decode('utf-8'))
     34 #打印出操作信息到log文件里
     35 log.write("安装apk……
    ")
     36 #等待2秒
     37 MonkeyRunner.sleep(2)
     38 
     39 #启动app,参数里是app的包名/活动名
     40 device.startActivity(component='com.smartisan.notes/.NotesActivity')
     41 MonkeyRunner.sleep(2)
     42 #打印操作信息
     43 log.write("启动app……
    ")
     44 #截图
     45 result = device.takeSnapshot()
     46 #保存截图 
     47 result.writeToFile(path+"主页面".decode('utf-8')+now+'.png','png')
     48 
     49 #点击搜索款的位置坐标。
     50 device.touch(111,155,'DOWN_AND_UP')
     51 MonkeyRunner.sleep(2)
     52 #输入smartisan字样
     53 device.type("smartisan")
     54 #截图
     55 result1=device.takeSnapshot()
     56 #保存截图
     57 result1.writeToFile(path+"搜索框截图".decode('utf-8')+'.png','png')
     58 
     59 
     60 #移动第一个便签的位置到最后面去,参数是:一个起始点坐标,一个终点坐标,移动的时间,移动的步骤
     61 device.drag((232,235),(216,472),3,2)
     62 MonkeyRunner.sleep(3)
     63 #截图
     64 result2=device.takeSnapshot()
     65 #保存截图
     66 result2.writeToFile(path+"移动便签".decode('utf-8')+now+".png",'png')
     67 
     68 
     69 #第一个便签向右滑动
     70 device.drag((109,360),(322,360))
     71 MonkeyRunner.sleep(3)
     72 
     73 #截图
     74 result3=device.takeSnapshot()
     75 #保存截图
     76 result3.writeToFile(path+"右移动便签".decode('utf-8')+now+".png",'png')
     77 
     78 #点击最后一个便签的位置
     79 device.touch(182,583,'DOWN_AND_UP')
     80 MonkeyRunner.sleep(5)
     81 #点击发送的位置
     82 device.touch(324,73,'DOWN_AND_UP')
     83 MonkeyRunner.sleep(5)
     84 #点击发送至长微博的位置
     85 device.touch(227,789,'DOWN_AND_UP')
     86 MonkeyRunner.sleep(5)
     87 #点击生成长微博的位置
     88 device.touch(228,791,'DOWN_AND_UP')
     89 MonkeyRunner.sleep(5)
     90 
     91 #截图
     92 result4=device.takeSnapshot()
     93 #保存图片
     94 result4.writeToFile(path+"发长微博截图".decode("utf-8")+now+'.png','png')
     95 #点击下一步的位置
     96 device.touch(426,81,'DOWN_AND_UP')
     97 MonkeyRunner.sleep(3)
     98 
     99 #截图
    100 result5=device.takeSnapshot()
    101 #保存截图
    102 result5.writeToFile(path+"输入微博账号".decode("utf-8")+now+'.png','png')
    103 
    104 #点击输入微博账号和密码的几个位置,分别输入账号和密码
    105 device.touch(196,311,'DOWN_AND_UP')
    106 MonkeyRunner.sleep(3)
    107 device.type("powermo@126.com")
    108 MonkeyRunner.sleep(3)
    109 device.touch(168,378,'DOWN_AND_UP')
    110 MonkeyRunner.sleep(3)
    111 device.type("powermo1234")
    112 MonkeyRunner.sleep(3)
    113 #点击登录
    114 device.touch(237,449,'DOWN_AND_UP')
    115 MonkeyRunner.sleep(3)
    116 
    117 #截图
    118 result6=device.takeSnapshot()
    119 #保存截图
    120 result6.writeToFile(path+"登陆微博".decode("utf-8")+now+'.png','png')
    121 
    122 
    123 #下面就开始对之前的截图进行对比了
    124 #第一张截图做对比,去文件中找到我们要对比的图片
    125 resultTrue=MonkeyRunner.loadImageFromFile('D:\picture2\shottrue.png')
    126 log.write("主页面对比图片……
    ")
    127 #判断图片相识度是否是为90%
    128 if(result.sameAs(resultTrue,0.9)):
    129     #在命令行打印出信息
    130     print("主页面图片对比成功")
    131     #打印信息到log文件
    132     log.write("主页面图片对比成功……
    ")
    133 else:
    134     #打印信息到命令行
    135     print("主页面图片对比失败")
    136     log.write("主页面图片对比失败……
    ")
    137 
    138 
    139 #去文件中找到我们规定的图片用来对比
    140 result1True=MonkeyRunner.loadImageFromFile('D:\picture2\shottrue1.png')
    141 #判断图片相识度是否是为90%
    142 if(result1.sameAs(result1True,0.9)):
    143     print("搜索框图片对比成功")
    144     log.write("搜索框图片对比成功……
    ")
    145 else:
    146     print("搜索框图片对比失败")
    147     log.write("搜索框图片对比失败……
    ")
    148 
    149 
    150 
    151 #对移动便签图片对比
    152 result2True=MonkeyRunner.loadImageFromFile('D:\picture2\shottrue2.png')
    153 ##判断图片相识度是否是为80%
    154 if(result2.sameAs(result2True,0.8)):
    155     print("移动便签对比成功")
    156     log.write("移动便签对比成功……
    ")
    157 else:
    158     print("移动便签图片对比失败")
    159     log.write("移动便签对比失败……
    ")
    160 
    161 
    162 
    163 
    164 #对移动便签图片进行对比,去文件中找我们规定的图片
    165 result3True=MonkeyRunner.loadImageFromFile('D:\picture2\shottrue3.png')
    166 ##判断图片相识度是否是为80%
    167 if(result3.sameAs(result3True,0.8)):
    168     print("右移便签图片对比成功")
    169     log.write("右移便签图片对比成功……
    ")
    170 else:
    171     print("右移便签图片对比失败")
    172     log.write("右移便签图片对比失败……
    ")
    173 
    174 #对长微博图片对比
    175 result4True=MonkeyRunner.loadImageFromFile('D:\picture2\shottrue4.png')
    176 if(result4.sameAs(result4True,0.8)):
    177     print("发长微博图片对比成功")
    178     log.write("发长微博图片对比成功……
    ")
    179 else:
    180     print("发长微博图片对比失败")
    181     log.write("发长微博图片对比失败……
    ")
    182 
    183 
    184 result5True=MonkeyRunner.loadImageFromFile('D:\picture2\shottrue5.png')
    185 if(result5.sameAs(result5True,0.8)):
    186     print("输入微博账号图片对比成功")
    187     log.write("输入微博账号图片对比成功……
    ")
    188 else:
    189     print("输入微博账号图片对比失败")
    190     log.write("输入微博账号图片对比失败……
    ")
    191 
    192 
    193 result6True=MonkeyRunner.loadImageFromFile('D:\picture2\shottrue6.png')
    194 if(result6.sameAs(result6True,0.8)):
    195     print("登陆微博图片对比成功")
    196     log.write("登陆微博图片对比成功……
    ")
    197 else:
    198     print("登陆微博图片对比失败")
    199     log.write("登陆微博图片对比失败……
    ")
  • 相关阅读:
    leetcode-9-basic-binary search
    selection problem-divide and conquer
    leetcode-8-pointer
    leetcode-7-hashTable
    前端学习之——js解析json数组
    Google浏览器如何加载本地文件
    JAVA全栈工程师应具备怎样的知识体系?
    Java全栈工程师知识体系介绍
    数据可视化工具
    使用js的FileReader对象
  • 原文地址:https://www.cnblogs.com/dtest/p/4155478.html
Copyright © 2020-2023  润新知