Skip to content

Commit

Permalink
Add scenario outline data to scenario title
Browse files Browse the repository at this point in the history
  • Loading branch information
alextiley committed Jul 15, 2018
1 parent d62feec commit c956b32
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 2 deletions.
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "wdio-json-cucumber-reporter",
"version": "1.2.1",
"version": "1.2.2",
"description": "Generates a JSON report for cucumber via webdriver.io",
"main": "lib/reporter.js",
"directories": {
Expand All @@ -21,7 +21,9 @@
"author": "Alex Tiley",
"license": "MIT",
"dependencies": {
"diff": "^3.5.0",
"gherkin": "^5.1.0",
"lodash": "^4.17.10",
"mkdirp": "^0.5.1"
},
"devDependencies": {
Expand Down
27 changes: 27 additions & 0 deletions src/JSONBuilder.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import os from 'os';
import _ from 'lodash';

class JSONBuilder {
constructor() {
Expand Down Expand Up @@ -47,6 +48,7 @@ class JSONBuilder {
uri: options.uri,
line: options.line,
steps: [],
arguments: [],
};

if (scenarioIndex === -1) {
Expand Down Expand Up @@ -92,6 +94,9 @@ class JSONBuilder {
} else {
scenario.steps[stepIndex] = stepData;
}
// Attach arguments to the scenario.
// At the end of report generation, these argumentss can be appended to the scenario title
scenario.arguments = _.union(scenario.arguments, options.arguments);
}

addHook(options) {
Expand Down Expand Up @@ -151,6 +156,28 @@ class JSONBuilder {
})
}

/**
* Given a scenario with an array of arguments, flatten the arguments into
* a comma delmited string and append them to the scenario title
* The arguments array is also cleaned up as this is not part of the scenario JSON spec
* and is currently just used for convenience
* @param options
*/
appendScenarioArgumentsToTitle(options) {
const report = this.initialiseReport(options.cid);

const featureIndex = report.features.findIndex(feature => feature.id === options.parentId);
const scenarioIndex = report.features[featureIndex].elements
.findIndex(scenario => scenario.id === options.id);

const scenario = report.features[featureIndex].elements[scenarioIndex];

if (scenario.arguments.length > 0) {
scenario.name += ` (${scenario.arguments.join(', ')})`;
}
delete scenario.arguments;
}

clearEmptyFeatures() {
Object.keys(this.reports).forEach((key) => {
this.reports[key].features = this.reports[key].features.filter(
Expand Down
28 changes: 27 additions & 1 deletion src/reporter.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const mkdirp = require('mkdirp');
const fs = require('fs');
const path = require('path');
const Gherkin = require('gherkin');
const jsdiff = require('diff');

class CucumberJSONReporter extends EventEmitter {

Expand Down Expand Up @@ -59,6 +60,7 @@ class CucumberJSONReporter extends EventEmitter {
* Decorate features with meta data whenever a feature finishes
*/
this.on('suite:end', (suite) => {
// Feature
if (suite.parent === null) {
this.jsonBuilder.addMeta({
cid: suite.cid,
Expand All @@ -67,6 +69,13 @@ class CucumberJSONReporter extends EventEmitter {
? this.options.cucumberJsonReporter.deviceName
: 'Local test environment'
});
// Scenario
} else {
this.jsonBuilder.appendScenarioArgumentsToTitle({
cid: suite.cid,
id: suite.uid,
parentId: suite.parent
});
}
});

Expand Down Expand Up @@ -120,6 +129,7 @@ class CucumberJSONReporter extends EventEmitter {
uri: test.file,
parentId: test.parent,
keyword: step.keyword,
arguments: this.getArgumentsFromStep(step.text, test.title),
line: this.getLineNumberFromUid(test.uid),
result: {
status: 'passed',
Expand Down Expand Up @@ -152,6 +162,7 @@ class CucumberJSONReporter extends EventEmitter {
uri: test.file,
parentId: test.parent,
keyword: step.keyword,
arguments: this.getArgumentsFromStep(step.text, test.title),
line: this.getLineNumberFromUid(test.uid),
result: {
status: 'failed',
Expand Down Expand Up @@ -182,16 +193,18 @@ class CucumberJSONReporter extends EventEmitter {
}

const isPending = this.hasUndefinedStepMessage(test.title);
const runnerTitle = this.removeUndefinedStepFromTitle(test.title);

const stepData = {
cid: test.cid,
type: 'step',
name: this.removeUndefinedStepFromTitle(test.title),
name: runnerTitle,
id: test.uid,
tags: test.tags,
uri: test.file,
parentId: test.parent,
keyword: step.keyword,
arguments: this.getArgumentsFromStep(step.text, runnerTitle),
line: this.getLineNumberFromUid(test.uid),
result: {
status: isPending ? 'pending' : 'skipped',
Expand Down Expand Up @@ -327,6 +340,19 @@ class CucumberJSONReporter extends EventEmitter {
}
return Number(line);
}

getArgumentsFromStep(definitionName, testRunnerName) {
let args = [];
try {
definitionName = definitionName.replace(/[<>]+/g, '');

args = jsdiff.diffWords(definitionName, testRunnerName)
.filter(diff => (diff.added === true))
.map(diff => diff.value);
} catch (e) {}

return args;
}
}

CucumberJSONReporter.reporterName = 'json-cucumber';
Expand Down
8 changes: 8 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -656,6 +656,10 @@ detect-libc@^1.0.2:
version "1.0.3"
resolved "https://registry.yarnpkg.com/detect-libc/-/detect-libc-1.0.3.tgz#fa137c4bd698edf55cd5cd02ac559f91a4c4ba9b"

diff@^3.5.0:
version "3.5.0"
resolved "https://npm.fontawesome.com/339BE114-8D5E-4407-A528-4D5536B5573A/diff/-/diff-3.5.0.tgz#800c0dd1e0a8bfbc95835c202ad220fe317e5a12"

electron-to-chromium@^1.3.47:
version "1.3.48"
resolved "https://registry.yarnpkg.com/electron-to-chromium/-/electron-to-chromium-1.3.48.tgz#d3b0d8593814044e092ece2108fc3ac9aea4b900"
Expand Down Expand Up @@ -934,6 +938,10 @@ kind-of@^6.0.0:
version "6.0.2"
resolved "https://registry.yarnpkg.com/kind-of/-/kind-of-6.0.2.tgz#01146b36a6218e64e58f3a8d66de5d7fc6f6d051"

lodash@^4.17.10:
version "4.17.10"
resolved "https://npm.fontawesome.com/339BE114-8D5E-4407-A528-4D5536B5573A/lodash/-/lodash-4.17.10.tgz#1b7793cf7259ea38fb3661d4d38b3260af8ae4e7"

lodash@^4.17.4:
version "4.17.10"
resolved "https://registry.yarnpkg.com/lodash/-/lodash-4.17.10.tgz#1b7793cf7259ea38fb3661d4d38b3260af8ae4e7"
Expand Down

0 comments on commit c956b32

Please sign in to comment.