/
test_case_runner.js
202 lines (187 loc) · 5.63 KB
/
test_case_runner.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
import _ from 'lodash'
import { getAmbiguousStepException } from './helpers'
import AttachmentManager from './attachment_manager'
import Promise from 'bluebird'
import Status from '../status'
import StepRunner from './step_runner'
export default class TestCaseRunner {
constructor({
eventBroadcaster,
skip,
testCase,
supportCodeLibrary,
worldParameters
}) {
const attachmentManager = new AttachmentManager(({ data, media }) => {
this.emit('test-step-attachment', {
index: this.testStepIndex,
data,
media
})
})
this.eventBroadcaster = eventBroadcaster
this.skip = skip
this.testCase = testCase
this.supportCodeLibrary = supportCodeLibrary
this.world = new supportCodeLibrary.World({
attach: ::attachmentManager.create,
parameters: worldParameters
})
this.beforeHookDefinitions = this.getBeforeHookDefinitions()
this.afterHookDefinitions = this.getAfterHookDefinitions()
this.testStepIndex = 0
this.result = {
duration: 0,
status: this.skip ? Status.SKIPPED : Status.PASSED
}
}
emit(name, data) {
let sourceLocation = {
uri: this.testCase.uri,
line: this.testCase.pickle.locations[0].line
}
let eventData = { ...data }
if (_.startsWith(name, 'test-case')) {
eventData.sourceLocation = sourceLocation
} else {
eventData.testCase = { sourceLocation }
}
this.eventBroadcaster.emit(name, eventData)
}
emitPrepared() {
const steps = []
this.beforeHookDefinitions.forEach(definition => {
const actionLocation = { uri: definition.uri, line: definition.line }
steps.push({ actionLocation })
})
this.testCase.pickle.steps.forEach(step => {
const actionLocations = this.getStepDefinitions(step).map(definition => {
return { uri: definition.uri, line: definition.line }
})
const sourceLocation = {
uri: this.testCase.uri,
line: _.last(step.locations).line
}
const data = { sourceLocation }
if (actionLocations.length === 1) {
data.actionLocation = actionLocations[0]
}
steps.push(data)
})
this.afterHookDefinitions.forEach(definition => {
const actionLocation = { uri: definition.uri, line: definition.line }
steps.push({ actionLocation })
})
this.emit('test-case-prepared', { steps })
}
getAfterHookDefinitions() {
return this.supportCodeLibrary.afterTestCaseHookDefinitions.filter(
hookDefinition => {
return hookDefinition.appliesToTestCase(this.testCase)
}
)
}
getBeforeHookDefinitions() {
return this.supportCodeLibrary.beforeTestCaseHookDefinitions.filter(
hookDefinition => {
return hookDefinition.appliesToTestCase(this.testCase)
}
)
}
getStepDefinitions(step) {
return this.supportCodeLibrary.stepDefinitions.filter(stepDefinition => {
return stepDefinition.matchesStepName({
stepName: step.text,
parameterTypeRegistry: this.supportCodeLibrary.parameterTypeRegistry
})
})
}
invokeStep(step, stepDefinition) {
return StepRunner.run({
defaultTimeout: this.supportCodeLibrary.defaultTimeout,
scenarioResult: this.scenarioResult,
step,
stepDefinition,
parameterTypeRegistry: this.supportCodeLibrary.parameterTypeRegistry,
world: this.world
})
}
isSkippingSteps() {
return this.result.status !== Status.PASSED
}
shouldUpdateStatus(testStepResult) {
switch (testStepResult.status) {
case Status.FAILED:
case Status.AMBIGUOUS:
return (
this.result.status !== Status.FAILED ||
this.result.status !== Status.AMBIGUOUS
)
default:
return (
this.result.status === Status.PASSED ||
this.result.status === Status.SKIPPED
)
}
}
async aroundTestStep(runStepFn) {
this.emit('test-step-started', { index: this.testStepIndex })
const testStepResult = await runStepFn()
if (testStepResult.duration) {
this.result.duration += testStepResult.duration
}
if (this.shouldUpdateStatus(testStepResult)) {
this.result.status = testStepResult.status
}
this.emit('test-step-finished', {
index: this.testStepIndex,
result: testStepResult
})
this.testStepIndex += 1
}
async run() {
this.emitPrepared()
this.emit('test-case-started', {})
await this.runHooks(this.beforeHookDefinitions)
await this.runSteps()
await this.runHooks(this.afterHookDefinitions)
this.emit('test-case-finished', { result: this.result })
return this.result
}
async runHook(hookDefinition) {
if (this.skip) {
return { status: Status.SKIPPED }
} else {
return await this.invokeStep(null, hookDefinition)
}
}
async runHooks(hookDefinitions) {
await Promise.each(hookDefinitions, async hookDefinition => {
await this.aroundTestStep(() => {
return this.runHook(hookDefinition)
})
})
}
async runStep(step) {
const stepDefinitions = this.getStepDefinitions(step)
if (stepDefinitions.length === 0) {
return { status: Status.UNDEFINED }
} else if (stepDefinitions.length > 1) {
return {
exception: getAmbiguousStepException(stepDefinitions),
status: Status.AMBIGUOUS
}
} else if (this.isSkippingSteps()) {
return { status: Status.SKIPPED }
} else {
return await this.invokeStep(step, stepDefinitions[0])
}
}
async runSteps() {
await Promise.each(this.testCase.pickle.steps, async step => {
await this.aroundTestStep(() => {
return this.runStep(step)
})
})
}
}