通过捕获和比较截图,对Web应用进行像素级视觉回归测试,并根据可配置的相似度阈值控制部署。
视觉 QA. 视觉回归测试网络应用的管道是一项面向实际任务的技能,主要用于您的应用程序的Capture基线截图, 用像素级的 diffing 和 ass/ fai 来比较新截图和基线。它将相关步骤、工具调用和结果整理方式集中到统一流程中,帮助使用者更快完成目标并减少重复操作。
实际使用前应先确认任务范围、数据来源、运行环境、必要权限和关键参数,再依据技能说明逐步执行;若输入条件不完整,应先补齐信息或采用保守配置,避免因错误假设导致结果偏离需求。
执行过程中需要关注工具调用是否成功、接口或依赖是否可用、输出格式是否符合预期,并对异常提示、缺失字段和边界情况进行处理;涉及批量任务时,还应保存进度,避免中断后重复操作。
面向 Web 应用的视觉回归测试流水线。捕获应用的基准截图,使用像素级差异比对新截图与基准图,并依据可配置的相似度阈值判定通过/失败。
# 捕获单个 URL python scripts/capture.py http://localhost:3000 --output .visual-qa/baselines # 捕获多个视口尺寸 python scripts/capture.py http://localhost:3000 --viewports desktop mobile tablet --output .visual-qa/baselines # 根据配置文件捕获多个页面 python scripts/capture.py --config .visual-qa.json --output .visual-qa/baselines
# 比对新截图 python scripts/diff.py --baseline .visual-qa/baselines --current .visual-qa/current --threshold 99
# 一键完成截图 + 差异比对 python scripts/gate.py --baseline .visual-qa/baselines --url http://localhost:3000 --threshold 99 # 启动本地开发服务器后执行 python scripts/gate.py --baseline .visual-qa/baselines --server "npm run dev" --port 3000 --threshold 99 # 使用配置文件 python scripts/gate.py --config .visual-qa.json
在项目根目录创建 .visual-qa.json:
{
"urls": ["/", "/about", "/pricing"],
"baseUrl": "http://localhost:3000",
"viewports": ["desktop", "mobile"],
"threshold": 99,
"server": "npm run dev",
"port": 3000,
"baselineDir": ".visual-qa/baselines",
"ignore": [".visual-qa/diffs", ".visual-qa/current"]
}
所有脚本均支持 --help 参数以查看详细用法。
使用 Playwright(无头 Chromium)捕获截图。
特性:
{url-slug}_{viewport}.png用法:
python scripts/capture.py--output [options] python scripts/capture.py --config --output
使用 Pillow 进行像素级差异比对。
特性:
用法:
python scripts/diff.py --baseline--current --output --threshold
一体化门禁脚本:单条命令完成截图 + 差异比对。
特性:
用法:
python scripts/gate.py --baseline--url --threshold python scripts/gate.py --baseline --server --port --threshold python scripts/gate.py --config
# 启动你的应用 npm run dev # 捕获桌面端和移动端基准图 python scripts/capture.py http://localhost:3000 --viewports desktop mobile --output .visual-qa/baselines
# 在 CI 流水线构建完成后执行 python scripts/gate.py --baseline .visual-qa/baselines --server "npm start" --port 3000 --threshold 99 # 退出码 0 = 通过,1 = 失败 if [ $? -eq 0 ]; then echo "Visual QA passed ✓" else echo "Visual QA failed ✗" exit 1 fi
# 1. 开发者修改 UI # 2. 捕获新截图 python scripts/capture.py http://localhost:3000 --output .visual-qa/current # 3. 生成差异图像 python scripts/diff.py --baseline .visual-qa/baselines --current .visual-qa/current --output .visual-qa/diffs # 4. 在 .visual-qa/diffs/ 目录中审阅差异图像 # 5. 若变更是预期行为,则更新基准图: rm -rf .visual-qa/baselines mv .visual-qa/current .visual-qa/baselines
创建 .visual-qa.json:
{
"urls": ["/", "/products", "/about", "/contact"],
"baseUrl": "http://localhost:3000",
"viewports": ["desktop", "mobile"],
"threshold": 99,
"baselineDir": ".visual-qa/baselines"
}
# 捕获全部页面 python scripts/capture.py --config .visual-qa.json --output .visual-qa/baselines # 对全部页面执行门禁检查 python scripts/gate.py --config .visual-qa.json
脚本依赖 Playwright 和 Pillow:
pip install playwright pillow python -m playwright install chromium
脚本会在运行时检查依赖,若缺失则自动打印安装提示。
--threshold 参数控制相似度百分比(取值范围:0–100):
建议通过实验为你的应用确定合适阈值:从严格模式(99%)起步,若出现误报再逐步放宽。
对于含动态内容的页面(例如日期、用户专属数据):
使用 data 属性 在测试期间隐藏动态元素:
[data-test-hide] { visibility: hidden !important; }
仅截取特定视口区域(未来增强功能)
为允许含动态内容的页面调低阈值
“Command not found: python”
python3 替代 python“Playwright not installed”
pip install playwright && python -m playwright install chromium“相似度低于阈值但图像看起来一致”
“服务器未能启动”
--port 参数与服务器实际监听端口一致npm run dev、npm start 等)gate.py 中增加等待时间(默认为 5 秒)“未找到图像文件”
{url-slug}_{viewport}.png.visual-qa/diffs 和 .visual-qa/current 加入 .gitignore如需查看各脚本完整参数说明,请运行:
python scripts/capture.py --help python scripts/diff.py --help python scripts/gate.py --help
相关专题
热门下载
相关下载
精品课程
共6课时 | 54.7万人学习
共89课时 | 134.1万人学习
共49课时 | 82.6万人学习
最新文章