0

0

使用 Selenium 和视觉比较进行视觉回归测试

WBOY

WBOY

发布时间:2024-07-12 08:00:40

|

952人浏览过

|

来源于dev.to

转载

使用 selenium 和视觉比较进行视觉回归测试

视觉测试对于确保 web 应用程序的外观在更新或更改后保持一致和视觉正确至关重要。本博客将指导您使用 selenium 进行浏览器自动化,并使用自定义图像比较实用程序来执行视觉测试。

简介

视觉测试通过比较不同时间点拍摄的屏幕截图来帮助检测 ui 中的意外变化。在本指南中,我们将使用 selenium 自动化 web 交互并截取屏幕截图,然后使用称为视觉比较的图像比较实用程序来比较这些屏幕截图。

先决条件

在开始之前,请确保您已安装以下软件:

  • python 3.x
  • selenium(pip install selenium)
  • 视觉比较(pip install visual-comparison)

设置环境

  1. 安装硒:
    pip 安装selenium

  2. 安装视觉比较包:
    pip install 视觉比较

编写 selenium 脚本

让我们编写一个 selenium 脚本,用于登录示例网站、截取屏幕截图并将其与基线图像进行比较。

科大讯飞-AI虚拟主播
科大讯飞-AI虚拟主播

科大讯飞推出的移动互联网智能交互平台,为开发者免费提供:涵盖语音能力增强型SDK,一站式人机智能语音交互解决方案,专业全面的移动应用分析;

下载

第一步:初始化webdriver并打开网页
首先,初始化webdriver并导航到目标网页:

from selenium import webdriver
from selenium.webdriver.common.by import by

# initialize the webdriver
driver = webdriver.chrome()

# open the target webpage
driver.get("https://www.saucedemo.com/v1/")
driver.maximize_window()
driver.implicitly_wait(5)

第2步:执行登录
接下来,填写用户名和密码字段并单击登录按钮登录网站。目前登录后可视化测试仪表板页面。您可以根据您的要求修改此代码:

# login to the website 
username = driver.find_element(by.id, "user-name")
username.send_keys("standard_user")

password = driver.find_element(by.id, "password")
password.send_keys("secret_sauce")

