我目前正在尝试通过 mochawesome 创建并通过电子邮件发送带有屏幕截图的 html 报告。为了通过电子邮件发送,我一直在尝试将 png 图像转换为 base64 以将其内联到 html 文件中。但是,我无法成功地做到这一点...我已经厌倦了将 png 编码为 base64 的不同方法,例如 FileReader、cy.readFile、fs.readFileSync、image-to-base64 npm 包等,但没有取得太大成功。想知道是否有一种特定的方法可以为赛普拉斯做到这一点,以及是否有人可以提供帮助。
Cypress.on('test:after:run', (test, runnable) => {
if(test.state === 'failed' && test.currentRetry === test.retries){
const title = test.title.replace('.', "").replace('#', '%23');
const parentTitle = runnable.parent.title.replace(".", "");
const filePath = (test.retries == 0) ?
`${Cypress.config('screenshotsFolder')}/${Cypress.spec.name}/${parentTitle} -- ${title} (failed).png`
: `${Cypress.config('screenshotsFolder')}/${Cypress.spec.name}/${parentTitle} -- ${title} (failed) (attempt 2).png`;
const fs = require("fs")
const pngAsBase64 = fs.readFileSync(filePath, "base64")
addContext({ test }, "data:image/png;base64," + pngAsBase64);
};
})
尝试了 FileReader、cy.readFile、fs.readFileSync、fs.readFile、image-to-base64 npm 包等,期望看到 png 变成 base64
fs.readFileSync
中的编码语法似乎不正确。
来自 node-js-fs-readfilesync-method/
语法:
fs.readFileSync( path, options )
- options: 可选参数,包含编码和标志,编码包含数据规范。它的默认值为 null,它返回原始缓冲区,并且该标志包含文件中操作的指示。它的默认值为‘r’。
const data = fs.readFileSync('./input.txt', { encoding: 'utf8', flag: 'r' });
更改此行:
const pngAsBase64 = fs.readFileSync(filePath, {encoding: 'base64'})