This repository was archived by the owner on Sep 21, 2022. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathconfig.js
More file actions
231 lines (202 loc) Β· 8.39 KB
/
Copy pathconfig.js
File metadata and controls
231 lines (202 loc) Β· 8.39 KB
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
'use strict';
var url = require('url'),
path = require('path'),
fs = require('q-io/fs'),
yaml = require('js-yaml'),
chalk = require('chalk'),
extend = require('node.extend'),
inherit = require('inherit'),
GeminiError = require('./errors/gemini-error'),
DEFAULT_SCREENSHOTS_DIR = 'gemini/screens';
var Config = module.exports = inherit({
__constructor: function(configPath, configText, overrides) {
overrides = overrides || {};
var config;
try {
config = yaml.load(configText);
} catch (e) {
throw new GeminiError('Error while parsing a config file: ' + configPath + '\n' +
e.reason + ' ' + e.mark,
'Gemini config should be valid YAML file.'
);
}
this.root = path.dirname(configPath);
this.rootUrl = overrides.rootUrl || config.rootUrl;
if (!this.rootUrl) {
throw new GeminiError(
'Required field "rootUrl" is not specified in config file: ' + configPath,
'"rootUrl" should point to the root of website under test.\nSuite URLs are resolved relative to it.'
);
}
this.gridUrl = overrides.gridUrl || config.gridUrl;
this.browsers = this._parseBrowsers(config.browsers || {phantomjs: 'phantomjs'});
this.capabilities = config.capabilities || {};
this._validateCapabilities();
this.http = this._parseHttp(config.http || {});
if ('debug' in config && typeof config.debug !== 'boolean') {
throw new GeminiError('Field "debug" must contain a boolean value');
}
this.debug = !!config.debug;
if ('parallelLimit' in config) {
if (typeof config.parallelLimit !== 'number') {
throw new GeminiError('Field "parallelLimit" must be an integer number');
}
if (config.parallelLimit < 0) {
throw new GeminiError('Field "parallelLimit" must be non-negative');
}
if (Math.floor(config.parallelLimit) !== config.parallelLimit) {
throw new GeminiError('Field "parallelLimit" must be an integer number');
}
this.parallelLimit = config.parallelLimit;
}
if (this._requiresGrid() && !this.gridUrl) {
throw new GeminiError(
'Field "gridUrl" is required for using non-phantomjs browsers',
[
'Specify selenium grid URL in your config file or use only',
'phantomjs browser.',
'Selenium server installation instructions:',
'',
'https://code.google.com/p/selenium/wiki/Grid2'
].join('\n')
);
}
if ('tolerance' in config) {
if (typeof config.tolerance !== 'number') {
throw new GeminiError('Field "tolerance" must be a number');
}
if (config.tolerance > 1 || config.tolerance < 0) {
throw new GeminiError('Field "tolerance" must be between 0 and 1');
}
this.tolerance = config.tolerance;
} else {
this.tolerance = Number.MIN_VALUE;
}
if ('diffColor' in config) {
if (typeof config.diffColor !== 'string') {
throw new GeminiError('Field "diffColor" must be string');
}
if (!/^#[0-9a-fA-F]{6}$/.test(config.diffColor)) {
throw new GeminiError('Field "diffColor" must be hexadecimal color string (i.e. #ff0000)');
}
this.diffColor = config.diffColor;
} else {
this.diffColor = '#ff00ff';
}
this.screenshotsDir = path.resolve(this.root,
config.screenshotsDir || DEFAULT_SCREENSHOTS_DIR);
},
_requiresGrid: function() {
return Object.keys(this.browsers).some(function(name) {
return this.browsers[name].browserName !== 'phantomjs';
}, this);
},
_validateCapabilities: function() {
if ('takesScreenshot' in this.capabilities) {
throw new GeminiError(
'Setting `takesScreenshot` capability for all browsers is not allowed.\n' +
'It is required and will be set automatically.'
);
}
},
_parseBrowsers: function(browsers) {
var parsedBrowsers = {};
// legacy browsers config
if (Array.isArray(browsers)) {
// Convert legacy browsers config value to the current one
parsedBrowsers = this._parseLegacyBrowsers(browsers);
// legacy browsers config option warning
console.error([
chalk.yellow('Warning! You are using deprecated format of "browsers" option'),
chalk.yellow('in gemini config. Replace it\'s value with the following code:'),
'', yaml.safeDump({browsers: parsedBrowsers}),
chalk.yellow('For more info on project configuration see the instructions:'),
chalk.yellow('https://github.com/bem/gemini#configuration')
].join('\n'));
return parsedBrowsers;
} else if (browsers !== null && typeof browsers === 'object') {
// Convert `browser-id: browser-name` to `browser-id: {browserName: browser-name}`
Object.keys(browsers).forEach(function(id) {
if (typeof browsers[id] === 'string') {
parsedBrowsers[id] = {browserName: browsers[id]};
} else {
parsedBrowsers[id] = browsers[id];
}
}, this);
return parsedBrowsers;
}
throw new GeminiError('Field "browsers" must be an object or an array (deprecated).',
'Configure your project by following the instructions:\n\n',
'https://github.com/bem/gemini#configuration');
},
_parseLegacyBrowsers: function(browsers) {
var parsedBrowsers = {};
browsers
.map(function(browser) {
if (typeof browser === 'string') {
return {browserName: browser};
}
if ('name' in browser) {
browser = extend(true, {}, browser);
browser.browserName = browser.name;
delete browser.name;
}
return browser;
})
.forEach(function(browser) {
var id = browser.browserName;
if (browser.version) {
id += '-v' + browser.version;
}
parsedBrowsers[id] = browser;
});
return parsedBrowsers;
},
_parseHttp: function(http) {
var conf = {};
['timeout', 'retries', 'retryDelay']
.forEach(function(option) {
if (option in http) {
if (typeof http[option] !== 'number') {
throw new GeminiError('Field "http.' + option + '" must be a number');
}
conf[option] = http[option];
}
});
return conf;
},
getAbsoluteUrl: function getAbsoluteUrl(relUrl) {
return url.resolve(this.rootUrl, relUrl);
},
getScreenshotsDir: function(suite, state) {
return path.resolve(this.screenshotsDir, this._getPathForSuite(suite), state);
},
getScreenshotPath: function getScrenshotPath(suite, state, browser) {
return path.join(this.getScreenshotsDir(suite, state), browser + '.png');
},
_getPathForSuite: function(suite) {
var result = '';
while (suite) {
result = path.join(suite.name, result);
suite = suite.parent;
}
return result;
}
}, {
read: function read(configPath, overrides) {
return fs.read(configPath)
.then(function(content) {
return new Config(configPath, content, overrides);
})
.fail(function(e) {
if (e.code === 'ENOENT') {
throw new GeminiError(
'Config file does not exists: ' + configPath,
'Specify config file or configure your project by following\nthe instructions:\n\n' +
'https://github.com/bem/gemini#configuration'
);
}
throw e;
});
}
});