forked from JoelEinbinder/karma-playwright-launcher
-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
77 lines (62 loc) · 3.26 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
const {devices, webkit, chromium, firefox} = require('playwright');
function applyDecorators(self, baseLauncherDecorator, captureTimeoutLauncherDecorator, retryLauncherDecorator) {
baseLauncherDecorator(self);
captureTimeoutLauncherDecorator(self);
retryLauncherDecorator(self);
}
function pwBrowser(self, name, browserType, headless, args, logger) {
self.name = self.displayName = name;
self.on("start", async (url) => {
const log = logger.create(self.displayName);
try {
self._browser = await browserType.launch({ headless, ...args.launchOptions });
const page = await self._browser.newPage({ ...devices[args.device], ...args.contextOptions });
await page.goto(url);
} catch (err) {
log.error(err);
self._done("failure");
}
});
self.on("kill", async (done) => {
const log = logger.create(self.displayName);
try {
self._browser && await self._browser.close();
} catch (err) {
log.error(err);
}
self._done();
return process.nextTick(done);
});
}
function ChromiumBrowser(args, logger, baseLauncherDecorator, captureTimeoutLauncherDecorator, retryLauncherDecorator) {
applyDecorators(this, baseLauncherDecorator, captureTimeoutLauncherDecorator, retryLauncherDecorator);
pwBrowser(this, 'Chromium', chromium, false, args, logger);
}
function ChromiumHeadlessBrowser(args, logger, baseLauncherDecorator, captureTimeoutLauncherDecorator, retryLauncherDecorator) {
applyDecorators(this, baseLauncherDecorator, captureTimeoutLauncherDecorator, retryLauncherDecorator);
pwBrowser(this, 'ChromiumHeadless', chromium, true, args, logger);
}
function FirefoxBrowser(args, logger, baseLauncherDecorator, captureTimeoutLauncherDecorator, retryLauncherDecorator) {
applyDecorators(this, baseLauncherDecorator, captureTimeoutLauncherDecorator, retryLauncherDecorator);
pwBrowser(this, 'Firefox', firefox, false, args, logger);
}
function FirefoxHeadlessBrowser(args, logger, baseLauncherDecorator, captureTimeoutLauncherDecorator, retryLauncherDecorator) {
applyDecorators(this, baseLauncherDecorator, captureTimeoutLauncherDecorator, retryLauncherDecorator);
pwBrowser(this, 'FirefoxHeadless', firefox, true, args, logger);
}
function WebKitBrowser(args, logger, baseLauncherDecorator, captureTimeoutLauncherDecorator, retryLauncherDecorator) {
applyDecorators(this, baseLauncherDecorator, captureTimeoutLauncherDecorator, retryLauncherDecorator);
pwBrowser(this, 'WebKit', webkit, false, args, logger);
}
function WebKitHeadlessBrowser(args, logger, baseLauncherDecorator, captureTimeoutLauncherDecorator, retryLauncherDecorator) {
applyDecorators(this, baseLauncherDecorator, captureTimeoutLauncherDecorator, retryLauncherDecorator);
pwBrowser(this, 'WebKitHeadless', webkit, true, args, logger);
}
module.exports = {
'launcher:Chromium': ['type', ChromiumBrowser],
'launcher:ChromiumHeadless': ['type', ChromiumHeadlessBrowser],
'launcher:Firefox': ['type', FirefoxBrowser],
'launcher:FirefoxHeadless': ['type', FirefoxHeadlessBrowser],
'launcher:WebKit': ['type', WebKitBrowser],
'launcher:WebKitHeadless': ['type', WebKitHeadlessBrowser],
}