forked from kobenguyent/codeceptjs-rphelper
-
Notifications
You must be signed in to change notification settings - Fork 17
/
index.js
395 lines (326 loc) · 10.5 KB
/
index.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
const RPClient = require('@reportportal/client-javascript');
const fs = require('fs');
const path = require('path');
const debug = require('debug')('codeceptjs:reportportal');
const { event, recorder, output, container } = codeceptjs;
const deepClone = require('lodash.clonedeep');
const helpers = container.helpers();
let helper;
const rp_FAILED = 'FAILED';
const rp_PASSED = 'PASSED';
const rp_SUITE = 'SUITE';
const rp_TEST = 'TEST';
const rp_STEP = 'STEP';
const screenshotHelpers = [
'WebDriver',
'Protractor',
'Appium',
'Nightmare',
'Puppeteer',
'TestCafe',
'Playwright',
];
for (const helperName of screenshotHelpers) {
if (Object.keys(helpers).indexOf(helperName) > -1) {
helper = helpers[helperName];
}
}
const defaultConfig = {
token: '',
endpoint: '',
projectName: '',
launchName: 'codeceptjs tests',
launchDescription: '',
attributes: [],
debug: false,
rerun: undefined,
enabled: false,
mode: 'DEFAULT'
};
const requiredFields = ['projectName', 'token', 'endpoint'];
module.exports = (config) => {
config = Object.assign(defaultConfig, config);
for (let field of requiredFields) {
if (!config[field]) throw new Error(`ReportPortal config is invalid. Key ${field} is missing in config.\nRequired fields: ${requiredFields} `)
}
let reportUrl;
let launchObj;
let suiteObj;
let testObj;
let stepObj;
let failedStep;
let rpClient;
let suiteStatus = rp_PASSED;
let launchStatus = rp_PASSED;
let currentMetaSteps = [];
function logCurrent(data, file) {
const obj = stepObj || testObj;
if (obj) rpClient.sendLog(obj.tempId, data, file);
}
event.dispatcher.on(event.all.before, async () => {
launchObj = startLaunch();
try {
await launchObj.promise;
} catch (err) {
output.error(`❌ Can't connect to ReportPortal, exiting...`);
output.error(err);
process.exit(1);
}
output.print(`📋 Writing results to ReportPortal: ${config.projectName} > ${config.endpoint}`);
const outputLog = output.log;
const outputDebug = output.debug;
const outputError = output.error;
output.log = (message) => {
outputLog(message);
logCurrent({ level: 'trace', message });
}
output.debug = (message) => {
outputDebug(message);
logCurrent({ level: 'debug', message });
}
output.error = (message) => {
outputError(message);
logCurrent({ level: 'error', message });
}
});
event.dispatcher.on(event.suite.before, (suite) => {
recorder.add(async () => {
suiteObj = startTestItem(suite.title, rp_TEST);
debug(`${suiteObj.tempId}: The suiteId '${suite.title}' is started.`);
suite.tempId = suiteObj.tempId;
suiteStatus = rp_PASSED;
});
});
event.dispatcher.on(event.test.before, (test) => {
recorder.add(async () => {
if (test._retries > 0) {
if (test._currentRetry > 0) {
test.retriedTitle = test.title + ' [⟳' + test._currentRetry + ']'
}
}
currentMetaSteps = [];
stepObj = null;
testObj = startTestItem(test.retriedTitle || test.title, rp_STEP, suiteObj.tempId, true);
test.tempId = testObj.tempId;
failedStep = null;
debug(`${testObj.tempId}: The testId '${test.title}' is started.`);
})
});
event.dispatcher.on(event.step.before, (step) => {
recorder.add(async () => {
const parent = await startMetaSteps(step);
stepObj = startTestItem(step.toString().slice(0, 300), rp_STEP, parent.tempId);
step.tempId = stepObj.tempId;
})
});
event.dispatcher.on(event.step.after, (step) => {
recorder.add(() => finishStep(step));
});
event.dispatcher.on(event.step.failed, (step) => {
for (const metaStep of currentMetaSteps) {
if (metaStep) metaStep.status = 'failed';
}
if (step && step.tempId) failedStep = Object.assign({}, step);
});
event.dispatcher.on(event.step.passed, (step, err) => {
for (const metaStep of currentMetaSteps) {
metaStep.status = 'passed';
}
failedStep = null;
});
event.dispatcher.on(event.test.failed, async (test, err) => {
launchStatus = rp_FAILED;
suiteStatus = rp_FAILED;
let retriedTempId;
if (test.ctx && test.ctx.test && test.ctx.test.retriedTitle && test.ctx.test._currentRetry > 0 ) {
debug(`Retried run of test`);
retriedTempId = test.ctx.test.tempId
}
if (failedStep && failedStep.tempId) {
const step = failedStep;
debug(`Attaching screenshot & error to failed step`);
const screenshot = await attachScreenshot();
resp = await rpClient.sendLog(step.tempId, {
level: 'ERROR',
message: `${err.stack}`,
time: step.startTime,
}, screenshot).promise;
}
if (!test.tempId) return;
const testTempId = retriedTempId ? retriedTempId : test.tempId
debug(`${testTempId}: Test '${test.title}' failed.`);
if (!failedStep) {
await rpClient.sendLog(testTempId, {
level: 'ERROR',
message: `${err.stack}`,
}).promise;
}
rpClient.finishTestItem(testTempId, {
endTime: test.endTime || rpClient.helpers.now(),
status: rp_FAILED,
message: `${err.stack}`,
});
});
event.dispatcher.on(event.test.passed, (test, err) => {
debug(`${test.tempId}: Test '${test.title}' passed.`);
rpClient.finishTestItem(test.tempId, {
endTime: test.endTime || rpClient.helpers.now(),
status: rp_PASSED,
});
});
event.dispatcher.on(event.test.after, (test) => {
recorder.add(async () => {
debug(`closing ${currentMetaSteps.length} metasteps for failed test`);
if (failedStep) await finishStep(failedStep);
await Promise.all(currentMetaSteps.reverse().map(m => finishStep(m)));
stepObj = null;
testObj = null;
});
});
event.dispatcher.on(event.suite.after, (suite) => {
recorder.add(async () => {
debug(`${suite.tempId}: Suite '${suite.title}' finished ${suiteStatus}.`);
return rpClient.finishTestItem(suite.tempId, {
endTime: suite.endTime || rpClient.helpers.now(),
status: rpStatus(suiteStatus)
});
});
});
function startTestItem(testTitle, method, parentId = null, stats = null) {
try {
const hasStats = stats || (method !== rp_STEP);
return rpClient.startTestItem({
name: testTitle,
type: method,
hasStats,
}, launchObj.tempId, parentId);
} catch (error) {
output.err(error);
}
}
event.dispatcher.on(event.all.result, async () => {
// await recorder.promise;
debug('Finishing launch...');
if (suiteObj) {
rpClient.finishTestItem(suiteObj.tempId, {
status: suiteStatus,
}).promise;
}
if (!process.env.RP_LAUNCH_ID) await finishLaunch();
});
function startLaunch(suiteTitle) {
rpClient = new RPClient({
token: config.token,
endpoint: config.endpoint,
project: config.projectName,
debug: config.debug,
});
const options = {
name: config.launchName || suiteTitle,
description: config.launchDescription,
attributes: config.launchAttributes,
rerun: config.rerun,
rerunOf: config.rerunOf,
mode: config.mode,
}
if (process.env.RP_LAUNCH_ID) {
options.id = process.env.RP_LAUNCH_ID
}
return rpClient.startLaunch(options);
}
async function attachScreenshot() {
if (!helper) return undefined;
const fileName = `${rpClient.helpers.now()}.png`;
try {
await helper.saveScreenshot(fileName);
} catch (err) {
output.error(`Couldn't save screenshot`);
return undefined;
}
const content = fs.readFileSync(path.join(global.output_dir, fileName));
fs.unlinkSync(path.join(global.output_dir, fileName));
return {
name: 'failed.png',
type: 'image/png',
content,
}
}
async function finishLaunch() {
try {
debug(`${launchObj.tempId} Finished launch: ${launchStatus}`)
const launch = rpClient.finishLaunch(launchObj.tempId, {
status: launchStatus,
});
const response = await launch.promise;
reportUrl = response.link;
output.print(` 📋 Report #${response.number} saved ➡`, response.link);
event.emit('reportportal.result', response);
} catch (error) {
console.log(error);
debug(error);
}
}
async function startMetaSteps(step) {
let metaStepObj = {};
const metaSteps = metaStepsToArray(step.metaStep);
// close current metasteps
for (let j = currentMetaSteps.length-1; j >= metaSteps.length; j--) {
await finishStep(currentMetaSteps[j]);
}
for (const i in metaSteps) {
const metaStep = metaSteps[i];
if (isEqualMetaStep(metaStep, currentMetaSteps[i])) {
metaStep.tempId = currentMetaSteps[i].tempId;
continue;
}
// close metasteps other than current
for (let j = currentMetaSteps.length-1; j >= i; j--) {
await finishStep(currentMetaSteps[j]);
delete currentMetaSteps[j];
}
metaStepObj = currentMetaSteps[i-1] || metaStepObj;
const isNested = !!metaStepObj.tempId;
metaStepObj = startTestItem(metaStep.toString(), rp_STEP , metaStepObj.tempId || testObj.tempId, false);
metaStep.tempId = metaStepObj.tempId;
debug(`${metaStep.tempId}: The stepId '${metaStep.toString()}' is started. Nested: ${isNested}`);
}
currentMetaSteps = deepClone(metaSteps);
return currentMetaSteps[currentMetaSteps.length - 1] || testObj;
}
function finishStep(step) {
if (!step) return;
if (!step.tempId) {
debug(`WARNING: '${step.toString()}' step can't be closed, it has no tempId`);
return;
}
debug(`Finishing '${step.toString()}' step`);
return rpClient.finishTestItem(step.tempId, {
endTime: rpClient.helpers.now(),
status: rpStatus(step.status),
});
}
return {
addLog: logCurrent,
};
};
function metaStepsToArray(step) {
let metaSteps = [];
iterateMetaSteps(step, metaStep => metaSteps.push(metaStep));
return metaSteps;
}
function iterateMetaSteps(step, fn) {
if (step && step.metaStep) iterateMetaSteps(step.metaStep, fn);
if (step) fn(step);
}
const isEqualMetaStep = (metastep1, metastep2) => {
if (!metastep1 && !metastep2) return true;
if (!metastep1 || !metastep2) return false;
return metastep1.actor === metastep2.actor
&& metastep1.name === metastep2.name
&& metastep1.args.join(',') === metastep2.args.join(',');
};
function rpStatus(status) {
if (status === 'success') return rp_PASSED;
if (status === 'failed') return rp_FAILED;
return status;
}