4

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?
Vince Bowdren
  • 8,326
  • 3
  • 31
  • 56
Manya
  • 315
  • 4
  • 17

1 Answers1

6

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.

Ram Pasala
  • 4,931
  • 3
  • 16
  • 26
  • Step 1 worked. Additionally I also had to add the js file created in require of cucumberOpts. For Step 2 - what is the windows equivalent of this command? I am executing on a Windows machine – Manya Jul 20 '16 at 20:51
  • 1
    Got it - type ./Reports/cucumber_report.json | ./node_modules/.bin/cucumber-junit > cucumber_report.xml – Manya Jul 20 '16 at 21:40
  • were you able to generate the xml report? – Ram Pasala Jul 21 '16 at 11:27
  • glad it worked! you could accept the answer if it solved your problem :) – Ram Pasala Jul 21 '16 at 15:55