我使用 cypress
+ multiple-cucumber-html-report
在执行后生成报告。可以将自定义数据添加到报告中,例如执行开始和结束时间。
我假设此信息以某种方式来自 cypress,作为结果元数据的一部分。
我尝试在运行完成后将结果放入 json 文件中,方法是将其添加到 cypress 配置文件中:
import { defineConfig } from 'cypress';
import * as fs from 'fs';
async function setupNodeEvents(on, config) {
on('after:run', async (results) => {
if (results) {
fs.mkdirSync("cypress/.run", { recursive: true });
fs.writeFile("cypress/.run/results.json", JSON.stringify(results), (err) => {
if (err)
console.log(err);
else {
console.log("Successful results has been written");
}
});
}
})
return config;
}
export default defineConfig({
e2e: {
setupNodeEvents,
experimentalInteractiveRunEvents: true
},
});
然后在报告生成文件中读取这些结果:
const report = require('multiple-cucumber-html-reporter');
const fs = require('fs');
fs.readFile('cypress/.run/results.json', function read(err, data) {
if (err) {
throw err;
}
var runInfos = JSON.parse(data);
report.generate({
jsonDir: './cypress/result/',
reportPath: './cypress/report/',
metadata:{
browser: {
name: runInfos.browserName,
version: runInfos.browserVersion
},
device: 'Cypress',
platform: {
name: mapOs(runInfos.osName)
}
},
customData: {
title: 'Run info',
data: [
{label: 'Project', value: 'project'},
{label: 'Execution Start Time', value: new Date(runInfos.startedTestsAt).toLocaleString()},
{label: 'Execution End Time', value: new Date(runInfos.endedTestsAt).toLocaleString()}
]
}
});
});
不幸的是,after:run
从未被触发,甚至没有抛出错误。
最佳答案
查看 After Run API 上的文档
When running via
cypress open
, theafter:run
event only fires if theexperimentalInteractiveRunEvents
flag is enabled.
不幸的是,从您的配置示例来看,您必须使用旧版本的 Cypress(v10 之前)。
为了使其更有用,请使用上面提到的标志升级 Cypress,如下所示:
// cypress.config.js
const { defineConfig } = require('cypress')
module.exports = defineConfig({
// setupNodeEvents can be defined in either
// the e2e or component configuration
e2e: {
setupNodeEvents(on, config) {
on('after:run', (results) => {
/* ... */
})
},
experimentalInteractiveRunEvents: true, // here is the flag, not documented in the configuration page
},
})
关于javascript - 如何在:run after tests finish execution之后触发cypress,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/75341578/