目前有个想法,就是将UI截图与自动化截图进行对比。不一致的情况下提示错误

截图对比方法有:

import cv2

import numpy as np

# 均值哈希算法

def aHash(img):

# 缩放为8*8

img = cv2.resize(img, (8, 8))

# 转换为灰度图

gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

# s为像素和初值为0,hash_str为hash值初值为''

s = 0

hash_str = ''

# 遍历累加求像素和

for i in range(8):

for j in range(8):

s = s + gray[i, j]

# 求平均灰度

avg = s / 64

# 灰度大于平均值为1相反为0生成图片的hash值

for i in range(8):

for j in range(8):

if gray[i, j] > avg:

hash_str = hash_str + '1'

else:

hash_str = hash_str + '0'

return hash_str

# 差值感知算法

def dHash(img):

# 缩放8*8

img = cv2.resize(img, (9, 8))

# 转换灰度图

gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

hash_str = ''

# 每行前一个像素大于后一个像素为1,相反为0,生成哈希

for i in range(8):

for j in range(8):

if gray[i, j] > gray[i, j + 1]:

hash_str = hash_str + '1'

else:

hash_str = hash_str + '0'

return hash_str

# 感知哈希算法(pHash)

def pHash(img):

# 缩放32*32

img = cv2.resize(img, (32, 32)) # , interpolation=cv2.INTER_CUBIC

# 转换为灰度图

gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

# 将灰度图转为浮点型,再进行dct变换

dct = cv2.dct(np.float32(gray))

# opencv实现的掩码操作

dct_roi = dct[0:8, 0:8]

hash = []

avreage = np.mean(dct_roi)

for i in range(dct_roi.shape[0]):

for j in range(dct_roi.shape[1]):

if dct_roi[i, j] > avreage:

hash.append(1)

else:

hash.append(0)

return hash

# 通过得到RGB每个通道的直方图来计算相似度

def classify_hist_with_split(image1, image2, size=(256, 256)):

# 将图像resize后,分离为RGB三个通道,再计算每个通道的相似值

image1 = cv2.resize(image1, size)

image2 = cv2.resize(image2, size)

sub_image1 = cv2.split(image1)

sub_image2 = cv2.split(image2)

sub_data = 0

for im1, im2 in zip(sub_image1, sub_image2):

sub_data += calculate(im1, im2)

sub_data = sub_data / 3

return sub_data

# 计算单通道的直方图的相似值

def calculate(image1, image2):

hist1 = cv2.calcHist([image1], [0], None, [256], [0.0, 255.0])

hist2 = cv2.calcHist([image2], [0], None, [256], [0.0, 255.0])

# 计算直方图的重合度

degree = 0

for i in range(len(hist1)):

if hist1[i] != hist2[i]:

degree = degree + (1 - abs(hist1[i] - hist2[i]) / max(hist1[i], hist2[i]))

else:

degree = degree + 1

degree = degree / len(hist1)

return degree

# Hash值对比

def cmpHash(hash1, hash2):

n = 0

# hash长度不同则返回-1代表传参出错

if len(hash1)!=len(hash2):

return -1

# 遍历判断

for i in range(len(hash1)):

# 不相等则n计数+1,n最终为相似度

if hash1[i] != hash2[i]:

n = n + 1

return n

img1 = cv2.imread('openpic/x1y2.png') # 11--- 16 ----13 ---- 0.43

img2 = cv2.imread('openpic/x2y4.png')

img1 = cv2.imread('openpic/x3y5.png') # 10----11 ----8------0.25

img2 = cv2.imread('openpic/x9y1.png')

img1 = cv2.imread('openpic/x1y2.png') # 6------5 ----2--------0.84

img2 = cv2.imread('openpic/x2y6.png')

img1 = cv2.imread('openpic/t1.png') # 14------19---10--------0.70

img2 = cv2.imread('openpic/t2.png')

img1 = cv2.imread('openpic/t1.png') # 39------33---18--------0.58

img2 = cv2.imread('openpic/t3.png')

hash1 = aHash(img1)

hash2 = aHash(img2)

n = cmpHash(hash1, hash2)

print('均值哈希算法相似度:', n)

hash1 = dHash(img1)

hash2 = dHash(img2)

n = cmpHash(hash1, hash2)

print('差值哈希算法相似度:', n)

hash1 = pHash(img1)

hash2 = pHash(img2)

n = cmpHash(hash1, hash2)

print('感知哈希算法相似度:', n)

n = classify_hist_with_split(img1, img2)

print('三直方图算法相似度:', n)

由于截图对比要求较高,我选择差值哈希算法。

具体截图代码如下

对比代码

结果:

