MonkeyRunner_MonkeryRunner图片对比代码

本文介绍了一种基于图片比对的自动化UI测试方法,该方法利用MonkeyRunner工具进行截图并与基准图片进行像素级比较,以判断界面元素是否符合预期。适用于Android应用的自动化测试。

转载自: http://www.51testing.com/?uid-225927-action-viewspace-itemid-249291


************** picCompare.py
from __future__ import division
from com.android.monkeyrunner import MonkeyRunner, MonkeyDevice, MonkeyImage
log=open("F:\\demo\\demo.txt","w")
#this function used for pic compare
#the variables is base-pic,compare-pic,pic-width,pic-high,pic similarity
def picCompare(imageA,imageB,width,high,factor):
    i=0
    Rc=Gc=Bc=0
    Total=255*width*high/4
    while i<width:
        j=0
        while j<high:
            cc=imageA.getRawPixel(i,j)
            dd=imageB.getRawPixel(i,j)
            Rc=Rc+abs(cc[1]-dd[1])
            Gc=Gc+abs(cc[2]-dd[2])
            Bc=Bc+abs(cc[3]-dd[3])
            j=j+2
        i=i+2
    Rsub=Rc/Total
    Gsub=Gc/Total
    Bsub=Bc/Total
    picSame=1-(Rsub+Gsub+Bsub)/3
    if picSame>factor:
        return 1
    else:
        return 0
**********测试脚本
import sys
sys.path.append("E:\\android-sdk-windows\\tools")
import picCompare as COM
from com.android.monkeyrunner import MonkeyRunner, MonkeyDevice, MonkeyImage
log=open("F:\\demo\\demo.txt","w")
imageA=MonkeyRunner.loadImageFromFile("F:\\demo\\sougou.png")
#judge connection
device = MonkeyRunner.waitForConnection()
#
imageB=device.takeSnapshot()
imageB=imageB.getSubImage((20,320,60,60))
if COM.picCompare(imageA,imageB,60,60,0.8):
    log.write("OK,the will do action Click\n")
    device.touch(20,320,"DOWN_AND_UP")
else:
    imageB.writeToFile("F:\\demo\\errorSogou.png","png")
    log.write("couldn't find sogou item,please check")
log.close()
 
 
******************************
使用说明,andorid手机自动化测试过程中,如果UE能提前确定,则使用MonkeyRunner Recorder捕获全部的测试控件,
然后通过图像对比,执行对应的Activity。
这个是基于SeeTest基础上个人改编的,欢迎讨论~~

评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值