|
1 | 1 | /* eslint-disable prefer-template */ |
| 2 | +const statuses = require("cucumber").Status; |
2 | 3 | const { resolveAndRunStepDefinition } = require("./resolveStepDefinition"); |
| 4 | +const { generateCucumberJson } = require("./cukejson/generateCucumberJson"); |
3 | 5 |
|
4 | 6 | const replaceParameterTags = (rowData, text) => |
5 | 7 | Object.keys(rowData).reduce( |
6 | 8 | (value, key) => value.replace(`<${key}>`, rowData[key]), |
7 | 9 | text |
8 | 10 | ); |
9 | 11 |
|
10 | | -const stepTest = function(stepDetails, exampleRowData) { |
11 | | - cy.log(`${stepDetails.keyword} ${stepDetails.text}`); |
12 | | - resolveAndRunStepDefinition.call( |
13 | | - this, |
14 | | - stepDetails, |
15 | | - replaceParameterTags, |
16 | | - exampleRowData |
| 12 | +// eslint-disable-next-line func-names |
| 13 | +const stepTest = function(state, stepDetails, exampleRowData) { |
| 14 | + cy.then(() => state.onStartStep(stepDetails)) |
| 15 | + .then(() => |
| 16 | + resolveAndRunStepDefinition.call( |
| 17 | + this, |
| 18 | + stepDetails, |
| 19 | + replaceParameterTags, |
| 20 | + exampleRowData |
| 21 | + ) |
| 22 | + ) |
| 23 | + .then(() => state.onFinishStep(stepDetails, statuses.PASSED)); |
| 24 | +}; |
| 25 | + |
| 26 | +const runTest = (scenario, stepsToRun, rowData) => { |
| 27 | + const indexedSteps = stepsToRun.map((step, index) => |
| 28 | + Object.assign({}, step, { index }) |
17 | 29 | ); |
| 30 | + |
| 31 | + // eslint-disable-next-line func-names |
| 32 | + it(`Scenario: ${scenario.name}`, function() { |
| 33 | + const state = window.testState; |
| 34 | + return cy |
| 35 | + .then(() => state.onStartScenario(scenario, indexedSteps)) |
| 36 | + .then(() => |
| 37 | + indexedSteps.forEach(step => stepTest.call(this, state, step, rowData)) |
| 38 | + ) |
| 39 | + .then(() => state.onFinishScenario(scenario)); |
| 40 | + }); |
18 | 41 | }; |
19 | 42 |
|
20 | | -const createTestFromScenario = (scenario, backgroundSection) => { |
21 | | - if (scenario.examples) { |
22 | | - scenario.examples.forEach(example => { |
23 | | - const exampleValues = []; |
| 43 | +const writeCucumberJsonFile = json => { |
| 44 | + const outputFolder = |
| 45 | + window.cucumberJson.outputFolder || "cypress/cucumber-json"; |
| 46 | + const outputPrefix = window.cucumberJson.filePrefix || ""; |
| 47 | + const outputSuffix = window.cucumberJson.fileSuffix || ".cucumber"; |
| 48 | + const fileName = json[0] ? json[0].uri : "empty"; |
| 49 | + const outFile = `${outputFolder}/${outputPrefix}${fileName}${outputSuffix}.json`; |
| 50 | + cy.writeFile(outFile, json, { log: false }); |
| 51 | +}; |
| 52 | + |
| 53 | +const createTestFromScenarios = ( |
| 54 | + scenariosToRun, |
| 55 | + backgroundSection, |
| 56 | + testState |
| 57 | +) => { |
| 58 | + // eslint-disable-next-line func-names |
| 59 | + describe(`Feature: ${testState.feature.name}`, function() { |
| 60 | + before(() => { |
| 61 | + cy.then(() => testState.onStartTest()); |
| 62 | + }); |
| 63 | + |
| 64 | + // ctx is cleared between each 'it' |
| 65 | + // eslint-disable-next-line func-names, prefer-arrow-callback |
| 66 | + beforeEach(function() { |
| 67 | + window.testState = testState; |
| 68 | + |
| 69 | + const failHandler = err => { |
| 70 | + Cypress.off("fail", failHandler); |
| 71 | + testState.onFail(err); |
| 72 | + throw err; |
| 73 | + }; |
| 74 | + |
| 75 | + Cypress.on("fail", failHandler); |
| 76 | + }); |
| 77 | + |
| 78 | + scenariosToRun.forEach(section => { |
| 79 | + if (section.examples) { |
| 80 | + section.examples.forEach(example => { |
| 81 | + const exampleValues = []; |
| 82 | + const exampleLocations = []; |
24 | 83 |
|
25 | | - example.tableBody.forEach((row, rowIndex) => { |
26 | | - example.tableHeader.cells.forEach((header, headerIndex) => { |
27 | | - exampleValues[rowIndex] = Object.assign({}, exampleValues[rowIndex], { |
28 | | - [header.value]: row.cells[headerIndex].value |
| 84 | + example.tableBody.forEach((row, rowIndex) => { |
| 85 | + exampleLocations[rowIndex] = row.location; |
| 86 | + example.tableHeader.cells.forEach((header, headerIndex) => { |
| 87 | + exampleValues[rowIndex] = Object.assign( |
| 88 | + {}, |
| 89 | + exampleValues[rowIndex], |
| 90 | + { |
| 91 | + [header.value]: row.cells[headerIndex].value |
| 92 | + } |
| 93 | + ); |
| 94 | + }); |
29 | 95 | }); |
30 | | - }); |
31 | | - }); |
32 | 96 |
|
33 | | - exampleValues.forEach((rowData, index) => { |
34 | | - // eslint-disable-next-line prefer-arrow-callback |
35 | | - const scenarioName = replaceParameterTags(rowData, scenario.name); |
36 | | - it(`${scenarioName} (example #${index + 1})`, function() { |
37 | | - if (backgroundSection) { |
38 | | - backgroundSection.steps.forEach(step => { |
39 | | - stepTest.call(this, step); |
| 97 | + exampleValues.forEach((rowData, index) => { |
| 98 | + // eslint-disable-next-line prefer-arrow-callback |
| 99 | + const scenarioName = replaceParameterTags(rowData, section.name); |
| 100 | + const uniqueScenarioName = `${scenarioName} (example #${index + |
| 101 | + 1})`; |
| 102 | + const exampleSteps = section.steps.map(step => { |
| 103 | + const newStep = Object.assign({}, step); |
| 104 | + newStep.text = replaceParameterTags(rowData, newStep.text); |
| 105 | + return newStep; |
40 | 106 | }); |
41 | | - } |
42 | 107 |
|
43 | | - scenario.steps.forEach(step => { |
44 | | - const newStep = Object.assign({}, step); |
45 | | - newStep.text = replaceParameterTags(rowData, newStep.text); |
| 108 | + const stepsToRun = backgroundSection |
| 109 | + ? backgroundSection.steps.concat(exampleSteps) |
| 110 | + : exampleSteps; |
46 | 111 |
|
47 | | - stepTest.call(this, newStep, rowData); |
| 112 | + const scenarioExample = Object.assign({}, section, { |
| 113 | + name: uniqueScenarioName, |
| 114 | + example: exampleLocations[index] |
| 115 | + }); |
| 116 | + |
| 117 | + runTest.call(this, scenarioExample, stepsToRun, rowData); |
48 | 118 | }); |
49 | 119 | }); |
50 | | - }); |
51 | | - }); |
52 | | - } else { |
53 | | - it(scenario.name, function() { |
54 | | - if (backgroundSection) { |
55 | | - backgroundSection.steps.forEach(step => stepTest.call(this, step)); |
| 120 | + } else { |
| 121 | + const stepsToRun = backgroundSection |
| 122 | + ? backgroundSection.steps.concat(section.steps) |
| 123 | + : section.steps; |
| 124 | + |
| 125 | + runTest.call(this, section, stepsToRun); |
56 | 126 | } |
57 | | - scenario.steps.forEach(step => stepTest.call(this, step)); |
58 | 127 | }); |
59 | | - } |
| 128 | + |
| 129 | + // eslint-disable-next-line func-names, prefer-arrow-callback |
| 130 | + after(function() { |
| 131 | + cy.then(() => testState.onFinishTest()).then(() => { |
| 132 | + if (window.cucumberJson.generate) { |
| 133 | + const json = generateCucumberJson(testState); |
| 134 | + writeCucumberJsonFile(json); |
| 135 | + } |
| 136 | + }); |
| 137 | + }); |
| 138 | + }); |
60 | 139 | }; |
61 | 140 |
|
62 | 141 | module.exports = { |
63 | | - createTestFromScenario |
| 142 | + createTestFromScenarios |
64 | 143 | }; |
0 commit comments