mirror of
https://github.com/nuxt/nuxt.git
synced 2024-11-23 14:15:13 +00:00
3f1d634fb7
* Minor consistency enhancements * Arrow parenthesis consistency * Change linting rule * Fix typo * Update .eslintrc.js to only require parens for blocks * Update style according to brace-only suggestion * Remove --fix from lint * Tweak no-loading time (failing test) * Tweak no-loading time (failing test) (2) * Tweak no-loading time (failing test) (3) * Tweak no-loading time (failing test) (4) * Tweak no-loading time (failing test) (5)
78 lines
2.3 KiB
JavaScript
78 lines
2.3 KiB
JavaScript
import puppeteer from 'puppeteer'
|
|
|
|
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(
|
|
{
|
|
args: ['--no-sandbox', '--disable-setuid-sandbox'],
|
|
executablePath: process.env.PUPPETEER_EXECUTABLE_PATH
|
|
},
|
|
options
|
|
)
|
|
)
|
|
}
|
|
|
|
async close() {
|
|
if (!this.browser) return
|
|
await this.browser.close()
|
|
}
|
|
|
|
async page(url) {
|
|
if (!this.browser) throw new Error('Please call start() before page(url)')
|
|
const page = await this.browser.newPage()
|
|
await page.goto(url)
|
|
await page.waitForFunction('!!window.$nuxt')
|
|
page.html = () =>
|
|
page.evaluate(() => window.document.documentElement.outerHTML)
|
|
page.$text = selector => page.$eval(selector, el => el.textContent)
|
|
page.$$text = selector =>
|
|
page.$$eval(selector, els => els.map(el => el.textContent))
|
|
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
|
|
)
|
|
page.$nuxt = await page.evaluateHandle('window.$nuxt')
|
|
|
|
page.nuxt = {
|
|
async navigate(path, waitEnd = true) {
|
|
const hook = page.evaluate(() => {
|
|
return new Promise(resolve =>
|
|
window.$nuxt.$once('routeChanged', resolve)
|
|
).then(() => new Promise(resolve => setTimeout(resolve, 50)))
|
|
})
|
|
await page.evaluate(
|
|
($nuxt, path) => $nuxt.$router.push(path),
|
|
page.$nuxt,
|
|
path
|
|
)
|
|
if (waitEnd) await hook
|
|
return { hook }
|
|
},
|
|
routeData() {
|
|
return page.evaluate(($nuxt) => {
|
|
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)
|
|
}
|
|
}
|
|
return page
|
|
}
|
|
}
|