# click on the login button
login_button = driver.find_element(by.id, "login-button")
login_button.click()`

**step 3: take a screenshot**
after logging in, take a screenshot of the page and save it:
# take a screenshot after login to visualize the changes
actual_image_path = "actual.png"
driver.save_screenshot(actual_image_path)

# close the browser
driver.quit()

第四步:比较图像
使用自定义图像比较实用程序将基线图像 (expected.png) 与新拍摄的屏幕截图 (actual.png) 进行比较:

from visual_comparison.utils import imagecomparisonutil

# load the expected image and the actual screenshot
expected_image_path = "expected.png"
expected_image = imagecomparisonutil.read_image(expected_image_path)
actual_image = imagecomparisonutil.read_image(actual_image_path)

# choose the path to save the comparison result
result_destination = "result.png"

# compare the images and save the result
similarity_index = imagecomparisonutil.compare_images(expected_image, actual_image, result_destination)
print("similarity index:", similarity_index)

# asserting both images
match_result = imagecomparisonutil.check_match(expected_image_path, actual_image_path)
assert match_result

完整脚本

这是结合所有步骤的完整脚本:

"""
this python script compares the baseline image with the actual image.
after any source code modification, the visual changes are compared easily through this script.
"""
from selenium import webdriver
from selenium.webdriver.common.by import by
from visual_comparison.utils import imagecomparisonutil

# initialize the webdriver
driver = webdriver.chrome()

# open the target webpage
driver.get("https://www.saucedemo.com/v1/")
driver.maximize_window()
driver.implicitly_wait(5)

# login to the website 
username = driver.find_element(by.id, "user-name")
username.send_keys("standard_user")

password = driver.find_element(by.id, "password")
password.send_keys("secret_sauce")

# click on the login button
login_button = driver.find_element(by.id, "login-button")
login_button.click()

# take a screenshot after login to visualize the changes
actual_image_path = "actual.png"
expected_image_path = "expected.png"
driver.save_screenshot(actual_image_path)

# close the browser
driver.quit()

# load the expected image and the actual screenshot
expected_image = imagecomparisonutil.read_image(expected_image_path)
actual_image = imagecomparisonutil.read_image(actual_image_path)

# choose the path to save the comparison result
result_destination = "result.png"

# compare the images and save the result
similarity_index = imagecomparisonutil.compare_images(expected_image, actual_image, result_destination)
print("similarity index:", similarity_index)

# asserting both images
match_result = imagecomparisonutil.check_match(expected_image_path, actual_image_path)
assert match_result
Output
Similarity Index: 1.0 (i.e.No Visual Changes)

注意:在执行上述脚本之前创建基线图像/预期图像。参考此仓库github链接

结论

本指南演示了如何使用 selenium 进行网络自动化和视觉比较包来比较屏幕截图来执行视觉测试。通过自动化视觉测试,您可以确保 ui 更改不会引入任何视觉缺陷,从而保持一致的用户体验。

热门AI工具

更多
DeepSeek
DeepSeek

幻方量化公司旗下的开源大模型平台

豆包大模型
豆包大模型

字节跳动自主研发的一系列大型语言模型

通义千问
通义千问

阿里巴巴推出的全能AI助手

腾讯元宝
腾讯元宝

腾讯混元平台推出的AI助手

文心一言
文心一言

文心一言是百度开发的AI聊天机器人,通过对话可以生成各种形式的内容。

讯飞写作
讯飞写作

基于讯飞星火大模型的AI写作工具,可以快速生成新闻稿件、品宣文案、工作总结、心得体会等各种文文稿

即梦AI
即梦AI

一站式AI创作平台,免费AI图片和视频生成。

ChatGPT
ChatGPT

最最强大的AI聊天机器人程序,ChatGPT不单是聊天机器人,还能进行撰写邮件、视频脚本、文案、翻译、代码等任务。

相关专题

更多
pip安装使用方法
pip安装使用方法

安装步骤:1、确保Python已经正确安装在您的计算机上;2、下载“get-pip.py”脚本;3、按下Win + R键,然后输入cmd并按下Enter键来打开命令行窗口;4、在命令行窗口中,使用cd命令切换到“get-pip.py”所在的目录;5、执行安装命令;6、验证安装结果即可。大家可以访问本专题下的文章,了解pip安装使用方法的更多内容。

339

2023.10.09

更新pip版本
更新pip版本

更新pip版本方法有使用pip自身更新、使用操作系统自带的包管理工具、使用python包管理工具、手动安装最新版本。想了解更多相关的内容,请阅读专题下面的文章。

412

2024.12.20

pip设置清华源
pip设置清华源

设置方法:1、打开终端或命令提示符窗口;2、运行“touch ~/.pip/pip.conf”命令创建一个名为pip的配置文件;3、打开pip.conf文件,然后添加“[global];index-url = https://pypi.tuna.tsinghua.edu.cn/simple”内容,这将把pip的镜像源设置为清华大学的镜像源;4、保存并关闭文件即可。

761

2024.12.23

python升级pip
python升级pip

本专题整合了python升级pip相关教程,阅读下面的文章了解更多详细内容。

349

2025.07.23

github中文官网入口 github中文版官网网页进入
github中文官网入口 github中文版官网网页进入

github中文官网入口https://docs.github.com/zh/get-started,GitHub 是一种基于云的平台,可在其中存储、共享并与他人一起编写代码。 通过将代码存储在GitHub 上的“存储库”中,你可以: “展示或共享”你的工作。 持续“跟踪和管理”对代码的更改。

748

2026.01.21

PHP 命令行脚本与自动化任务开发
PHP 命令行脚本与自动化任务开发

本专题系统讲解 PHP 在命令行环境(CLI)下的开发与应用,内容涵盖 PHP CLI 基础、参数解析、文件与目录操作、日志输出、异常处理,以及与 Linux 定时任务(Cron)的结合使用。通过实战示例,帮助开发者掌握使用 PHP 构建 自动化脚本、批处理工具与后台任务程序 的能力。

40

2025.12.13

Python 自然语言处理(NLP)基础与实战
Python 自然语言处理(NLP)基础与实战

本专题系统讲解 Python 在自然语言处理(NLP)领域的基础方法与实战应用,涵盖文本预处理(分词、去停用词)、词性标注、命名实体识别、关键词提取、情感分析,以及常用 NLP 库(NLTK、spaCy)的核心用法。通过真实文本案例,帮助学习者掌握 使用 Python 进行文本分析与语言数据处理的完整流程,适用于内容分析、舆情监测与智能文本应用场景。

10

2026.01.27

拼多多赚钱的5种方法 拼多多赚钱的5种方法
拼多多赚钱的5种方法 拼多多赚钱的5种方法

在拼多多上赚钱主要可以通过无货源模式一件代发、精细化运营特色店铺、参与官方高流量活动、利用拼团机制社交裂变,以及成为多多进宝推广员这5种方法实现。核心策略在于通过低成本、高效率的供应链管理与营销,利用平台社交电商红利实现盈利。

109

2026.01.26

edge浏览器怎样设置主页 edge浏览器自定义设置教程
edge浏览器怎样设置主页 edge浏览器自定义设置教程

在Edge浏览器中设置主页,请依次点击右上角“...”图标 > 设置 > 开始、主页和新建标签页。在“Microsoft Edge 启动时”选择“打开以下页面”,点击“添加新页面”并输入网址。若要使用主页按钮,需在“外观”设置中开启“显示主页按钮”并设定网址。

16

2026.01.26

热门下载

更多
网站特效
/
网站源码
/
网站素材
/
前端模板

精品课程

更多
相关推荐
/
热门推荐
/
最新课程
最新Python教程 从入门到精通
最新Python教程 从入门到精通

共4课时 | 22.3万人学习

Django 教程
Django 教程

共28课时 | 3.6万人学习

SciPy 教程
SciPy 教程

共10课时 | 1.3万人学习

关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2026 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号