欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 文旅 > 明星 > 使用教程:基于 uiautomator2 和 pytest 的图片相似度测试脚本

使用教程:基于 uiautomator2 和 pytest 的图片相似度测试脚本

2024/10/28 13:37:15 来源:https://blog.csdn.net/qq_42206726/article/details/143172052  浏览:    关键词:使用教程:基于 uiautomator2 和 pytest 的图片相似度测试脚本

使用教程:基于 uiautomator2pytest 的图片相似度测试脚本

这篇博客将介绍一个 Python 脚本,它可以通过截图功能对设备屏幕进行监控,并将生成的截图与本地存储的基准图片进行相似度对比。我们将利用 PILnumpyskimage 库进行图像处理,并使用 pytest 框架进行自动化测试。

项目背景

在自动化测试过程中,尤其是对于需要图形界面验证的应用,验证屏幕截图与预期图像是否相似是非常有用的。这个脚本通过 uiautomator2 从设备截图,并与本地的基准图片进行比对。我们使用结构相似性(SSIM)算法来计算图像相似度并判断图像是否符合预期。

完整代码:

from PIL import Image
import numpy as np
import pytest
from skimage.metrics import structural_similarity as ssim
import uiautomator2 as u2
import time
import subprocess
import os
from datetime import datetime
# 创建目录
os.makedirs('resources/images', exist_ok=True)# 截图并保存
def save_screenshot(d):current_time = datetime.now().strftime("%Y%m%d_%H%M%S")screenshot_path = f'resources/images/screenshot_{current_time}.png'd.screenshot(screenshot_path)print(f"截图已保存: {screenshot_path}")return screenshot_path  # 返回截图路径# 比较图片相似度的函数
def compare_images_similarity(image1_path, image2_path, threshold=0.5):try:img1 = Image.open(image1_path).convert('L')img2 = Image.open(image2_path).convert('L')img1 = np.array(img1)img2 = np.array(img2)if img1.shape != img2.shape

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com