使用 Nightwatch-Cucumber 执行测试后生成 Cucumber 报告

Generate Cucumber report after test execution with Nightwatch-Cucumber

直到 Nightwatch-Cucumber@7.3.1 框架使用 Cucumber 2 并且我在测试执行后直接在一个 Event Handler 的钩子中实现了我的 Cucumber 报告生成,例如:

const reporter = require("cucumber-html-reporter");

defineSupportCode(function({ registerHandler }) {
  registerHandler("AfterFeature", function(features, callback) {
    try {
      var options = {
        theme: "bootstrap",
        jsonFile: "./reports/json_result/cucumber.json",
        output: "./reports/json_result/cucumber_report.html",
        reportSuiteAsScenarios: true,
        launchReport: false,
        metadata: {
          "App Version": "0.0.3"
       }
     };
     reporter.generate(options);
    } catch (e) {
      console.log(
        "Report generation is not possible with the following message:"
      );
      console.log(e);
    }

    client.end();
    callback();
  });
});

但是由于 Nightwatch-Cucumber@8.0.0 框架使用 Cucumber 3Event Handler 不再可用。现在我想使用Cucumber.jsAfterAll函数,但是在执行AfterAll函数的时候并没有生成cucumberjson报告的内容。所以我得到异常 reports/json_result/cucumber.json: Unexpected end of JSON input,因为 cucumber json 文件此时是空的。如何在测试执行后生成 Cucumber 报告,例如 AfterAll 以进行拆卸。

这是我当前的代码:

const reporter = require("cucumber-html-reporter");

defineSupportCode(function({ AfterAll }) {
  AfterAll(function(callback) {
    try {
      var options = {
        theme: "bootstrap",
        jsonFile: "./reports/json_result/cucumber.json",
        output: "./reports/json_result/cucumber_report.html",
        reportSuiteAsScenarios: true,
        launchReport: false,
        metadata: {
          "App Version": "0.0.3"
       }
     };
     reporter.generate(options);
    } catch (e) {
      console.log(
        "Report generation is not possible with the following message:"
      );
      console.log(e);
    }

    client.end();
    callback();
  });
});

您必须 运行 在单独的 NodeJs 进程中生成报告。示例 package.json 可能如下所示。

{
  ...
  "e2e": "npm-run-all e2e-test e2e-report --continue-on-error",
  "e2e-test": "nightwatch",
  "e2e-report": "node create-html-report.js",
  ...
}

此示例使用 npm-run-all 包,它能够 运行 多个 npm-scripts 顺序并跨平台工作。