-
Notifications
You must be signed in to change notification settings - Fork 452
/
config.js
139 lines (127 loc) · 4.27 KB
/
config.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
const defaultConfig = {
navigationTimeout: Number(process.env.TAIKO_NAVIGATION_TIMEOUT) || 30000, //Millisecond
observeTime: 3000, //Millisecond
retryInterval: 100, //Millisecond
retryTimeout: Number(process.env.TAIKO_RETRY_TIMEOUT) || 10000, //Millisecond
noOfElementToMatch: 20,
observe: false,
waitForNavigation: true,
waitForEvents: [],
ignoreSSLErrors: true,
headful: false,
local: process.env.LOCAL_PROTOCOL || false,
criConnectionRetries: process.env.TAIKO_CRI_CONNECTION_RETRIES || 50,
firefox:
(process.env.TAIKO_BROWSER_PATH &&
process.env.TAIKO_BROWSER_PATH.toLowerCase().includes('firefox')) ||
false,
highlightOnAction:
process.env.TAIKO_BROWSER_PATH &&
process.env.TAIKO_BROWSER_PATH.toLowerCase().includes('firefox')
? 'false'
: process.env.TAIKO_HIGHLIGHT_ON_ACTION || 'true',
blockAlignment: 'nearest',
inlineAlignment: 'nearest',
};
const setConfig = (options) => {
for (const key in options) {
if (Object.prototype.hasOwnProperty.call(defaultConfig, key)) {
if (typeof defaultConfig[key] !== typeof options[key]) {
throw new Error(
`Invalid value for ${key}. Expected ${typeof defaultConfig[
key
]} received ${typeof options[key]}`,
);
}
defaultConfig[key] = options[key];
} else {
throw new Error(
`Invalid config ${key}. Allowed configs are ${Object.keys(defaultConfig).join(', ')}`,
);
}
}
};
const getConfig = (optionName) => {
if (optionName) {
if (Object.prototype.hasOwnProperty.call(defaultConfig, optionName)) {
return defaultConfig[optionName];
} else {
throw new Error(
`Invalid config ${optionName}. Allowed configs are ${Object.keys(defaultConfig).join(
', ',
)}`,
);
}
} else {
return Object.assign({}, defaultConfig);
}
};
const setBrowserOptions = (options) => {
options.port = options.port || 0;
options.host = options.host || '127.0.0.1';
options.headless =
options.headless === undefined || options.headless === null ? true : options.headless;
const observe = _determineValueFor('observe', options.observe);
setConfig({
observeTime: determineObserveDelay(observe, options.observeTime),
observe: observe,
ignoreSSLErrors: _determineValueFor('ignoreSSLErrors', options.ignoreCertificateErrors),
headful: !options.headless,
});
return options;
};
const setNavigationOptions = (options) => {
options.waitForNavigation = determineWaitForNavigation(options.waitForNavigation);
options.waitForEvents = options.waitForEvents || defaultConfig.waitForEvents;
options.navigationTimeout = options.navigationTimeout || defaultConfig.navigationTimeout;
options.waitForStart = options.waitForStart || 100;
return options;
};
const setClickOptions = (options, x, y) => {
options = setNavigationOptions(options);
options.x = x;
options.y = y;
options.button = options.button || 'left';
options.clickCount = options.clickCount || 1;
options.elementsToMatch = options.elementsToMatch || 10;
return options;
};
const setScrollOptions = (options) => {
options.blockAlignment = options.blockAlignment || defaultConfig.blockAlignment;
options.inlineAlignment = options.inlineAlignment ||defaultConfig.inlineAlignment;
return options;
};
const determineObserveDelay = (shouldObserve, observeTime) => {
if (shouldObserve) {
return _determineValueFor('observeTime', observeTime);
} else {
return observeTime || 0;
}
};
const determineWaitForNavigation = (waitForNavigation) => {
return _determineValueFor('waitForNavigation', waitForNavigation);
};
const determineRetryInterval = (retryInterval) => {
return _determineValueFor('retryInterval', retryInterval);
};
const determineRetryTimeout = (retryTimeout) => {
return _determineValueFor('retryTimeout', retryTimeout);
};
const _determineValueFor = (configName, providedValue) => {
return _hasValue(providedValue) ? providedValue : defaultConfig[configName];
};
const _hasValue = (value) => {
return !(value === undefined || value === null);
};
module.exports = {
defaultConfig,
setConfig,
getConfig,
determineWaitForNavigation,
determineRetryInterval,
determineRetryTimeout,
setNavigationOptions,
setClickOptions,
setBrowserOptions,
setScrollOptions,
};