在app的自动化测试过程中,如想要断言图片展示是否正确

方案:
截取图片图片跟期望的图片做对比,检查图片是否一致
def cut_image(self,locator,name,wait_ele='visibility',by=MobileBy.ID,wait=10, requence=0.5):
from common.dir_config import common
ele = self.find_element_wait_and_focus(locator, wait_ele, by, wait,
requence)
try:
path = common + '/image/' + str(name) + '.png'
self.logger.info("对元素{0}进行截图操作。".format(locator))
ele.screenshot(path) #截取图片
return path
except Exception as e:
self.logger.error("元素截图失败{0}".format(e))
raise
def image_contrast(img_base,img_true):
"""
对比图片相似度
:param img_base: 期望图片
:param img_true: 实际图片
:return: 相似度
"""
img_base = Image.open(img_base)
img_true = Image.open(img_true)
h1 = img_base.histogram()
h2 = img_true.histogram()
result = math.sqrt(reduce(operator.add,list(map(lambda a,b:(a-b)**2,h1,h2)))/len(h1))
return result
test.py
iconIv = self.cut_image('xdyz.nesdsing.intbee:id/iconIv','image1')
re = image_contrast(iconIv,common + '/image/renwu.png')
assert re == 0 #图片相似度越高,越接近于0
缺陷:
1、如期望图片保存的是原图,而截图出来的图片跟原图分辨率不一致,对比后,相似度会非常低,无法输出正确的断言结果
解决:
先在调试测试时,截图,把截取下来的图作为期望图保存下来,再做对比,这样可以做到图片分辨率一致。
网友评论