From dde616fbdbf64715217a0fbebbb112acb25d04e3 Mon Sep 17 00:00:00 2001 From: Clark Du Date: Mon, 19 Mar 2018 16:29:44 +0800 Subject: [PATCH] fix: avoid override of puppeteer browser --- test/e2e/basic.browser.test.js | 7 +- test/e2e/children.patch.browser.test.js | 7 +- test/utils/browser.js | 142 ++++++++++++------------ 3 files changed, 79 insertions(+), 77 deletions(-) diff --git a/test/e2e/basic.browser.test.js b/test/e2e/basic.browser.test.js index 5d9c972cdc..cd8e8a9e62 100644 --- a/test/e2e/basic.browser.test.js +++ b/test/e2e/basic.browser.test.js @@ -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 diff --git a/test/e2e/children.patch.browser.test.js b/test/e2e/children.patch.browser.test.js index 5f4b937e1a..9aa830d617 100644 --- a/test/e2e/children.patch.browser.test.js +++ b/test/e2e/children.patch.browser.test.js @@ -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 diff --git a/test/utils/browser.js b/test/utils/browser.js index 8cf29162e8..2888cf8183 100644 --- a/test/utils/browser.js +++ b/test/utils/browser.js @@ -1,76 +1,76 @@ import puppeteer from 'puppeteer' -let browser = null - -export async function start(options = {}) { - // https://github.com/GoogleChrome/puppeteer/blob/master/docs/api.md#puppeteerlaunchoptions - browser = await puppeteer.launch( - Object.assign( - { - args: ['--no-sandbox', '--disable-setuid-sandbox'] - }, - options - ) - ) -} - -export async function close() { - if (!browser) return - await browser.close() -} - -export async function page(url) { - if (!browser) throw new Error('Please call start() before page(url)') - const page = await 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 +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'] + }, + options ) - 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) - } + ) + } + + 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 } - return page }