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.
This commit is contained in:
Andrey Lushnikov 2018-06-01 13:57:50 -07:00 committed by GitHub
parent 07b91f61a9
commit 9955a1e673
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 23 additions and 1 deletions

View File

@ -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}
*/

View File

@ -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);