javascript - 如何在:run after tests finish execution之后触发cypress

标签 javascript node.js cypress report metadata

我使用 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, the after:run event only fires if the experimentalInteractiveRunEvents 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/

相关文章:

javascript - 如何使用文本框中的日期值从日历中选择日期

javascript - 登录后如何将用户显示在 URL 中的名称主页?

javascript - Node.js 需要函数父对象吗?

testing - 获取柏树类列表的长度?

node.js - Cypress 在浏览器外部执行 API 调用

vue.js - 将 Cypress 与 vuetify 一起使用

javascript - 使用 Javascript 验证是 Armstrong 编号

javascript - Window.close() 不适用于关闭当前选项卡

javascript - 如何使用 PhantomJS 抓取嵌入的 JSON

node.js - Azure Web应用程序 Node 部署,更改应用程序根目录?