How to create Selenium Cucumber html reports with Node JS

5.8k Views Asked by At

I want to create cucumber html reports and I am new to Node JS and I tried searching for it and I used the following

this.registerHandler('AfterFeatures', function(callback) {
        try {
            var options = {
                theme: "bootstrap",
                jsonFile: "/report/cucumber.json",
                output: "/report/cucumber_report.html",
                reportSuiteAsScenarios: true,
                launchReport: true,
                metadata: {
                    "App Version": "0.0.1"
                }
            };
            reporter.generate(options);
        } catch (e) {
            console.log(e);
        }
        callback();
    });

But when I run my code, The cucumber feature scenarios gets executed and it finally gives me an error stating,

Unable to parse cucumberjs output into json: '/report/cucumber.json' { Error: /report/cucumber.json: ENOENT: no such file or directory, open '/report/cucumber.json'
    at Object.fs.openSync (fs.js:652:18)
    at Object.fs.readFileSync (fs.js:553:33)
    at Object.readFileSync (/Users/sarav/Documents/GitHub/automationtests/node_modules/jsonfile/index.js:67:22)
    at isValidJsonFile (/Users/sarav/Documents/GitHub/automationtests/node_modules/cucumber-html-reporter/lib/reporter.js:404:48)
    at Object.generate (/Users/sarav/Documents/GitHub/automationtests/node_modules/cucumber-html-reporter/lib/reporter.js:426:9)
    at Object.generateReport [as generate] (/Users/sarav/Documents/GitHub/automationtests/node_modules/cucumber-html-reporter/index.js:30:21)
    at /Users/sarav/Documents/GitHub/automationtests/features/support/hooks.js:49:22
    at _combinedTickCallback (internal/process/next_tick.js:131:7)
    at process._tickCallback (internal/process/next_tick.js:180:9)
  errno: -2,
  code: 'ENOENT',
  syscall: 'open',
  path: '/report/cucumber.json' }

Do the above code automatically generates .json and .html file or we need to manually create a .json file and converts that into a html report.

I have worked on Java and it automatically creates the json and html reports at the end the execution.

As this is very new I am not able to figure out whats the missing part

Thanks

1

There are 1 best solutions below

0
On BEST ANSWER

Your code to generate HTML report will expect the json file: /report/cucumber.json had been exist.

So you need other code to help to generate the json file during test running, I will give code used in my project for your reference.

Note: below code can only work on Cucumber 1, can't work on Cucumver 2, below is the version I used:

  "dependencies": {
    "cucumber": "1.2.1",
    "cucumber-html-reporter": "0.2.6",

1) cucumber-json-report.js to generate Cucumber JSON report during running.

var fs = require('fs-extra');
var path = require('path');
var moment = require('moment');
var Cucumber = require('cucumber');

module.exports = function() {

  var JsonFormatter = Cucumber.Listener.JsonFormatter();

  JsonFormatter.log = function(string) {

    var outputDir = './reports';
    var targetJson = outputDir + '/cucumber_report.json';

    if (fs.existsSync(outputDir)) {
      fs.moveSync(outputDir, outputDir + '_' + moment().format('YYYYMMDD_HHmmss') + "_" + Math.floor(Math.random() * 10000), {
        overwrite: true
      });
    }
    fs.mkdirSync(outputDir);
    fs.writeFileSync(targetJson, string);
  };

  this.registerListener(JsonFormatter);
};

2) screenshot.js to take screenshot when failure

module.exports = function() {

  this.After(function(scenario, callback) {
    if (scenario.isFailed()) {
      browser.takeScreenshot().then(function(buffer) {
        var decodedImage = new Buffer(buffer, 'base64');
        scenario.attach(decodedImage, 'image/png');
        callback();
      }, function(err) {
        callback(err);
      });
    } else {
      callback();
    }
  });
};

3) cucumber-html-report.js to generate Cucumber HTML report after all features running end.

var reporter = require('cucumber-html-reporter');

    module.exports = function() {

      this.AfterFeatures(function(features, callback) {
        var options = {
          theme: 'bootstrap',
          jsonFile: 'reports/cucumber_report.json',
          output: 'reports/cucumber_report.html',
          reportSuiteAsScenarios: true
        };

        reporter.generate(options);
        callback();
      });
};

4) Protractor conf.js to include above three files in cucumberOpts.require

cucumberOpts: {
    monochrome: true,
    strict: true,
    plugin: ["pretty"],
    require:[
        './step_definitions/*step.js',
        './support/screenshot.js',
        './support/cucumber-json-report.js',
        './support/cucumber-html-report.js'
    ],
    tags: '',
},