Skip to content

Commit 341356b

Browse files
committed
feat: split puppeteer/puppeteer-core
1 parent 0cdbd66 commit 341356b

File tree

4 files changed

+108
-73
lines changed

4 files changed

+108
-73
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@
7070
"browserstack-local": "^1.3.7",
7171
"chromedriver": "^2.46.0",
7272
"geckodriver": "^1.16.0",
73+
"puppeteer": "^1.13.0",
7374
"puppeteer-core": "^1.13.0",
7475
"selenium-webdriver": "^4.0.0-alpha.1"
7576
}

src/puppeteer/core.js

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
import ChromeDetector from '../utils/detectors/chrome'
2+
import Browser from '../browser'
3+
import BrowserError from '../utils/error'
4+
import Webpage from './webpage'
5+
6+
export default class PuppeteerCoreBrowser extends Browser {
7+
async _loadDependencies() {
8+
super._loadDependencies()
9+
10+
if (!PuppeteerCoreBrowser.core) {
11+
PuppeteerCoreBrowser.core = await this.loadDependency('puppeteer-core')
12+
}
13+
}
14+
15+
setHeadless() {
16+
super.setHeadless()
17+
this.config.browserArguments.push('--headless')
18+
}
19+
20+
setLogLevel(types) {
21+
const typeMap = {
22+
warning: 'warn'
23+
}
24+
25+
if (types && typeof types === 'string') {
26+
types = [types]
27+
}
28+
29+
this.hook('page:created', (page) => {
30+
/* eslint-disable no-console */
31+
page.on('console', (msg) => {
32+
const msgType = msg.type()
33+
let type = typeMap[msgType] || msgType
34+
if (!types || types.includes(msgType) || types.includes(type)) {
35+
if (!console[type]) {
36+
console.warn(`Unknown console type ${type}`)
37+
type = 'log'
38+
}
39+
console[type](msg.text())
40+
}
41+
})
42+
/* eslint-enable no-console */
43+
})
44+
}
45+
46+
async _start(capabilities, ...args) {
47+
// https://github.com/GoogleChrome/puppeteer/blob/master/docs/api.md#puppeteerlaunchoptions
48+
const launchOptions = {
49+
args: [
50+
'--no-sandbox',
51+
'--disable-setuid-sandbox',
52+
...args,
53+
...this.config.browserArguments
54+
],
55+
...capabilities
56+
}
57+
58+
if (this.constructor.name === 'PuppeteerCoreBrowser') {
59+
let executablePath = process.env.PUPPETEER_EXECUTABLE_PATH
60+
if (!executablePath) {
61+
const detector = new ChromeDetector()
62+
executablePath = detector.detect()
63+
}
64+
65+
if (!executablePath) {
66+
throw new BrowserError(this, `Could not find a Chrome executable`)
67+
}
68+
69+
launchOptions.executablePath = executablePath
70+
}
71+
72+
this.driver = await PuppeteerCoreBrowser.core.launch(launchOptions)
73+
}
74+
75+
async _close() {
76+
if (!this.driver) {
77+
return
78+
}
79+
80+
await this.driver.close()
81+
}
82+
83+
_page(url, readyCondition) {
84+
const page = new Webpage(this)
85+
return page.open(url, readyCondition)
86+
}
87+
}

src/puppeteer/index.js

Lines changed: 6 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -1,79 +1,12 @@
1-
import ChromeDetector from '../utils/detectors/chrome'
2-
import Browser from '../browser'
3-
import BrowserError from '../utils/error'
4-
import Webpage from './webpage'
1+
import PuppeteerCoreBrowser from './core'
52

6-
export default class PuppeteerBrowser extends Browser {
3+
export default class PuppeteerBrowser extends PuppeteerCoreBrowser {
74
async _loadDependencies() {
8-
super._loadDependencies()
9-
10-
if (!PuppeteerBrowser.core) {
11-
PuppeteerBrowser.core = await this.loadDependency('puppeteer-core')
12-
}
13-
}
14-
15-
setLogLevel(types) {
16-
const typeMap = {
17-
warning: 'warn'
18-
}
19-
20-
if (types && typeof types === 'string') {
21-
types = [types]
22-
}
23-
24-
this.hook('page:created', (page) => {
25-
/* eslint-disable no-console */
26-
page.on('console', (msg) => {
27-
const msgType = msg.type()
28-
let type = typeMap[msgType] || msgType
29-
if (!types || types.includes(msgType) || types.includes(type)) {
30-
if (!console[type]) {
31-
console.warn(`Unknown console type ${type}`)
32-
type = 'log'
33-
}
34-
console[type](msg.text())
35-
}
36-
})
37-
/* eslint-enable no-console */
38-
})
39-
}
40-
41-
async _start(capabilities, ...args) {
42-
let executablePath = process.env.PUPPETEER_EXECUTABLE_PATH
43-
if (!executablePath) {
44-
const detector = new ChromeDetector()
45-
executablePath = detector.detect()
5+
if (!PuppeteerCoreBrowser.core) {
6+
PuppeteerCoreBrowser.core = await this.loadDependency('puppeteer')
467
}
478

48-
if (!executablePath) {
49-
throw new BrowserError(this, `Could not find a chrome executable`)
50-
}
51-
52-
// https://github.com/GoogleChrome/puppeteer/blob/master/docs/api.md#puppeteerlaunchoptions
53-
const launchOptions = {
54-
args: [
55-
'--no-sandbox',
56-
'--disable-setuid-sandbox',
57-
'--disable-dev-shm-usage',
58-
...args
59-
],
60-
executablePath,
61-
...capabilities
62-
}
63-
64-
this.driver = await PuppeteerBrowser.core.launch(launchOptions)
65-
}
66-
67-
async _close() {
68-
if (!this.driver) {
69-
return
70-
}
71-
72-
await this.driver.close()
73-
}
74-
75-
_page(url, readyCondition) {
76-
const page = new Webpage(this)
77-
return page.open(url, readyCondition)
9+
// call super after setting core
10+
super._loadDependencies()
7811
}
7912
}

yarn.lock

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4764,6 +4764,20 @@ puppeteer-core@^1.13.0:
47644764
rimraf "^2.6.1"
47654765
ws "^6.1.0"
47664766

4767+
puppeteer@^1.13.0:
4768+
version "1.13.0"
4769+
resolved "https://registry.yarnpkg.com/puppeteer/-/puppeteer-1.13.0.tgz#187ccf5ed5caf08ed1291b262d033cc364bf88ab"
4770+
integrity sha512-LUXgvhjfB/P6IOUDAKxOcbCz9ISwBLL9UpKghYrcBDwrOGx1m60y0iN2M64mdAUbT4+7oZM5DTxOW7equa2fxQ==
4771+
dependencies:
4772+
debug "^4.1.0"
4773+
extract-zip "^1.6.6"
4774+
https-proxy-agent "^2.2.1"
4775+
mime "^2.0.3"
4776+
progress "^2.0.1"
4777+
proxy-from-env "^1.0.0"
4778+
rimraf "^2.6.1"
4779+
ws "^6.1.0"
4780+
47674781
q@^1.5.1:
47684782
version "1.5.1"
47694783
resolved "https://registry.yarnpkg.com/q/-/q-1.5.1.tgz#7e32f75b41381291d04611f1bf14109ac00651d7"

0 commit comments

Comments
 (0)