python手机自动化截图_python UI自动化截图对比相关推荐

  1. python手机app自动_python+appium 自动化1--启动手机京东app

    出处:https://www.cnblogs.com/yoyoketang/p/6128735.html 前言: 环境搭建好了.接下来先体验下如何启动app--1.首先获取包名:2.然后获取launc ...

  2. LuckyFrameWeb测试平台(一款支持接口自动化、WEB UI自动化、APP自动化,并且支持分布式测试的全纬度免费开源测试平台)

    官网:luckyframe.cn 源码地址:https://gitee.com/seagull1985/LuckyFrameWeb 分布式测试:使用Web-Client的方式,Web端负责基本信息管理 ...

  3. python ui自动化测试仪器_python UI自动化测试

    为了减小维护成本: 1.UI自动化测试需要有较为稳定的环境 2.代码设计合理,那么我们就需要面向对象的设计一个框架,将重复的代码模块化 一.首先总结一下 UI自动化大概要哪些模块 1.config(配 ...

  4. python 网页自动化框架_python+selenium自动化框架

    项目以前的UI自动化大概是这样分层的 1.Common功能层:一些经常被调用的基本常用操作,用来完成某项具体的功能,与具体的业务无关.比如常用的封装: --将元素定位方法重新封装 --封装上下左右滑动 ...

  5. python selenium unittest_python+selenium+unittest——ui自动化的轻量级选择

    最近项目部分趋于稳定,部分功能进入了维护阶段,但每次的小改动都需要进行回归测试,回归的用例不多但也算是重复劳动浪费资源.为了节约这部分人力,我考虑引入web的ui自动化.之前在其他项目中用的都是jav ...

  6. python做ui自动化_[python]RobotFramework自定义库实现UI自动化

    1.安装教程 环境搭建不多说,网上资料一大堆,可参考https://www.cnblogs.com/puresoul/p/3854963.html,写的比较详细,值得推荐.目前python3是不支持r ...

  7. jemeter python接口自动化测试平台_python接口自动化测试之request

    之前小编写过Rest-assuert接口测试框架,它是基于java的,那么怎么轻轻松松玩转python接口自动化测试呢?今天小编就写写如何使用python Request进行接口自动测试. 学习任何一 ...

  8. python自动化控制_python用于自动化控制编程

    Python自动化是做什么的 软件测试里面有Python自动化测试这个课程吗? 在人工智能+大数据的飞速发展的大环境下,Python开发语言日渐火爆,Python自动化测试也将逐渐取代传统的软件测试, ...

  9. python(十二)Uiautomator2搭建UI自动化框架实战

    前言 由于公司UI自动化框架底层用的是Uiautomator2,所以我就用Uiautomator2搭了一套UI自动化框架,并运用某软件做了一个实战,思路其实和之前写的Appnium一样的 ps:这里其 ...

最新文章

  1. Android应用开发-快速入门
  2. 巧用HashMap一行代码统计单词出现次数
  3. java支付模块架构,涨薪7K!
  4. linux下c语言编程gedit,Ubuntu Linux下实现Gedit支持NesC语法高亮
  5. Python库:Pyinstaller库、pip工具、pip指定安装源和版本
  6. 关于未在本地计算机上注册“Microsoft.Jet.OleDb.4.0”提供程序的解决办法
  7. 深度学习中常见的Normalization总结—Batch Normalization,Instance Normalization
  8. FreeSwitch中的会议功能
  9. 学习总结-《父与子的编程之旅》chapter 17
  10. android桌面工具,不一样的Android桌面小工具
  11. python小波变换时频分析
  12. 12306为什么买票这么难
  13. 一维码二维码的生成及打印
  14. i78700k配什么显卡好_最新适合搭配i7-8700的显卡推荐 i7 8700配什么显卡好
  15. space-around space-evenly区别
  16. js遍历对象(js遍历对象的所有属性)
  17. windows7所有版本
  18. 家用小型监控器安装位置与功能
  19. Mybatis框架Result Maps解析
  20. 蓄电池充放电一体机(铅酸电池组和铁锂电池组)放电、充电、循环充电设备应用

热门文章

  1. 在 Asp.Net Core 中使用 worker services
  2. 简单聊聊AspNetCore的启动流程
  3. WSL2 支持挂载物理磁盘,Windows 可直接访问 ext4
  4. dotNET Core 3.X 请求处理管道和中间件的理解
  5. asp.net core 使用newtonsoft完美序列化WebApi返回的ValueTuple
  6. 知道的越多,越感觉自己渺小
  7. WTM重磅更新,LayuiAdmin免费用 and more
  8. HttpClientFactory 使用说明 及 对 HttpClient 的回顾和对比
  9. 程序员修仙之路--把用户访问记录优化到极致
  10. ASP.NET Core Identity 实战(3)认证过程