Skip to content

Commit 30482ef

Browse files
holgergplgandecki
authored andcommitted
feat: Scenario outline and data tables (#142)
* Providing failing test for Scenario Outlines in conjunction with data-tables * Test is green * Define function earlier * Replacing Object.spread with Object.assign. Cypress Tests seem to fail on that. * changed variablename * Replaced with earlier version
1 parent a8d3df6 commit 30482ef

File tree

6 files changed

+75
-18
lines changed

6 files changed

+75
-18
lines changed

cypress/integration/ScenarioOutline.feature

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,19 @@ Feature: Being a plugin handling Scenario Outline
1818
Then I should get "<juice>"
1919

2020
Examples:
21-
| fruit | juice |
22-
| apple | apple juice |
23-
| pineapple | pineapple juice |
24-
| strawberry | strawberry juice |
21+
| fruit | juice |
22+
| apple | apple juice |
23+
| pineapple | pineapple juice |
24+
| strawberry | strawberry juice |
25+
26+
Scenario Outline: A calculation
27+
When I enter <first> and <second>
28+
Then I see following result table
29+
| first | second | result |
30+
| <first> | <second> | <result> |
31+
32+
Examples:
33+
| first | second | result |
34+
| 1 | 2 | 3 |
35+
| 3 | 4 | 7 |
36+
| 5 | 6 | 11 |
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/* global then, when */
2+
3+
let sum = { first: 0, second: 0, result: 0 };
4+
5+
when("I enter {int} and {int}", (a, b) => {
6+
sum = { first: a, second: b, result: a + b };
7+
});
8+
9+
then("I see following result table", dataTable => {
10+
dataTable.hashes().forEach(row => {
11+
expect(sum.first).to.equal(parseInt(row.first, 10));
12+
expect(sum.second).to.equal(parseInt(row.second, 10));
13+
expect(sum.result).to.equal(parseInt(row.result, 10));
14+
});
15+
});

lib/createTestFromScenario.js

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,22 @@
11
/* eslint-disable prefer-template */
22
const { resolveAndRunStepDefinition } = require("./resolveStepDefinition");
33

4-
const stepTest = function(stepDetails) {
5-
cy.log(`${stepDetails.keyword} ${stepDetails.text}`);
6-
resolveAndRunStepDefinition.call(this, stepDetails);
7-
};
8-
94
const replaceParameterTags = (rowData, text) =>
105
Object.keys(rowData).reduce(
116
(value, key) => value.replace(`<${key}>`, rowData[key]),
127
text
138
);
149

10+
const stepTest = function(stepDetails, exampleRowData) {
11+
cy.log(`${stepDetails.keyword} ${stepDetails.text}`);
12+
resolveAndRunStepDefinition.call(
13+
this,
14+
stepDetails,
15+
replaceParameterTags,
16+
exampleRowData
17+
);
18+
};
19+
1520
const createTestFromScenario = (scenario, backgroundSection) => {
1621
if (scenario.examples) {
1722
scenario.examples.forEach(example => {
@@ -39,7 +44,7 @@ const createTestFromScenario = (scenario, backgroundSection) => {
3944
const newStep = Object.assign({}, step);
4045
newStep.text = replaceParameterTags(rowData, newStep.text);
4146

42-
stepTest.call(this, newStep);
47+
stepTest.call(this, newStep, rowData);
4348
});
4449
});
4550
});

lib/resolveStepDefinition.js

Lines changed: 31 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -50,24 +50,47 @@ function resolveStepDefinition(step) {
5050

5151
module.exports = {
5252
// eslint-disable-next-line func-names
53-
resolveAndRunStepDefinition(step) {
53+
resolveAndRunStepDefinition(step, replaceParameterTags, exampleRowData) {
54+
const modifiedStep = step;
5455
const { expression, implementation } = resolveStepDefinition(step);
5556
if (expression && implementation) {
5657
let argument;
57-
if (step.argument) {
58-
if (step.argument.type === "DataTable") {
59-
argument = new DataTable(step.argument);
60-
} else if (step.argument.type === "DocString") {
61-
argument = step.argument.content;
58+
if (modifiedStep.argument) {
59+
if (modifiedStep.argument.type === "DataTable") {
60+
if (exampleRowData) {
61+
if (!modifiedStep.argument.templateRows) {
62+
modifiedStep.argument.templateRows = modifiedStep.argument.rows;
63+
}
64+
const scenarioDataTableRows = modifiedStep.argument.templateRows.map(
65+
tr => {
66+
if (tr && tr.type === "TableRow") {
67+
const cells = {
68+
cells: tr.cells.map(c => {
69+
const value = {
70+
value: replaceParameterTags(exampleRowData, c.value)
71+
};
72+
return Object.assign({}, c, value);
73+
})
74+
};
75+
return Object.assign({}, tr, cells);
76+
}
77+
return tr;
78+
}
79+
);
80+
modifiedStep.argument.rows = scenarioDataTableRows;
81+
}
82+
argument = new DataTable(modifiedStep.argument);
83+
} else if (modifiedStep.argument.type === "DocString") {
84+
argument = modifiedStep.argument.content;
6285
}
6386
}
6487
return implementation.call(
6588
this,
66-
...expression.match(step.text).map(match => match.getValue()),
89+
...expression.match(modifiedStep.text).map(match => match.getValue()),
6790
argument
6891
);
6992
}
70-
throw new Error(`Step implementation missing for: ${step.text}`);
93+
throw new Error(`Step implementation missing for: ${modifiedStep.text}`);
7194
},
7295
given: (expression, implementation) => {
7396
stepDefinitionRegistry.runtime(expression, implementation);

lib/resolveStepDefinition.test.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ const readAndParseFeatureFile = featureFilePath => {
3030
describe("Scenario Outline", () => {
3131
require("../cypress/support/step_definitions/scenario_outline_integer");
3232
require("../cypress/support/step_definitions/scenario_outline_string");
33+
require("../cypress/support/step_definitions/scenario_outline_data_table");
3334

3435
createTestsFromFeature(
3536
readAndParseFeatureFile("./cypress/integration/ScenarioOutline.feature")

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
"main": "lib/index.js",
66
"scripts": {
77
"test": "eslint . && jest && cypress run",
8+
"test:debug": "jest && DEBUG=cypress:* cypress open\n",
89
"fixStyle": "eslint . --fix",
910
"semantic-release": "semantic-release"
1011
},

0 commit comments

Comments
 (0)