2017-11-01 15:44:27 +00:00
|
|
|
import puppeteer from 'puppeteer'
|
|
|
|
|
2018-03-19 08:29:44 +00:00
|
|
|
export default class Browser {
|
|
|
|
async start(options = {}) {
|
|
|
|
// https://github.com/GoogleChrome/puppeteer/blob/master/docs/api.md#puppeteerlaunchoptions
|
|
|
|
this.browser = await puppeteer.launch(
|
|
|
|
Object.assign(
|
|
|
|
{
|
2018-03-20 11:07:05 +00:00
|
|
|
args: ['--no-sandbox', '--disable-setuid-sandbox'],
|
|
|
|
executablePath: process.env.PUPPETEER_EXECUTABLE_PATH
|
2018-03-19 08:29:44 +00:00
|
|
|
},
|
|
|
|
options
|
|
|
|
)
|
2018-01-13 05:22:11 +00:00
|
|
|
)
|
2018-03-19 08:29:44 +00:00
|
|
|
}
|
2017-11-01 15:44:27 +00:00
|
|
|
|
2018-03-19 08:29:44 +00:00
|
|
|
async close() {
|
|
|
|
if (!this.browser) return
|
|
|
|
await this.browser.close()
|
|
|
|
}
|
2017-11-01 15:44:27 +00:00
|
|
|
|
2018-10-09 12:07:23 +00:00
|
|
|
async page(url, globalName = 'nuxt') {
|
2018-03-19 08:29:44 +00:00
|
|
|
if (!this.browser) throw new Error('Please call start() before page(url)')
|
|
|
|
const page = await this.browser.newPage()
|
|
|
|
await page.goto(url)
|
2018-10-09 12:07:23 +00:00
|
|
|
page.$nuxtGlobalHandle = `window.$${globalName}`
|
|
|
|
await page.waitForFunction(`!!${page.$nuxtGlobalHandle}`)
|
2018-03-19 08:29:44 +00:00
|
|
|
page.html = () =>
|
|
|
|
page.evaluate(() => window.document.documentElement.outerHTML)
|
2018-11-24 18:49:19 +00:00
|
|
|
page.$text = (selector, trim) => page.$eval(selector, (el, trim) => {
|
|
|
|
return trim ? el.textContent.replace(/^\s+|\s+$/g, '') : el.textContent
|
|
|
|
}, trim)
|
|
|
|
page.$$text = (selector, trim) =>
|
|
|
|
page.$$eval(selector, (els, trim) => els.map((el) => {
|
|
|
|
return trim ? el.textContent.replace(/^\s+|\s+$/g, '') : el.textContent
|
|
|
|
}), trim)
|
2018-03-19 08:29:44 +00:00
|
|
|
page.$attr = (selector, attr) =>
|
|
|
|
page.$eval(selector, (el, attr) => el.getAttribute(attr), attr)
|
|
|
|
page.$$attr = (selector, attr) =>
|
|
|
|
page.$$eval(
|
|
|
|
selector,
|
|
|
|
(els, attr) => els.map(el => el.getAttribute(attr)),
|
|
|
|
attr
|
2018-01-13 05:22:11 +00:00
|
|
|
)
|
2018-10-09 12:07:23 +00:00
|
|
|
|
|
|
|
page.$nuxt = await page.evaluateHandle(page.$nuxtGlobalHandle)
|
2018-03-19 08:29:44 +00:00
|
|
|
|
|
|
|
page.nuxt = {
|
|
|
|
async navigate(path, waitEnd = true) {
|
2018-10-09 12:07:23 +00:00
|
|
|
const hook = page.evaluate(`
|
|
|
|
new Promise(resolve =>
|
|
|
|
${page.$nuxtGlobalHandle}.$once('routeChanged', resolve)
|
2018-03-19 08:29:44 +00:00
|
|
|
).then(() => new Promise(resolve => setTimeout(resolve, 50)))
|
2018-10-09 12:07:23 +00:00
|
|
|
`)
|
2018-03-19 08:29:44 +00:00
|
|
|
await page.evaluate(
|
|
|
|
($nuxt, path) => $nuxt.$router.push(path),
|
|
|
|
page.$nuxt,
|
|
|
|
path
|
|
|
|
)
|
2018-10-09 12:07:23 +00:00
|
|
|
if (waitEnd) {
|
|
|
|
await hook
|
|
|
|
}
|
2018-03-19 08:29:44 +00:00
|
|
|
return { hook }
|
|
|
|
},
|
|
|
|
routeData() {
|
2018-08-06 00:12:44 +00:00
|
|
|
return page.evaluate(($nuxt) => {
|
2018-03-19 08:29:44 +00:00
|
|
|
return {
|
|
|
|
path: $nuxt.$route.path,
|
|
|
|
query: $nuxt.$route.query
|
|
|
|
}
|
|
|
|
}, page.$nuxt)
|
|
|
|
},
|
|
|
|
loadingData() {
|
|
|
|
return page.evaluate($nuxt => $nuxt.$loading.$data, page.$nuxt)
|
|
|
|
},
|
|
|
|
errorData() {
|
|
|
|
return page.evaluate($nuxt => $nuxt.nuxt.err, page.$nuxt)
|
|
|
|
},
|
|
|
|
storeState() {
|
|
|
|
return page.evaluate($nuxt => $nuxt.$store.state, page.$nuxt)
|
|
|
|
}
|
2017-11-01 15:44:27 +00:00
|
|
|
}
|
2018-03-19 08:29:44 +00:00
|
|
|
return page
|
2017-11-01 15:44:27 +00:00
|
|
|
}
|
|
|
|
}
|