-
Notifications
You must be signed in to change notification settings - Fork 6
/
index.js
77 lines (64 loc) · 1.95 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
const del = require('del');
const fs = require('fs');
exports.promiseScreenshot = null;
let options = {
page: null,
dirName: null,
scriptName: null,
onlyFailures: false
};
exports.init = async (_options) => {
/* global jasmine */
options = _options;
try { fs.mkdirSync(options.dirName+'/screenshots', { recursive: true }); } catch (error) {}
await del([options.dirName+'/screenshots/*.png']);
jasmine.getEnv().addReporter({
specDone: async function(result) {
if (exports.promiseScreenshot) {
var p = exports.promiseScreenshot;
exports.promiseScreenshot = null;
Promise.all([p]).then(function() {
exports.promiseScreenshot = screenshot(result);
exports.promiseScreenshot.then(function() {
exports.promiseScreenshot = null;
});
});
}
else {
exports.promiseScreenshot = screenshot(result);
exports.promiseScreenshot.then(function() {
exports.promiseScreenshot = null;
});
}
},
});
};
exports.cleanup = function(funcCleanup) {
if (exports.promiseScreenshot) {
exports.promiseScreenshot.then(function() {
funcCleanup();
});
}
else {
funcCleanup();
}
}
async function screenshot(result) {
const specName = result.fullName.replace(/[\s/]/g, '-');
const scriptName = options.scriptName.replace(/[\s/]/g, '-')
return new Promise(function(resolve, reject) {
if (result.status === 'failed') {
options.page.screenshot({path: options.dirName + '/screenshots/0-fail-' + scriptName + '-' + specName + '.png'})
.then(function() { resolve(); })
.catch(function() { resolve(); });
}
else if (options.onlyFailures) {
resolve();
}
else {
options.page.screenshot({path: options.dirName + '/screenshots/' + scriptName + '-' + specName + '.png'})
.then(function() { resolve(); })
.catch(function() { resolve(); });
}
});
}