forked from wix-incubator/eyes.it
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
93 lines (80 loc) · 2.16 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
/* global fit it */
'use strict';
var path = require('path');
var uuid = require('uuid');
var Eyes = require('eyes.protractor').Eyes;
var appName = require(path.join(process.cwd(), 'package.json')).name;
var eyes = new Eyes();
var eyesOpen = false;
var hooked = browser.get;
browser.get = function (address) {
return hooked.apply(this, arguments).then(function (result) {
if (eyesOpen) {
eyes.checkWindow('get ' + address);
}
return result;
});
};
function handleError(err, done) {
fail(err);
done();
}
function isPassedWindowSizeArgument(argumentsObj) {
return typeof argumentsObj[2] === 'object'
}
function eyesWithout(fn) {
return function () {
if (isPassedWindowSizeArgument(arguments)) {
delete arguments[2];
}
return fn.apply(this, arguments);
}
}
function eyesWith(fn) {
return function () {
var windowSize = eyes.defaultWindowSize;
if (isPassedWindowSizeArgument(arguments)) {
windowSize = arguments[2];
delete arguments[2];
}
var spec = fn.apply(this, arguments);
var hooked = spec.beforeAndAfterFns;
spec.beforeAndAfterFns = function () {
var result = hooked.apply(this, arguments);
result.befores.unshift({fn: function (done) {
eyesOpen = true;
eyes.open(browser, appName, 'eyes.it ' + spec.getFullName(), windowSize).then(done);
}, timeout: () => 30000});
result.afters.unshift({fn: function (done) {
eyesOpen = false;
eyes.checkWindow('end');
eyes.close().then(done).catch(err => handleError(err, done));
}, timeout: () => 30000});
return result;
};
return spec;
};
}
function getBatchUUID() {
return process.env.EYES_BATCH_UUID;
}
function setOnceBatchUUID(uuid) {
if (!getBatchUUID()) {
process.env.EYES_BATCH_UUID = uuid;
}
}
function _init() {
setOnceBatchUUID(uuid.v4());
if (process.env.EYES_API_KEY) {
eyes.setApiKey(process.env.EYES_API_KEY);
eyes.it = eyesWith(it);
eyes.fit = eyesWith(fit);
} else {
eyes.it = eyesWithout(it);
eyes.fit = eyesWithout(fit);
}
eyes.defaultWindowSize = null;
eyes.setBatch(appName, getBatchUUID());
}
_init();
module.exports = eyes;