fix: avoid override of puppeteer browser

This commit is contained in:
Clark Du 2018-03-19 16:29:44 +08:00
parent be4adbd366
commit dde616fbdb
No known key found for this signature in database
GPG Key ID: D0E5986AF78B86D9
3 changed files with 79 additions and 77 deletions

View File

@ -1,8 +1,9 @@
import { Nuxt } from '../..'
import { loadFixture, getPort } from '../utils'
import * as browser from '../utils/browser'
import { Nuxt } from '..'
import Browser from './utils/browser'
import { loadFixture, getPort } from './utils'
let port
const browser = new Browser()
const url = route => 'http://localhost:' + port + route
let nuxt = null

View File

@ -1,8 +1,9 @@
import { Nuxt, Utils } from '../..'
import * as browser from '../utils/browser'
import { loadFixture, getPort } from '../utils'
import { Nuxt, Utils } from '..'
import Browser from './utils/browser'
import { loadFixture, getPort } from './utils'
let port
const browser = new Browser()
const url = route => 'http://localhost:' + port + route
let nuxt = null

View File

@ -1,10 +1,9 @@
import puppeteer from 'puppeteer'
let browser = null
export async function start(options = {}) {
export default class Browser {
async start(options = {}) {
// https://github.com/GoogleChrome/puppeteer/blob/master/docs/api.md#puppeteerlaunchoptions
browser = await puppeteer.launch(
this.browser = await puppeteer.launch(
Object.assign(
{
args: ['--no-sandbox', '--disable-setuid-sandbox']
@ -14,14 +13,14 @@ export async function start(options = {}) {
)
}
export async function close() {
if (!browser) return
await browser.close()
async close() {
if (!this.browser) return
await this.browser.close()
}
export async function page(url) {
if (!browser) throw new Error('Please call start() before page(url)')
const page = await browser.newPage()
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 = () =>
@ -74,3 +73,4 @@ export async function page(url) {
}
return page
}
}