如何修改JS代码在Allure报告中添加已有截图(无需实时截取)
解决Allure报告使用GitHub仓库现有截图的问题
你需要利用Allure的createAttachment方法直接传入图片的二进制数据,而非DOM元素。以下两种方案可实现需求:
方案一:使用本地已下载的截图文件
先把GitHub仓库里的截图下载到本地项目目录(比如screenshots文件夹),再通过Node.js文件系统模块读取文件内容传给Allure:
const fs = require('fs').promises; const path = require('path'); // 替换为你本地截图的实际路径 const localScreenshotPath = path.join(__dirname, 'screenshots', 'step-screenshot.png'); // 读取截图文件为Buffer格式 const screenshotBuffer = await fs.readFile(localScreenshotPath); // 将截图添加到Allure报告 allure.createAttachment('步骤截图', screenshotBuffer, 'image/png');
方案二:直接从GitHub获取截图内容
使用GitHub原始文件URL(注意必须是raw.githubusercontent.com开头的地址,不能用仓库网页预览链接),通过HTTP请求获取图片二进制数据:
const fetch = require('node-fetch'); // 替换为你的GitHub截图原始URL const githubRawImageUrl = 'https://raw.githubusercontent.com/你的用户名/你的仓库名/分支名/截图文件路径.png'; // 请求获取图片数据 const response = await fetch(githubRawImageUrl); const screenshotBuffer = await response.buffer(); // 将截图添加到Allure报告 allure.createAttachment('GitHub仓库截图', screenshotBuffer, 'image/png');
为什么之前的img.src方法无效?
Allure Node.js SDK的createAttachment要求传入二进制Buffer或Base64字符串,而你尝试的DOM Image对象是浏览器端用于渲染的元素,和Allure的API要求不匹配,因此无法被识别。
内容的提问来源于stack exchange,提问作者riffa
相关产品推荐
相关产品推荐

