Skip to content

Commit 5e48ad0

Browse files
author
Marc Rooding
committed
Added another way to use the JSON output hook
1 parent cfc86b9 commit 5e48ad0

File tree

1 file changed

+65
-7
lines changed

1 file changed

+65
-7
lines changed

README.md

Lines changed: 65 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -47,20 +47,75 @@ gulp.src('./cucumber-test-results.json')
4747
}));
4848
```
4949

50-
#### Saving CucumberJS json to disk when using Protractor
50+
#### Saving CucumberJS JSON to disk when using Protractor
5151
If you're using Protractor in combination with CucumberJS there currently is [no way](https://github.com/cucumber/cucumber-js/issues/90) to save the CucumberJS JSON output to a file.
5252

5353
It is however possible to add a listener to the CucumberJS JSON formatter and save it to a file manually. The following hook can be added to your project and included to your Protractor configuration.
5454

55+
I've added 2 different hooks which basically do the same but one of the 2 requires you to add an extra dependency to your package.json. You're free to choose which one you prefer.
56+
57+
##### Without any extra dependencies
58+
5559
```js
60+
var Cucumber = require('cucumber'),
61+
fs = require('fs');
62+
path = require('path');
63+
64+
var JsonFormatter = Cucumber.Listener.JsonFormatter();
65+
66+
var reportDirectory = 'reports/one/two/';
67+
var reportFileName = 'cucumber-test-results.json';
68+
69+
var reportDirectoryPath = path.join(__dirname, '../../' + reportDirectory);
70+
var reportFilePath = path.join(reportDirectoryPath + reportFileName);
71+
72+
function mkdirp(path, root) {
73+
var dirs = path.split('/'), dir = dirs.shift(), root = (root || '') + dir + '/';
74+
75+
try {
76+
fs.mkdirSync(root);
77+
} catch (e) {
78+
if(!fs.statSync(root).isDirectory()) throw new Error(e);
79+
}
80+
81+
return !dirs.length || mkdirp(dirs.join('/'), root);
82+
}
83+
5684
module.exports = function JsonOutputHook() {
57-
var Cucumber = require('cucumber');
58-
var JsonFormatter = Cucumber.Listener.JsonFormatter();
59-
var fs = require('fs');
60-
var path = require('path');
85+
JsonFormatter.log = function (json) {
86+
fs.open(reportFilePath, 'w+', function (err, fd) {
87+
if (err) {
88+
mkdirp(reportDirectoryPath);
89+
fd = fs.openSync(reportFilePath, 'w+');
90+
}
91+
92+
fs.writeSync(fd, json);
93+
94+
console.log('json file location: ' + reportFilePath);
95+
});
96+
};
97+
98+
this.registerListener(JsonFormatter);
99+
};
100+
101+
```
102+
103+
##### With the fs-extra dependency
61104

105+
If you're going for this implementation, be sure to add fs-extra(https://www.npmjs.com/package/fs-extra) to your package.json in order for this to work.
106+
107+
```js
108+
var Cucumber = require('cucumber');
109+
fs = require('fs-extra');
110+
path = require('path');
111+
112+
var JsonFormatter = Cucumber.Listener.JsonFormatter();
113+
114+
var reportFile = ''../../reports/cucumber-test-results.json';
115+
116+
module.exports = function JsonOutputHook() {
62117
JsonFormatter.log = function (json) {
63-
var destination = path.join(__dirname, '../../reports/cucumber-test-results.json');
118+
var destination = path.join(__dirname, reportFile);
64119
fs.open(destination, 'w+', function (err, fd) {
65120
if (err) {
66121
fs.mkdirsSync(destination);
@@ -77,7 +132,7 @@ module.exports = function JsonOutputHook() {
77132
};
78133
```
79134
80-
Above snippet will hook into the CucumberJS JSON formatter and save the JSON to a file called 'cucumber-test-results.json' in the './reports' folder (relative from this file's location)
135+
Both 2 snippets above will hook into the CucumberJS JSON formatter and save the JSON to a file called 'cucumber-test-results.json' in the '../../reports' folder (relative from this file's location)
81136

82137
#### Setting up Protractor, CucumberJS and the JSON listener
83138

@@ -123,6 +178,9 @@ In lieu of a formal styleguide, take care to maintain the existing coding style.
123178
Copyright for portions of project [gulp-protractor-cucumber-html-report](https://github.com/mrooding/gulp-protractor-cucumber-html-report) are held by Robert Hilscher, 2015 as part of project [grunt-protractor-cucumber-html-report](https://github.com/robhil/grunt-protractor-cucumber-html-report). All other copyright for project [gulp-protractor-cucumber-html-report](https://github.com/mrooding/gulp-protractor-cucumber-html-report) are held by Marc Rooding, 2015.
124179

125180
## Release History
181+
0.0.9:
182+
- The readme now contains 2 different ways to use the JSON output hook. One using an external library and one without.
183+
126184
0.0.8:
127185
- Fix for not ignoring the After screenshot step
128186

0 commit comments

Comments
 (0)