-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathindex.js
445 lines (396 loc) · 14.7 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
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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
var wd = require('selenium-webdriver');
var urlModule = require('url');
var urlparse = urlModule.parse;
var urlformat = urlModule.format;
var chrome = require('selenium-webdriver/chrome');
var edge = require('selenium-webdriver/edge');
var firefox = require('selenium-webdriver/firefox');
var ie = require('selenium-webdriver/ie');
var safari = require('selenium-webdriver/safari');
var until = wd.until;
const CREATING_SESSION = 'CREATING_SESSION';
const CREATED_SESSION = 'CREATED_SESSION';
const WAITING = 'WAITING';
const ignoreArgs = ['base', 'gridUrl', 'suppressWarning', 'x-ua-compatible',
'heartbeatInterval', 'promptOn', 'delayLaunch', 'windowGeometry'];
// default preferences shamelessly taken from karma-firefox-launcher
const defaultFirefoxPrefs = {
'browser.shell.checkDefaultBrowser': false,
'browser.bookmarks.restore_default_bookmarks': false,
'dom.disable_open_during_load': false,
'dom.max_script_run_time': 0,
'extensions.autoDisableScopes': 0,
'browser.tabs.remote.autostart': false,
'browser.tabs.remote.autostart.2': false,
'extensions.enabledScopes': 15,
};
// default chrome args shamelessly taken from karma-chrome-launcher
const defaultChromeArgs = [
'--no-default-browser-check',
'--no-first-run',
'--disable-default-apps',
'--disable-popup-blocking',
'--disable-translate',
'--disable-background-timer-throttling',
// on macOS, disable-background-timer-throttling is not enough
// and we need disable-renderer-backgrounding too
// see https://github.com/karma-runner/karma-chrome-launcher/issues/123
'--disable-renderer-backgrounding',
'--disable-device-discovery-notifications',
];
const defaultIeOptions = {
browserAttachTimeout: 30000,
};
var SeleniumGridInstance = function (name, args, logger, baseLauncherDecorator,
captureTimeoutLauncherDecorator, retryLauncherDecorator) {
if (!args.browserName) {
throw new Error('browserName is required!');
}
var log = logger.create('SeleniumGrid');
var gridUrl = args.gridUrl || 'http://localhost:4444/wd/hub';
var self = this;
// Intialize capabilities with default values
const capabilities = new wd.Capabilities({
platform: 'ANY',
testName: 'Karma test',
version: ''
});
if (args.browserName === 'internet explorer') {
if (!args.suppressWarning) {
log.warn('Internet Explorer requires some specific configuration to work properly. ' +
'Follow the instructions on https://github.com/SeleniumHQ/selenium/wiki/InternetExplorerDriver#required-configuration ' +
'and then add `suppressWarning` to the launcher config.');
}
// workaround until IE options are properly implemented in selenium-webdriver
capabilities.set('se:ieOptions', defaultIeOptions);
}
const options = {};
options[wd.Browser.CHROME] = new chrome.Options();
options[wd.Browser.EDGE] = new edge.Options();
options[wd.Browser.FIREFOX] = new firefox.Options();
options[wd.Browser.IE] = new ie.Options();
options[wd.Browser.SAFARI] = new safari.Options();
Object.keys(defaultFirefoxPrefs).forEach((pref) => {
options[wd.Browser.FIREFOX].setPreference(pref, defaultFirefoxPrefs[pref]);
});
options[wd.Browser.CHROME].addArguments(defaultChromeArgs);
Object.keys(args).forEach(function (key) {
if (ignoreArgs.indexOf(key) !== -1) {
// used strictly for karma
return;
}
if (key === 'firefoxPreferences') {
Object.keys(args.firefoxPreferences).forEach((pref) => {
options[wd.Browser.FIREFOX].setPreference(pref,
args.firefoxPreferences[pref]);
});
return;
}
if ((key === 'arguments' || key === 'extensions' || key === 'options') &&
(!options[args.browserName])) {
throw new Error(key + ' not supported when using non-standard browser ' +
args.browserName + '; you must use the equivalent capability');
}
if (key === 'arguments') {
if (args[key].constructor !== Array) {
throw new Error('arguments must be an Array for ' + args.browserName);
}
if (!options[args.browserName].addArguments) {
throw new Error('arguments not supported for ' + args.browserName);
}
options[args.browserName].addArguments(args[key]);
return;
}
if (key === 'extensions') {
if (args[key].constructor !== Array) {
throw new Error('extensions must be an Array for ' + args.browserName);
}
if (!options[args.browserName].addExtensions) {
throw new Error('extensions not supported for ' + args.browserName);
}
options[args.browserName].addArguments(args[key]);
return;
}
if (key === 'options') {
if (!typeof args[key] === 'object') {
throw new Error('options must be an object for ' + args.browserName);
}
Object.keys(args[key]).forEach((option) => {
if (!options[args.browserName][option]) {
throw new Error('option ' + option + ' not supported for ' + args.browserName);
}
options[args.browserName][option](args[key][option]);
});
return;
}
capabilities.set(key, args[key]);
});
baseLauncherDecorator(this);
captureTimeoutLauncherDecorator(this);
retryLauncherDecorator(this);
self.name = name;
self.browser = null;
var heartbeatErrors = 0;
var heartbeat;
const heartbeatFunction = () => {
log.debug('hearbeat for ' + self.name);
self.browser.getTitle()
.catch((err) => {
heartbeatErrors++;
log.error('Caught error for browser ' + self.name + ' during ' +
'heartbeat: ' + err);
if (err.name !== 'NoSuchSessionError') {
log.error(err.stack);
}
if (heartbeatErrors >= 5) {
log.error('Too many heartbeat errors, attempting to stop ' + self.name);
args.heartbeatInterval && clearInterval(heartbeat);
this.error = this.error || err;
this.kill();
}
});
};
const promptFunction = (elId) => {
return new Promise((resolve, reject) => {
log.info('inside prompt function for ' + self.name);
// sometimes we get stuck inside the prompt function when quitting,
// so reject after a fixed amount of time just to unblock things
let bumpTimeout = setTimeout(() => reject('prompt function stuck'), 10000);
var windowRef;
self.browser.getWindowHandle().then((ref) => {
windowRef = ref;
return self.browser.switchTo().frame(0);
})
.then(() => self.browser.wait(until.elementLocated(wd.By.id(elId))))
.then(() => {
log.debug('Trying to focus ' + self.name + ' with an alert...');
return self.browser.switchTo().frame(null);
})
.then(() => self.browser.switchTo().window(windowRef))
.then(() => self.browser.executeScript("alert('test')"))
.then(() => self.browser.switchTo().alert())
.then((alert) => alert.dismiss())
.then(() => self.browser.switchTo().window(windowRef))
.then(() => {
resolve();
clearTimeout(bumpTimeout);
})
.catch((err) => {
log.error('caught in prompt for ' + self.name + ': ' + err);
clearTimeout(bumpTimeout);
reject(err);
});
});
};
// This is done by passing the option on the url, in response the Karma server will
// set the following meta in the page.
// <meta http-equiv="X-UA-Compatible" content="[VALUE]"/>
function handleXUaCompatible(urlObj) {
if (args['x-ua-compatible']) {
urlObj.query['x-ua-compatible'] = args['x-ua-compatible'];
}
}
var loadTimeout = null;
var started;
var startPromise;
this.awaitingKill = () => {
return this.state === this.STATE_BEING_KILLED ||
this.state === this.STATE_BEING_FORCE_KILLED;
};
this.on('start', (url) => {
// reset kill state and error state so we don't auto-end the session immediately
this.error = null;
var urlObj = urlparse(url, true);
handleXUaCompatible(urlObj);
delete urlObj.search; //url.format does not want search attribute
url = urlformat(urlObj);
log.debug('Grid URL: ' + gridUrl);
log.debug('Browser capabilities: ' + JSON.stringify(capabilities));
let delayTime = 0;
if (args.delayLaunch) {
log.debug('Delaying launch of ' + args.browserName + ' for ' + args.delayLaunch + 'ms');
this.state = WAITING;
delayTime = args.delayLaunch;
}
loadTimeout = setTimeout(() => {
let errorStage;
log.debug('Launching ' + self.name);
this.state = CREATING_SESSION;
startPromise = new Promise((resolve, reject) => {
self.browser = new wd.Builder()
.setChromeOptions(options[wd.Browser.CHROME])
.setEdgeOptions(options[wd.Browser.EDGE])
.setFirefoxOptions(options[wd.Browser.FIREFOX])
.setIeOptions(options[wd.Browser.IE])
.setSafariOptions(options[wd.Browser.SAFARI])
.usingServer(gridUrl)
.withCapabilities(capabilities)
.build()
self.browser.then(() => {
log.debug(self.name + ' started');
resolve();
started = true;
this.state = CREATED_SESSION;
startPromise = startPromise.then(() => {
log.debug(self.name + ' resolved startPromise; navigating to karma page');
});
startPromise = startPromise.then(() => self.browser.get(url))
.then(() => {
log.debug(self.name + ' loaded karma; running applicable initialization');
return Promise.resolve();
})
.catch(err => {
errorStage = 'navigate';
return Promise.reject(err);
});
if (args.windowGeometry) {
startPromise = startPromise.then(() => {
log.debug(self.name + ' setting window geometry');
return self.browser.manage().window().setRect(args.windowGeometry).then(() => {
log.debug(self.name + ' set window geometry');
return Promise.resolve();
});
});
}
if (args.promptOn) {
startPromise = startPromise.then(() => promptFunction(args.promptOn));
}
if (args.heartbeatInterval) {
// TODO: This should maybe be before the `get` call?
startPromise = startPromise.then(() => {
log.debug(self.name + ' setting heartbeat');
heartbeat = setInterval(heartbeatFunction, args.heartbeatInterval);
return Promise.resolve();
});
}
startPromise = startPromise.then(() => {
log.debug(self.name + ' initialized');
return Promise.resolve();
});
startPromise = startPromise.catch((err) => {
if (errorStage) {
// re-raise error caught during navigation
return Promise.reject(err);
} else {
errorStage = 'init';
return Promise.reject(err);
}
});
return startPromise;
})
.catch((err) => {
if (this.awaitingKill()) {
log.debug('ignoring error from ' + self.name + '; browser is shutting down');
reject(err);
return Promise.resolve();
} else {
started = false;
let message;
if (errorStage === 'init') {
message = self.name + ' encountered error during initialization: ' + err;
} else if (errorStage === 'navigate') {
message = self.name + ' encountered error navigating to karma page: ' + err;
} else {
message = self.name + ' was unable to create WebDriver session: ' + err;
}
log.error(message);
this.error = err;
reject(message);
self._done();
}
}); // self.browser.then
}); // startPromise
}, delayTime);
});
let killInterval;
let killElapsed = 0;
this._stopSession = (done, startError) => {
var self = this;
let killPromise = Promise.resolve();
clearInterval(heartbeat);
if (!started) {
log.info(self.name + ' not started... Killed ' + self.name);
done();
return killPromise;
}
if (args.resetBeforeQuit) {
// load a blank page and wait before quitting. for browsers that don't
// close the window before quitting. noticed on real iOS 12 devices.
killPromise = killPromise.then(() => {
log.debug('Resetting ' + self.name + ' and pausing.');
return new Promise((resolve, reject) => {
self.browser.get('about:blank').then(() => setTimeout(() => {
log.debug('Reset ' + self.name + ' to about:blank');
resolve();
}, 10000));
});
});
}
if (args.closeBeforeQuit) {
// explicitly call browser.close(), which should be unnecessary according
// to webdriver. noticed on real iOS 10 devices.
killPromise = killPromise.then(() => {
log.debug('Closing browser window for ' + self.name + '.');
return self.browser.close().then(() => {
log.debug('Closed browser window for ' + self.name);
this.state = 'CLOSED';
return Promise.resolve();
});
});
}
killPromise = killPromise.then(() => {
log.debug('Quitting WebDriver for ' + self.name + '.');
return self.browser.quit()
.then(() => {
log.info('Killed ' + self.name + '.');
done();
return Promise.resolve();
});
});
killPromise = killPromise.catch((err) => {
if (err.name !== 'NoSuchSessionError' && err !== startError) {
// ignore NoSuchSessionErrors when killing
log.error('Error stopping browser ' + self.name + ': ' + err.toString());
}
done();
return Promise.resolve();
});
return killPromise;
};
this.on('kill', (done) => {
var self = this;
log.info('Trying to kill ' + self.name);
const end = () => {
self._done();
if (done) {
done();
}
};
const stopSession = (err) => {
return new Promise((startPromiseResolve, startPromiseReject) => {
this._stopSession(end, err).then(() => {
clearInterval(killInterval);
resolve('shutting down');
startPromiseReject('shutting down');
});
});
};
if (!self.browser) {
log.info('Browser ' + self.name + ' has not yet launched.');
loadTimeout && clearTimeout(loadTimeout);
end();
return Promise.resolve();
}
killInterval = setInterval(() => {
killElapsed += 10;
log.info('Waiting for ' + self.name + ' to quit... (' + killElapsed + 's)');
}, 10000);
return new Promise((resolve, reject) => {
startPromise = startPromise.then(stopSession, stopSession);
});
});
};
// PUBLISH DI MODULE
module.exports = {
'launcher:SeleniumGrid': ['type', SeleniumGridInstance]
};