Skip to content
This repository has been archived by the owner on Dec 18, 2019. It is now read-only.

Fix support for Scenario Outlines #126

Closed
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
43 changes: 29 additions & 14 deletions lib/cucumberEventListener.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,28 @@
import { EventEmitter } from 'events'

function findScenarioByExampleLocation (gherkinDoc, sourceLocation) {
var scenariosWithExamples = gherkinDoc.feature.children.filter(scenario => scenario.examples)
return scenariosWithExamples.find(child =>
child.examples.find(example =>
example.tableBody.find(exampleBody =>
exampleBody.location.line === sourceLocation.line
)
)
)
}

function findStepOrExampleByLocation (gherkinDoc, testCasePreparedEvents, stepEvent, sourceLocation) {
var uri = stepEvent.testCase.sourceLocation.uri
var preparedTestCase = testCasePreparedEvents.find(tcpe => tcpe.sourceLocation.uri === uri && tcpe.sourceLocation.line === sourceLocation.line)
var preparedTestCaseStep = preparedTestCase.steps[stepEvent.index]
var allSteps = gherkinDoc.feature.children.map(child => child.steps).reduce((list, childSteps) => list.concat(childSteps), [])
var allExamples = gherkinDoc.feature.children.map(child => child.examples).reduce((list, childExamples) => list.concat(childExamples || []), [])
var allExampleRows = allExamples.map(example => example.tableBody).reduce((list, exampleRow) => list.concat(exampleRow || []), [])
var line = (preparedTestCaseStep.sourceLocation || preparedTestCaseStep.actionLocation).line
return allSteps.find(childStep => childStep.location.line === line) ||
allExampleRows.find(exampleRow => exampleRow.location.line === line)
}

export class CucumberEventListener extends EventEmitter {
gherkinDocEvents = []
testCasePreparedEvents = []
Expand Down Expand Up @@ -95,12 +118,8 @@ export class CucumberEventListener extends EventEmitter {

const doc = this.gherkinDocEvents.find(gde => gde.uri === uri).document
const feature = doc.feature
const scenario = doc.feature.children.find(child => child.location.line === sourceLocation.line)
const preparedTestCase = this.testCasePreparedEvents.find(tcpe => tcpe.sourceLocation.uri === uri && tcpe.sourceLocation.line === sourceLocation.line)
const preparedTestCaseStep = preparedTestCase.steps[testStepStartedEvent.index]
const allSteps = feature.children.map(child => child.steps).reduce((list, childSteps) => list.concat(childSteps), [])
const step = allSteps.find(childStep => childStep.location.line === preparedTestCaseStep.sourceLocation.line)

const scenario = doc.feature.children.find(child => child.location.line === sourceLocation.line) || findScenarioByExampleLocation(doc, sourceLocation)
const step = findStepOrExampleByLocation(doc, this.testCasePreparedEvents, testStepStartedEvent, sourceLocation)
this.emit('before-step', uri, feature, scenario, step)
}

Expand All @@ -121,7 +140,7 @@ export class CucumberEventListener extends EventEmitter {
const uri = sourceLocation.uri

const doc = this.gherkinDocEvents.find(gde => gde.uri === uri).document
const scenario = doc.feature.children.find(child => child.location.line === sourceLocation.line)
const scenario = doc.feature.children.find(child => child.location.line === sourceLocation.line) || findScenarioByExampleLocation(doc, sourceLocation)
const scenarioHasHooks = scenario.steps.filter((step) => step.type === 'Hook').length > 0
if (scenarioHasHooks) {
return
Expand Down Expand Up @@ -154,13 +173,9 @@ export class CucumberEventListener extends EventEmitter {

const doc = this.gherkinDocEvents.find(gde => gde.uri === uri).document
const feature = doc.feature
const scenario = doc.feature.children.find(child => child.location.line === sourceLocation.line)
const preparedTestCase = this.testCasePreparedEvents.find(tcpe => tcpe.sourceLocation.uri === uri && tcpe.sourceLocation.line === sourceLocation.line)
const preparedTestCaseStep = preparedTestCase.steps[testStepFinishedEvent.index]
const allSteps = feature.children.map(child => child.steps).reduce((list, childSteps) => list.concat(childSteps), [])
const step = allSteps.find(childStep => childStep.location.line === preparedTestCaseStep.sourceLocation.line)
const scenario = doc.feature.children.find(child => child.location.line === sourceLocation.line) || findScenarioByExampleLocation(doc, sourceLocation)
const step = findStepOrExampleByLocation(doc, this.testCasePreparedEvents, testStepFinishedEvent, sourceLocation)
const result = testStepFinishedEvent.result

this.emit('after-step', uri, feature, scenario, step, result)
}

Expand All @@ -174,7 +189,7 @@ export class CucumberEventListener extends EventEmitter {

const doc = this.gherkinDocEvents.find(gde => gde.uri === uri).document
const feature = doc.feature
const scenario = doc.feature.children.find(child => child.location.line === sourceLocation.line)
const scenario = doc.feature.children.find(child => child.location.line === sourceLocation.line) || findScenarioByExampleLocation(doc, sourceLocation)

this.emit('after-scenario', uri, feature, scenario)
}
Expand Down
8 changes: 8 additions & 0 deletions test/fixtures/sample.feature
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,14 @@ Feature: Example feature
Background: Some repeated setup
Given I go on the website "http://webdriver.io"

Scenario Outline: A passing scenario
When I click on link "=<link>"
Then should the title of the page be "Google"
Examples:
| link |
| Google |
| Also Google |

Scenario: Foo Bar
When I click on link "=Google"
Then should the title of the page be "Google"
Expand Down