Manya
Manya

Reputation: 315

Protractor Cucumber JUnit XML report

I wish to create JUnit style XML reports from Protractor - Cucumber tests so that they can be used by CI.

Is there any detailed step on how this can be achieved?

Got the protractor-cucumber-junit npm library from below link but the documentation is not elaborate.

https://www.npmjs.com/package/protractor-cucumber-junit

Also the page points to a better plugin called 'cucumberjs-junitxml'. The documentation for this is given at

https://github.com/sonyschan/cucumberjs-junitxml

This too is not very helpful.

Questions:

  1. What are the elaborate steps to be followed after installing the plugins to get the final XML?
  2. What changes need to be done in the protractor config file or any other place within the project?

Upvotes: 4

Views: 4397

Answers (1)

Ram Pasala
Ram Pasala

Reputation: 5231

In order to generate the cucumber junit xml, first you need to generate cucumber json report:

1) Create a separate js file you can give it any name it would be better if you put this in hooks.js with all your before & after hooks

var Cucumber = require('cucumber'); // npm install --save cucumber
var fs = require('fs');

var hooks = function () {
var outputDir = './Reports/';
var JsonFormatter = Cucumber.Listener.JsonFormatter();

JsonFormatter.log = function (string) {
if (!fs.existsSync(outputDir)) {
  fs.mkdirSync(outputDir);
}

var targetJson = outputDir + 'cucumber_report.json';
fs.writeFile(targetJson, string, function (err) {
  if (err) {
    console.log('Failed to save cucumber test results to json file.');
    console.log(err);
  }
   });
  };
   this.registerListener(JsonFormatter);
 }
 module.exports = hooks;

This would create cucumber_report.json file in Reports folder in your current directory.

2) You can consume this json report with the cucumber-junit library https://www.npmjs.com/package/cucumber-junit or one of the libraries you mentioned above , all are similar & follow the simple instructions & then simply run the below command

$ cat ./Reports/cucumber_report.json | ./node_modules/.bin/cucumber-junit > cucumber_report.xml

It would generate the cucumber_report.xml file.

Upvotes: 6

Related Questions