From 9955a1e67391ab2be20bbf98eccaf9333726d936 Mon Sep 17 00:00:00 2001 From: Andrey Lushnikov Date: Fri, 1 Jun 2018 13:57:50 -0700 Subject: [PATCH] fix(browser): ensure first page is created when browser is launched (#2658) It's impossible to launch chromium without initial page. This patch makes sure that `puppeteer.launch()` always returns a browser with at least one page user can connect to. --- lib/Launcher.js | 23 ++++++++++++++++++++++- test/puppeteer.spec.js | 1 + 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/lib/Launcher.js b/lib/Launcher.js index e33a146f2be..6c191c1d0f6 100644 --- a/lib/Launcher.js +++ b/lib/Launcher.js @@ -165,12 +165,33 @@ class Launcher { } const ignoreHTTPSErrors = !!options.ignoreHTTPSErrors; const setDefaultViewport = !options.appMode; - return Browser.create(connection, [], ignoreHTTPSErrors, setDefaultViewport, chromeProcess, gracefullyCloseChrome); + const browser = await Browser.create(connection, [], ignoreHTTPSErrors, setDefaultViewport, chromeProcess, gracefullyCloseChrome); + await ensureInitialPage(browser); + return browser; } catch (e) { killChrome(); throw e; } + /** + * @param {!Browser} browser + */ + async function ensureInitialPage(browser) { + // Wait for initial page target to be created. + if (browser.targets().find(target => target.type() === 'page')) + return; + + let initialPageCallback; + const initialPagePromise = new Promise(resolve => initialPageCallback = resolve); + const listeners = [helper.addEventListener(browser, 'targetcreated', target => { + if (target.type() === 'page') + initialPageCallback(); + })]; + + await initialPagePromise; + helper.removeEventListeners(listeners); + } + /** * @return {Promise} */ diff --git a/test/puppeteer.spec.js b/test/puppeteer.spec.js index f3de335e844..d81f35ad5bf 100644 --- a/test/puppeteer.spec.js +++ b/test/puppeteer.spec.js @@ -218,6 +218,7 @@ module.exports.addTests = function({testRunner, expect, PROJECT_ROOT, defaultBro it('should support the pipe option', async() => { const options = Object.assign({pipe: true}, defaultBrowserOptions); const browser = await puppeteer.launch(options); + expect((await browser.pages()).length).toBe(1); expect(browser.wsEndpoint()).toBe(''); const page = await browser.newPage(); expect(await page.evaluate('11 * 11')).toBe(121);