-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
323 lines (281 loc) · 9.96 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
const Allure = require('allure-js-commons');
const { event, output } = require('codeceptjs');
const defaultConfig = {
outputDir: global.output_dir,
};
/**
* Creates an instance of the allure reporter
* @param {Config} [config={ outputDir: global.output_dir }] - Configuration for the allure reporter
* @returns {Object} Instance of the allure reporter
*/
module.exports = (config) => {
defaultConfig.outputDir = global.output_dir;
config = Object.assign(defaultConfig, config);
const plugin = {};
/**
* @type {Allure}
*/
const reporter = new Allure();
reporter.setOptions({ targetDir: config.outputDir });
let currentMetaStep = [];
let currentStep;
/**
* Mark a test case as pending
* @param {string} testName - Name of the test case
* @param {number} timestamp - Timestamp of the test case
* @param {Object} [opts={}] - Options for the test case
*/
reporter.pendingCase = function (testName, timestamp, opts = {}) {
reporter.startCase(testName, timestamp);
plugin.addCommonMetadata();
if (opts.description) plugin.setDescription(opts.description);
if (opts.severity) plugin.severity(opts.severity);
if (opts.severity) plugin.addLabel('tag', opts.severity);
reporter.endCase('pending', { message: opts.message || 'Test ignored' }, timestamp);
};
/**
* Add an attachment to the current test case
* @param {string} name - Name of the attachment
* @param {Buffer} buffer - Buffer of the attachment
* @param {string} type - MIME type of the attachment
*/
plugin.addAttachment = (name, buffer, type) => {
reporter.addAttachment(name, buffer, type);
};
/**
Set description for the current test case
@param {string} description - Description for the test case
@param {string} [type='text/plain'] - MIME type of the description
*/
plugin.setDescription = (description, type) => {
const currentTest = reporter.getCurrentTest();
if (currentTest) {
currentTest.setDescription(description, type);
} else {
logger.error(`The test is not run. Please use "setDescription" for events:
"test.start", "test.before", "test.after", "test.passed", "test.failed", "test.finish"`);
}
};
/**
A method for creating a step in a test case.
@param {string} name - The name of the step.
@param {Function} [stepFunc=() => {}] - The function that should be executed for this step.
@returns {any} - The result of the step function.
*/
plugin.createStep = (name, stepFunc = () => { }) => {
let result;
let status = 'passed';
reporter.startStep(name);
try {
result = stepFunc(this.arguments);
} catch (error) {
status = 'broken';
throw error;
} finally {
if (!!result
&& (typeof result === 'object' || typeof result === 'function')
&& typeof result.then === 'function'
) {
result.then(() => reporter.endStep('passed'), () => reporter.endStep('broken'));
} else {
reporter.endStep(status);
}
}
return result;
};
plugin.createAttachment = (name, content, type) => {
if (typeof content === 'function') {
const attachmentName = name;
const buffer = content.apply(this, arguments);
return createAttachment(attachmentName, buffer, type);
} reporter.addAttachment(name, content, type);
};
plugin.severity = (severity) => {
plugin.addLabel('severity', severity);
};
plugin.epic = (epic) => {
plugin.addLabel('epic', epic);
};
plugin.feature = (feature) => {
plugin.addLabel('feature', feature);
};
plugin.story = (story) => {
plugin.addLabel('story', story);
};
plugin.issue = (issue) => {
plugin.addLabel('issue', issue);
};
/**
Adds a label with the given name and value to the current test in the Allure report
@param {string} name - name of the label to add
@param {string} value - value of the label to add
*/
plugin.addLabel = (name, value) => {
const currentTest = reporter.getCurrentTest();
if (currentTest) {
currentTest.addLabel(name, value);
} else {
logger.error(`The test is not run. Please use "addLabel" for events:
"test.start", "test.before", "test.after", "test.passed", "test.failed", "test.finish"`);
}
};
/**
Adds a parameter with the given kind, name, and value to the current test in the Allure report
@param {string} kind - kind of the parameter to add
@param {string} name - name of the parameter to add
@param {string} value - value of the parameter to add
*/
plugin.addParameter = (kind, name, value) => {
const currentTest = reporter.getCurrentTest();
if (currentTest) {
currentTest.addParameter(kind, name, value);
} else {
logger.error(`The test is not run. Please use "addParameter" for events:
"test.start", "test.before", "test.after", "test.passed", "test.failed", "test.finish"`);
}
};
/**
* Add a special screen diff block to the current test case
* @param {string} name - Name of the screen diff block
* @param {string} expectedImg - string representing the contents of the expected image file encoded in base64
* @param {string} actualImg - string representing the contents of the actual image file encoded in base64
* @param {string} diffImg - string representing the contents of the diff image file encoded in base64.
* Could be generated by image comparison lib like "pixelmatch" or alternative
*/
plugin.addScreenDiff = (name, expectedImg, actualImg, diffImg) => {
const screenDiff = {
name,
expected: `data:image/png;base64,${expectedImg}`,
actual: `data:image/png;base64,${actualImg}`,
diff: `data:image/png;base64,${diffImg}`,
};
reporter.addAttachment(name, JSON.stringify(screenDiff), 'application/vnd.allure.image.diff');
};
plugin.addCommonMetadata = () => {
plugin.addLabel('language', 'javascript');
plugin.addLabel('framework', 'codeceptjs');
};
event.dispatcher.on(event.suite.before, (suite) => {
reporter.startSuite(suite.fullTitle());
});
event.dispatcher.on(event.suite.before, (suite) => {
for (const test of suite.tests) {
if (test.pending) {
reporter.pendingCase(test.title, null, test.opts.skipInfo);
}
}
});
event.dispatcher.on(event.suite.after, () => {
reporter.endSuite();
});
event.dispatcher.on(event.test.before, (test) => {
reporter.startCase(test.title);
plugin.addCommonMetadata();
if (config.enableScreenshotDiffPlugin) {
const currentTest = reporter.getCurrentTest();
currentTest.addLabel('testType', 'screenshotDiff');
}
currentStep = null;
});
event.dispatcher.on(event.test.started, (test) => {
const currentTest = reporter.getCurrentTest();
for (const tag of test.tags) {
currentTest.addLabel('tag', tag);
}
});
event.dispatcher.on(event.test.failed, (test, err) => {
if (currentStep) reporter.endStep('failed');
if (currentMetaStep.length) {
currentMetaStep.forEach(() => reporter.endStep('failed'));
currentMetaStep = [];
}
err.message = err.message.replace(ansiRegExp(), '');
if (reporter.getCurrentTest()) {
reporter.endCase('failed', err);
} else {
// this means before suite failed, we should report this.
reporter.startCase(`BeforeSuite of suite ${reporter.getCurrentSuite().name} failed.`);
plugin.addCommonMetadata();
reporter.endCase('failed', err);
}
});
event.dispatcher.on(event.test.passed, () => {
if (currentStep) reporter.endStep('passed');
if (currentMetaStep.length) {
currentMetaStep.forEach(() => reporter.endStep('passed'));
currentMetaStep = [];
}
reporter.endCase('passed');
});
event.dispatcher.on(event.test.skipped, (test) => {
let loaded = true;
if (test.opts.skipInfo.isFastSkipped) {
loaded = false;
reporter.startSuite(test.parent.fullTitle());
}
reporter.pendingCase(test.title, null, test.opts.skipInfo);
if (!loaded) {
reporter.endSuite();
}
});
event.dispatcher.on(event.step.started, (step) => {
startMetaStep(step.metaStep);
if (currentStep !== step) {
// In multi-session scenarios, actors' names will be highlighted with ANSI
// escape sequences which are invalid XML values
step.actor = step.actor.replace(ansiRegExp(), '');
reporter.startStep(step.toString());
currentStep = step;
}
});
event.dispatcher.on(event.step.comment, (step) => {
reporter.startStep(step.toString());
currentStep = step;
reporter.endStep('passed');
currentStep = null;
});
event.dispatcher.on(event.step.passed, (step) => {
if (currentStep === step) {
reporter.endStep('passed');
currentStep = null;
}
});
event.dispatcher.on(event.step.failed, (step) => {
if (currentStep === step) {
reporter.endStep('failed');
currentStep = null;
}
});
let maxLevel;
function finishMetastep(level) {
const metaStepsToFinish = currentMetaStep.splice(maxLevel - level);
metaStepsToFinish.forEach(() => {
// only if the current step is of type Step, end it.
if (reporter.suites && reporter.suites.length && reporter.suites[0].currentStep && reporter.suites[0].currentStep.constructor.name === 'Step') {
reporter.endStep('passed');
}
});
}
function startMetaStep(metaStep, level = 0) {
maxLevel = level;
if (!metaStep) {
finishMetastep(0);
maxLevel--;
return;
}
startMetaStep(metaStep.metaStep, level + 1);
if (metaStep.toString() !== currentMetaStep[maxLevel - level]) {
finishMetastep(level);
currentMetaStep.push(metaStep.toString());
reporter.startStep(metaStep.toString());
}
}
return plugin;
};
const ansiRegExp = function ({ onlyFirst = false } = {}) {
const pattern = [
'[\\u001B\\u009B][[\\]()#;?]*(?:(?:(?:[a-zA-Z\\d]*(?:;[-a-zA-Z\\d\\/#&.:=?%@~_]*)*)?\\u0007)',
'(?:(?:\\d{1,4}(?:;\\d{0,4})*)?[\\dA-PR-TZcf-ntqry=><~]))',
].join('|');
return new RegExp(pattern, onlyFirst ? undefined : 'g');
};