Skip to content
This repository has been archived by the owner on Jul 29, 2024. It is now read-only.

add option to log reports to file or custom logTo function on cucumber #2680

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 64 additions & 1 deletion lib/frameworks/cucumber.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,53 @@
var ConfigParser = require('../configParser'),
q = require('q');

// wrap up configuration to put formatter logs into
// specified logTo function
function Configuration(execOptions,logToFunction) {

var Cucumber = require('cucumber');

var self = Cucumber.Cli.Configuration(execOptions);

var appendLogToFomratter = function(formatter) {
var originalLog = formatter.log;

formatter.log = function(data) {
if (logToFunction) {
logToFunction(data);
}
originalLog(data);
};
};

if(self.getFormatter) {
var originalGetFormatter= self.getFormatter;

self.getFormatter = function getFormatter() {
var formatter = originalGetFormatter();
appendLogToFomratter(formatter);

return formatter;

};
} else if(self.getFormatters) {
// if multi format
var originalGetFormatters= self.getFormatters;
self.getFormatters = function getFormatters() {
// add logto on the first formatter
var formatters = originalGetFormatters();

appendLogToFomratter(formatters[0]);

return formatters;

};
}

return self;
}


/**
* Execute the Runner's test cases through Cucumber.
*
Expand Down Expand Up @@ -155,9 +202,25 @@ exports.run = function(runner, specs) {
};
};


return runner.runTestPreparer().then(function() {
return q.promise(function(resolve, reject) {
var cucumberConf = Cucumber.Cli.Configuration(execOptions);

// set up logReport function to log formatter
var logReport;

if (runner.getConfig().cucumberOpts.logTo) {
if(typeof(runner.getConfig().cucumberOpts.logTo) === 'string') {
var fs = require('fs');
logReport = function(data) {
fs.appendFile(runner.getConfig().cucumberOpts.logTo,data);
};
} else if (typeof(runner.getConfig().cucumberOpts.logTo) === 'function') {
logReport = runner.getConfig().cucumberOpts.logTo;
}
}

var cucumberConf = new Configuration(execOptions,logReport);
var runtime = Cucumber.Runtime(cucumberConf);
var formatters = cucumberConf.getFormatter ?
[cucumberConf.getFormatter()] :
Expand Down
1 change: 1 addition & 0 deletions scripts/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ var passingTests = [
'node lib/cli.js spec/onPreparePromiseFileConf.js',
'node lib/cli.js spec/mochaConf.js',
'node lib/cli.js spec/cucumberConf.js',
'node node_modules/mocha/bin/mocha spec/cucumber_report_spec.js',
'node lib/cli.js spec/withLoginConf.js',
'node lib/cli.js spec/suitesConf.js --suite okmany',
'node lib/cli.js spec/suitesConf.js --suite okspec',
Expand Down
24 changes: 24 additions & 0 deletions spec/cucumber-report/cucumberConfReportFile.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
var env = require('../environment.js');

// A small suite to make sure the cucumber framework works.
exports.config = {
seleniumAddress: env.seleniumAddress,

framework: 'cucumber',

// Spec patterns are relative to this directory.
specs: [
'../cucumber/*.feature'
],

capabilities: env.capabilities,

baseUrl: env.baseUrl,

cucumberOpts: {
require: 'cucumber/stepDefinitions.js',
tags: '@report',
format: 'json',
logTo: 'cucumberReport.tmp'
}
};
28 changes: 28 additions & 0 deletions spec/cucumber-report/cucumberConfReportFunction.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
var env = require('../environment.js');

var fs = require('fs');

// A small suite to make sure the cucumber framework works.
exports.config = {
seleniumAddress: env.seleniumAddress,

framework: 'cucumber',

// Spec patterns are relative to this directory.
specs: [
'../cucumber/*.feature'
],

capabilities: env.capabilities,

baseUrl: env.baseUrl,

cucumberOpts: {
require: 'cucumber/stepDefinitions.js',
tags: '@report',
format: 'json',
logTo: function(data) {
fs.appendFile('cucumberReportFunc.tmp',data);
}
}
};
75 changes: 75 additions & 0 deletions spec/cucumber-report/cucumber_report_spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
// Use the external Chai As Promised to deal with resolving promises in
// expectations.
var chai = require('chai');
var chaiAsPromised = require('chai-as-promised');
chai.use(chaiAsPromised);

var child_process = require('child_process'),
q = require('q'),
fs = require('fs');



var expect = chai.expect;

function spawnCucumberTests(configFile) {
return q.promise(function(resolve, reject) {

var output = '';
var test_process;
test_process = child_process.spawn('node',['lib/cli.js',configFile]);


test_process.on('error', function(err) {
reject(err);
});

test_process.on('exit', function(exitCode) {
resolve(exitCode);
});
});
}

// Chai's expect().to.exist style makes default jshint unhappy.
// jshint expr:true

describe('generate reports for cucumber', function() {


it('should generate a report in the specified file', function(done) {

this.timeout(10000);
var cucumberTestProcess = spawnCucumberTests('spec/cucumber-report/cucumberConfReportFile.js');
expect(cucumberTestProcess).to.eventually.equal(0).then(function() {
console.log('now check file');
return expect(q.promise(function(resolve,reject)
{
fs.exists('cucumberReport.tmp',resolve);
}
)).to.eventually.be.true;
}).then(function(){
console.log('now deleting file');
return q.promise(function(resolve,reject) { fs.unlink('cucumberReport.tmp',resolve); });
}).then(done,done);

});

it('should generate a report in the specified logTo function', function(done) {

this.timeout(10000);
var cucumberTestProcess = spawnCucumberTests('spec/cucumber-report/cucumberConfReportFunction.js');
expect(cucumberTestProcess).to.eventually.equal(0).then(function() {
console.log('now check file');
return expect(q.promise(function(resolve,reject)
{
fs.exists('cucumberReportFunc.tmp',resolve);
}
)).to.eventually.be.true;
}).then(function(){
console.log('now deleting file');
return q.promise(function(resolve,reject) { fs.unlink('cucumberReportFunc.tmp',resolve); });
}).then(done,done);

});

});
2 changes: 1 addition & 1 deletion spec/cucumber/lib.feature
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ Feature: Running Cucumber with Protractor
Then it should still do normal tests
Then it should expose the correct global variables

@dev
@dev @report
Scenario: Wrapping WebDriver
Given I go on "index.html"
Then the title should equal "My AngularJS App"