-
-
Notifications
You must be signed in to change notification settings - Fork 34
Expand file tree
/
Copy pathwithPage.js
More file actions
35 lines (32 loc) · 976 Bytes
/
withPage.js
File metadata and controls
35 lines (32 loc) · 976 Bytes
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
import puppeteer from 'puppeteer'
const fs = require('fs')
const path = require('path')
// Parse
function describe (jsHandle) {
return jsHandle.executionContext().evaluate((obj) => {
// serialize |obj| however you want
return JSON.stringify(obj)
}, jsHandle)
}
export default (t, run) => {
let _browser
return puppeteer
.launch({ headless: true, args: ['--no-sandbox'] })
.then((browser) => {
_browser = browser
return browser
.newPage()
.then((page) => {
page.on('console', async (msg) => {
const args = await Promise.all(msg.args().map((arg) => describe(arg)))
console.log('Logs from Headless Chrome', ...args)
})
return page.evaluate(fs.readFileSync(path.join(__dirname, 'bundled/Tween.js'), 'utf8')).then(() => page)
})
.then((page) => run(t, page).then(() => page))
})
.then((page) => {
_browser.close()
page.close()
})
}