GRme
GRme

Reputation: 2757

Generate Cucumber report after test execution with Nightwatch-Cucumber

Until [email protected] the framework used Cucumber 2 and I realized my Cucumber report generation in a hook with an Event Handler directly after test execution, like:

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();
  });
});

But since [email protected] the framework uses Cucumber 3 and the Event Handler isn't available anymore. Now I want to use AfterAll function of Cucumber.js, but the content of cucumber json report isn't generated at the time of executing the AfterAll function. So I get exception reports/json_result/cucumber.json: Unexpected end of JSON input, because the cucumber json file is empty at this time. How can I generate a Cucumber report after test execution in something like a AfterAll for a tear down.

Here is my current code:

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();
  });
});

Upvotes: 0

Views: 3239

Answers (1)

mucsi96
mucsi96

Reputation: 324

You have to run the report generation in a separate NodeJs process. An example package.json could be the following.

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

This example is using the npm-run-all package which is capable to run multiple npm-scripts sequential and works cross platform.

Upvotes: 1

Related Questions