-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathworker.js
478 lines (448 loc) · 16.3 KB
/
worker.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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
const fs = require('fs')
const path = require('path')
const testHash = require('./testHash')
const testFiles = require('./testFiles')
const shared = require('./shared')
/**
* supplemental data structure
*
* {
* <sessionId#1> : {
* current: <testHash>
* <testHash#1>: {description: <description>, steps: <steps>, files: <files>, _Custom: <custom>}
* <testHash#2>: {description: <description>, steps: <steps>, files: <files>, _Custom: <custom>}
* ...
* },
* <sessionId#2> : {
* current: <testHash>
* <testHash#1>: {description: <description>, steps: <steps>, files: <files>, _Custom: <custom>}
* <testHash#2>: {description: <description>, steps: <steps>, files: <files>, _Custom: <custom>}
* ...
* },
* ...
* }
*/
let supplemental = {}
let startTimes = {}
module.exports = class TesultsWorkerService {
/**
* `serviceOptions` contains all options specific to the service
* e.g. if defined as follows:
*
* ```
* services: [['custom', { foo: 'bar' }]]
* ```
*
* the `serviceOptions` parameter will be: `{ foo: 'bar' }`
*/
constructor (serviceOptions, capabilities, config) {
this.options = serviceOptions
this.cases = []
this.disabled = false
if (this.options.target === undefined) {
this.disabled = true
}
}
testParams (pickle) {
let params = {
"Device/Browser": browser.capabilities.browserName,
}
if (pickle !== undefined) {
params["Example Id"] = pickle.id
}
return params
}
/**
*
* Runs before a Cucumber Scenario.
* @param {ITestCaseHookParameter} world world object containing information on pickle and test step
* @param {Object} context Cucumber World object
*/
beforeScenario (world, context) {
let gherkinDocument = world.gherkinDocument
if (gherkinDocument === undefined) {
gherkinDocument = {}
}
let pickle = world.pickle
if (pickle === undefined) {
pickle = {}
}
let testCase = {suite: gherkinDocument.feature === undefined ? undefined : gherkinDocument.feature.name, name: pickle.name, params: this.testParams(pickle)}
if (supplemental[browser.sessionId] === undefined) {
supplemental[browser.sessionId] = {current: testHash(testCase)}
} else {
let data = supplemental[browser.sessionId]
data.current = testHash(testCase)
supplemental[browser.sessionId] = data
}
startTimes[testHash(testCase)] = Date.now()
}
/**
*
* Runs after a Cucumber Scenario.
* @param {ITestCaseHookParameter} world world object containing information on pickle and test step
* @param {Object} result results object containing scenario results `{passed: boolean, error: string, duration: number}`
* @param {boolean} result.passed true if scenario has passed
* @param {string} result.error error stack if scenario failed
* @param {number} result.duration duration of scenario in milliseconds
* @param {Object} context Cucumber World object
*/
afterScenario (world, result, context) {
if (this.disabled === true) {
return
}
let gherkinDocument = world.gherkinDocument
if (gherkinDocument === undefined) {
gherkinDocument = {}
}
let pickle = world.pickle
if (pickle === undefined) {
pickle = {}
}
let now = Date.now()
let testCase = {
name: pickle.name,
suite: gherkinDocument.feature === undefined ? undefined : gherkinDocument.feature.name,
result: "unknown",
rawResult: world.result.status,
end: now,
params: this.testParams(pickle)
}
if (world.result.status === "PASSED") {
testCase.result = "pass"
}
if (world.result.status === "FAILED") {
testCase.result = "fail"
}
if (result.duration !== undefined) {
try {
const duration = Math.trunc(now - startTimes[testHash(testCase)])
testCase.start = now - duration
testCase.duration = duration
} catch (err) {
// Do not set start and duration in this case
}
}
// Steps
let steps = []
if (pickle.steps !== undefined) {
if (Array.isArray(pickle.steps)) {
for (let i = 0; i < pickle.steps.length; i++) {
let stepRaw = pickle.steps[i]
let step = {
name: stepRaw.keyword,
desc: stepRaw.text,
result: (i === pickle.steps.length - 1) ? testCase.result : "pass"
}
steps.push(step)
}
}
}
if (steps.length > 0) {
testCase.steps = steps
}
testCase["_Device/Browser Version"] = browser.capabilities.browserVersion
let files = testFiles(this.options.files, testCase.suite, testCase.name)
if (files.length > 0) {
testCase.files = files
}
if (result.passed !== true) {
testCase.reason = result.error
}
// Supplemental fields
if (supplemental !== undefined) {
if (supplemental[browser.sessionId] !== undefined) {
let data = supplemental[browser.sessionId][testHash(testCase)]
if (data !== undefined) {
if (data.desc !== undefined) {
testCase.desc = data.desc
}
if (data.steps !== undefined) {
testCase.steps = data.steps
}
if (data.files !== undefined) {
testCase.files = data.files
}
Object.keys(data).forEach((key) => {
if (key.startsWith("_")) {
testCase[key] = data[key]
}
})
}
}
}
supplemental[browser.sessionId].current = undefined
this.cases.push(testCase)
}
/**
* Function to be executed before a test (in Mocha/Jasmine only)
* @param {Object} test test object
* @param {Object} context scope object the test was executed with
*/
beforeTest (test, context) {
let testCase = {suite: test.parent, name: test.title, params: this.testParams()}
if (test.title === undefined && test.parent === undefined
&& test.description !== undefined && test.fullName !== undefined) { // Jasmine
testCase.name = test.description
testCase.suite = test.fullName.replace(test.description, "").trim()
}
if (supplemental[browser.sessionId] === undefined) {
supplemental[browser.sessionId] = {current: testHash(testCase)}
} else {
let data = supplemental[browser.sessionId]
data.current = testHash(testCase)
supplemental[browser.sessionId] = data
}
}
/**
* Function to be executed after a test (in Mocha/Jasmine only)
* @param {Object} test test object
* @param {Object} context scope object the test was executed with
* @param {Error} result.error error object in case the test fails, otherwise `undefined`
* @param {Any} result.result return object of test function
* @param {Number} result.duration duration of test
* @param {Boolean} result.passed true if test has passed, otherwise false
* @param {Object} result.retries informations to spec related retries, e.g. `{ attempts: 0, limit: 0 }`
*/
afterTest (test, context, { error, result, duration, passed, retries }) {
if (this.disabled === true) {
return
}
if (error !== undefined) {
try {
error = {
name: error.name,
message: error.message,
stack: error.stack
}
} catch (err) {
// Swallow
}
}
let now = Date.now()
let testCase = {
name: test.title,
suite: test.parent,
result: passed ? "pass" : "fail",
reason: passed ? undefined : error,
start: now - duration,
end: now,
duration: duration,
_cid: test.cid,
_uid: test.uid,
_type: test.type,
_returned: result,
params: this.testParams()
}
if (test.title === undefined && test.parent === undefined
&& test.description !== undefined && test.fullName !== undefined) { // Jasmine
testCase.name = test.description
testCase.suite = test.fullName.replace(test.description, "").trim()
}
if (test.failedExpectations !== undefined) { // Jasmine
if (Array.isArray(test.failedExpectations)) {
if (test.failedExpectations.length > 0) {
testCase.result = "fail"
testCase.reason = test.failedExpectations[0]
}
}
}
testCase["_Device/Browser Version"] = browser.capabilities.browserVersion
let files = testFiles(this.options.files, test.parent, test.title)
if (files.length > 0) {
testCase.files = files
}
if (passed !== true) {
if (error !== undefined && test.title !== undefined) { // Mocha only
testCase.reason = error
}
}
if (test.data !== undefined) {
testCase["_wdio_data"] = test.data
}
// Supplemental fields
if (supplemental !== undefined) {
if (supplemental[browser.sessionId] !== undefined) {
let data = supplemental[browser.sessionId][testHash(testCase)]
if (data !== undefined) {
if (data.desc !== undefined) {
testCase.desc = data.desc
}
if (data.steps !== undefined) {
testCase.steps = data.steps
}
if (data.files !== undefined) {
testCase.files = data.files
}
Object.keys(data).forEach((key) => {
if (key.startsWith("_")) {
testCase[key] = data[key]
}
})
}
}
}
supplemental[browser.sessionId].current = undefined
this.cases.push(testCase)
}
/**
* Gets executed after all tests are done. You still have access to all global variables from
* the test.
* @param {Number} result 0 - test pass, 1 - test fail
* @param {Array.<Object>} capabilities list of capabilities details
* @param {Array.<String>} specs List of spec file paths that ran
*/
after (result, capabilities, specs) {
if (this.disabled === true) {
return
}
try {
let fileContents = JSON.stringify(this.cases)
fs.writeFileSync(path.join(shared.temp, browser.sessionId + ".json"), fileContents)
} catch (err) {
console.log("wdio-tesults-service error saving test cases: " + err)
}
}
afterSession (config, capabilities, specs) {
if (this.disabled === true) {
return
}
try {
let fileContents = JSON.stringify(this.cases)
fs.writeFileSync(path.join(shared.temp, browser.sessionId + ".json"), fileContents)
} catch (err) {
console.log("wdio-tesults-service error saving test cases: " + err)
}
}
// Supplemental reporter functions (description, custom, step and file)
/**
* Gets supplemental data for the current test
* @returns supplementalData
*/
static getSupplementalData () {
if (supplemental[browser.sessionId] === undefined) {
return undefined
}
let testCaseHash = supplemental[browser.sessionId].current
if (testCaseHash === undefined) {
return undefined
}
return supplemental[browser.sessionId][testCaseHash]
}
/**
* Sets supplemental data for the current test
* @param {Any} val the new supplemental data
* @returns void
*/
static setSupplementalData (val) {
if (supplemental[browser.sessionId] === undefined) {
return undefined
}
let testCaseHash = supplemental[browser.sessionId].current
if (testCaseHash === undefined) {
return undefined
}
supplemental[browser.sessionId][testCaseHash] = val
}
/**
* Set description for test case
* @param {String} val the description
* @returns void
*/
static description(val) {
if (val === undefined) {
return
}
let data = this.getSupplementalData()
if (data === undefined) {
this.setSupplementalData({desc: val})
} else {
data.desc = val
this.setSupplementalData(data)
}
}
/**
* Set a custom field for test case
* @param {String} key the name of the custom field
* @param {Any} val the value for the custom field
* @returns void
*/
static custom (key, val) {
if (key === undefined || val === undefined) {
return
}
let data = this.getSupplementalData()
let newData = {}
if (data !== undefined) {
newData = data
}
newData["_" + key] = val
this.setSupplementalData(newData)
}
/**
* Set a step for test case
* @param {Object} step step object consisting of a name and result (pass|fail|unknown)
* and optional description and reason (for failure) properties
* @returns void
*/
static step (step) {
if (step === undefined) {
return
}
if (step.description !== undefined) {
step.desc = step.description
delete step.description
}
let data = this.getSupplementalData()
let newData = {}
if (data === undefined) {
newData = {steps: [step]}
} else {
newData = data
let steps = newData.steps
if (steps === undefined) {
newData.steps = [step]
} else {
// deduplication start
// Removed due to user feedback - repeated steps should be permitted
// Note that removing deduplication will mean that on retries, steps will be repeated in output
/*let newDataStepsIndex = {}
for (let i = 0; i < newData.steps.length; i++) {
let newDataStep = newData.steps[i]
newDataStepsIndex[newDataStep.name] = i
}
if (newDataStepsIndex[step.name] !== undefined) {
newData.steps.splice(newDataStepsIndex[step.name])
}*/
// deduplication end
newData.steps.push(step)
}
}
this.setSupplementalData(newData)
}
/**
* Associate a file to test case
* @param {String} file absolute path to a file to associate to the test case
* @returns void
*/
static file (file) {
if (file === undefined) {
return
}
let data = this.getSupplementalData()
let newData = {}
if (data === undefined) {
newData = {files: [file]}
} else {
newData = data
let files = newData.files
if (files === undefined) {
newData.files = [file]
} else {
newData.files.push(file)
}
}
newData.files = [...new Set(newData.files)]; // deduplication
this.setSupplementalData(newData)
}
}