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

Make JSON output to be ready to convert to JUnitXML for CI usage #1719

Closed
wants to merge 1 commit into from
Closed
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
118 changes: 88 additions & 30 deletions lib/frameworks/cucumber.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,39 +59,62 @@ exports.run = function(runner, specs) {
global.cucumber = Cucumber.Cli(execOptions);

var testResult = [];
var stepResults = {
description: null,
assertions: [],
duration: 0
};
var scenarioFailed = false;

var scenarioFailed = false;
var failedCount = 0;
// Add a listener into cucumber so that protractor can find out which
// steps passed/failed
var addResultListener = function(formatter) {
var originalHandleAfterScenarioEvent = formatter.handleAfterScenarioEvent;
formatter.handleAfterScenarioEvent = function(event, callback) {

var originalHandleBeforeFeatureEvent = formatter.handleBeforeFeatureEvent;
formatter.handleBeforeFeatureEvent = function (event, callback) {
var feature = event.getPayloadItem('feature');
var currentFeatureId = feature.getName().replace(/ /g, '-');
var featureOutput = {
id: currentFeatureId,
name: feature.getName(),
description: feature.getDescription(),
line: feature.getLine(),
keyword: feature.getKeyword(),
uri: feature.getUri(),
elements: []
};

testResult.push(featureOutput);

if (originalHandleBeforeFeatureEvent
&& typeof(originalHandleBeforeFeatureEvent) === 'function') {
originalHandleBeforeFeatureEvent(event, callback);
} else {
callback();
}
}

var originalHandleBeforeScenarioEvent = formatter.handleBeforeScenarioEvent;
formatter.handleBeforeScenarioEvent = function(event, callback) {
var scenario = event.getPayloadItem('scenario');
stepResults.description = scenario.getName();
var currentScenarioId = testResult[testResult.length - 1].id + ';' + scenario.getName().replace(/ /g, '-');
var scenarioOutput = {
id: currentScenarioId,
name: scenario.getName(),
description: scenario.getDescription(),
line: scenario.getLine(),
keyword: scenario.getKeyword(),
steps: []
};

if (scenarioFailed) {
++failedCount;
runner.emit('testFail');
} else {
runner.emit('testPass');
}

testResult.push(stepResults);
stepResults = {
description: null,
assertions: [],
duration: 0
};
scenarioFailed = false;
testResult[testResult.length - 1].elements.push(scenarioOutput);

if (originalHandleAfterScenarioEvent
&& typeof(originalHandleAfterScenarioEvent) === 'function') {
originalHandleAfterScenarioEvent(event, callback);
if (originalHandleBeforeScenarioEvent
&& typeof(originalHandleBeforeScenarioEvent) === 'function') {
originalHandleBeforeScenarioEvent(event, callback);
} else {
callback();
}
Expand All @@ -100,22 +123,57 @@ exports.run = function(runner, specs) {
var originalHandleStepResultEvent = formatter.handleStepResultEvent;
formatter.handleStepResultEvent = function(event, callback) {
var stepResult = event.getPayloadItem('stepResult');
var steps = stepResult.getStep();

var stepOutput = {
name: steps.getName(),
line: steps.getLine(),
keyword: steps.getKeyword(),
results: {},
match: {}
};
var resultStatus;
var attachments;

if (stepResult.isSuccessful()) {
stepResults.assertions.push({
passed: true
});
stepResults.duration += stepResult.getDuration();
} else if (stepResult.isFailed()) {
scenarioFailed = true;
resultStatus = 'passed';
if (stepResult.hasAttachments()) {
attachments = stepResult.getAttachments();
}
stepOutput.results.duration = stepResult.getDuration();
}
else if (stepResult.isPending()) {
resultStatus = 'pending';
stepOutput.results.error_message = undefined;
}
else if (stepResult.isSkipped()) {
resultStatus = 'skipped';
}
else if (stepResult.isUndefined()) {
resultStatus = 'undefined';
}
else {
resultStatus = 'failed';
var failureMessage = stepResult.getFailureException();
stepResults.assertions.push({
passed: false,
errorMsg: failureMessage.message,
stackTrace: failureMessage.stack
if (failureMessage) {
stepOutput.results.error_message = (failureMessage.stack || failureMessage);
}
if (stepResult.hasAttachments()) {
attachments = stepResult.getAttachments();
}
stepOutput.results.duration = stepResult.getDuration();
}

stepOutput.results.status = resultStatus;

if (attachments) {
attachments.syncForEach(function(attachment) {
//TODO: formatter.embedding
});
stepResults.duration += stepResult.getDuration();
}

testResult[testResult.length - 1].elements[testResult[testResult.length - 1].elements.length - 1].steps.push(stepOutput);

if (originalHandleStepResultEvent
&& typeof(originalHandleStepResultEvent) === 'function') {
originalHandleStepResultEvent(event, callback);
Expand Down