-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
80 lines (63 loc) · 2.16 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
'use strict'
const Provider = require('browser-provider')
const Browser = require('abstract-browser')
const playwright = require('playwright')
const kPlaywright = Symbol('kPlaywright')
const kDisconnected = Symbol('kDisconnected')
const kBrowser = Symbol('kBrowser')
class PlaywrightProvider extends Provider.promises {
// Playwright doesn't expose versions (before launching):
// https://github.com/microsoft/playwright/issues/2604
async _manifests () {
const options = { headless: true }
const supports = { headless: true }
const dev = { ...supports, devtools: true }
return [
{ name: 'chromium', title: 'Playwright Chromium', options, supports: dev },
{ name: 'webkit', title: 'Playwright Webkit', options, supports },
{ name: 'firefox', title: 'Playwright Firefox', options, supports }
]
}
_browser (manifest, target) {
return new PlaywrightBrowser(manifest, target)
}
}
class PlaywrightBrowser extends Browser.promises {
constructor (manifest, target) {
super(manifest, target)
this[kPlaywright] = playwright[this.manifest.name]
this[kDisconnected] = this[kDisconnected].bind(this)
this[kBrowser] = null
}
async _open () {
const options = this.manifest.options
const headless = options.headless
const devtools = this.manifest.supports.devtools && !headless
this[kBrowser] = await this[kPlaywright].launch({
headless,
devtools,
// This is handled in airtap for all browsers
handleSIGINT: false,
handleSIGTERM: false,
handleSIGHUP: false,
...options.launch
})
this[kBrowser].once('disconnected', this[kDisconnected])
const context = await this[kBrowser].newContext(options.context)
const page = await context.newPage(options.page)
return page.goto(this.target.url)
}
[kDisconnected] () {
this[kBrowser] = null
this.emit('error', new Error('Premature exit'))
}
async _close () {
if (this[kBrowser]) {
this[kBrowser].removeListener('disconnected', this[kDisconnected])
const promise = this[kBrowser].close()
this[kBrowser] = null
return promise
}
}
}
module.exports = PlaywrightProvider