-
Notifications
You must be signed in to change notification settings - Fork 1
/
eyes.js
75 lines (63 loc) · 2.04 KB
/
eyes.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
const path = require('path');
const uuid = require('uuid');
const {Eyes} = require('@applitools/eyes-images');
const {
EYES_API_KEY,
EYES_BATCH_UUID,
EYES_PLATFORM,
EYES_API_SERVER_URL,
WITH_EYES_TIMEOUT
} = process.env;
const eyes = {
DEFAULT_TIMEOUT: parseInt(WITH_EYES_TIMEOUT, 10) || 30000,
checkImage: async () => false,
// eslint-disable-next-line no-unused-vars
setMatchTimeout: timeout => false,
// eslint-disable-next-line object-shorthand, no-unused-vars
openEyes: function (fn, test, options) {
return fn.call(this);
},
abortIfNotClosed: async () => Promise.resolve()
};
let addBaselineName = false;
let fixedViewPort = true;
function useFixedViewPort(value) {
fixedViewPort = !!value;
}
function useBaselineName(value) {
addBaselineName = !!value;
}
if (EYES_API_KEY) {
const {name} = require(path.join(process.cwd(), 'package.json'));
const instance = new Eyes();
if (EYES_PLATFORM || process.platform) {
instance.setHostOS(EYES_PLATFORM || process.platform);
}
instance.setApiKey(EYES_API_KEY);
instance.setBatch(name, EYES_BATCH_UUID || uuid.v4());
if (EYES_API_SERVER_URL) {
instance.setServerUrl(EYES_API_SERVER_URL);
}
eyes.checkImage = instance.checkImage.bind(instance);
eyes.setMatchTimeout = instance.setMatchTimeout.bind(instance);
eyes.abortIfNotClosed = instance.abortIfNotClosed.bind(instance);
eyes.openEyes = async function (fn, test, options) {
if (addBaselineName) {
instance.setBaselineEnvName(test);
}
const version = (options || {}).version || '1.0.0';
const throwOnFail = options ? options.throwOnFail : true;
try {
const viewport = fixedViewPort ? {width: 100, height: 100} : undefined;
await instance.open(name, `${test}, ${version}`, viewport);
await fn.call(this);
} catch (error) {
await instance.abortIfNotClosed();
throw error;
}
await instance.close(throwOnFail);
};
}
module.exports.eyes = eyes;
module.exports.useBaselineName = useBaselineName;
module.exports.useFixedViewPort = useFixedViewPort;