You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
#### Saving CucumberJS json to disk when using Protractor
50
+
#### Saving CucumberJS JSON to disk when using Protractor
51
51
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.
52
52
53
53
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.
54
54
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
+
55
59
```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
+
functionmkdirp(path, root) {
73
+
var dirs =path.split('/'), dir =dirs.shift(), root = (root ||'') + dir +'/';
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() {
62
117
JsonFormatter.log = function (json) {
63
-
var destination =path.join(__dirname, '../../reports/cucumber-test-results.json');
118
+
var destination = path.join(__dirname, reportFile);
64
119
fs.open(destination, 'w+', function (err, fd) {
65
120
if (err) {
66
121
fs.mkdirsSync(destination);
@@ -77,7 +132,7 @@ module.exports = function JsonOutputHook() {
77
132
};
78
133
```
79
134
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)
81
136
82
137
#### Setting up Protractor, CucumberJS and the JSON listener
83
138
@@ -123,6 +178,9 @@ In lieu of a formal styleguide, take care to maintain the existing coding style.
123
178
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.
124
179
125
180
## 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.
0 commit comments