feat(launcher): add new launcher option waitForInitialPage (#7105)

The existing behavior is expected to be unchanged as the value defaults to true.
Adding such option would allow user to skip the initial wait.

Issue: #3630
This commit is contained in:
Peng-Yu Chen 2021-05-06 21:30:04 +01:00 committed by GitHub
parent c9978d20d5
commit 2605309f74
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 32 additions and 2 deletions

View File

@ -625,6 +625,7 @@ try {
- `pipe` <[boolean]> Connects to the browser over a pipe instead of a WebSocket. Defaults to `false`. - `pipe` <[boolean]> Connects to the browser over a pipe instead of a WebSocket. Defaults to `false`.
- `extraPrefsFirefox` <[Object]> Additional [preferences](https://developer.mozilla.org/en-US/docs/Mozilla/Preferences/Preference_reference) that can be passed to Firefox (see `PUPPETEER_PRODUCT`) - `extraPrefsFirefox` <[Object]> Additional [preferences](https://developer.mozilla.org/en-US/docs/Mozilla/Preferences/Preference_reference) that can be passed to Firefox (see `PUPPETEER_PRODUCT`)
- `targetFilter` <?[function]\([Protocol.Target.TargetInfo]\):[boolean]> Use this function to decide if Puppeteer should connect to the given target. If a `targetFilter` is provided, Puppeteer only connects to targets for which `targetFilter` returns `true`. By default, Puppeteer connects to all available targets. - `targetFilter` <?[function]\([Protocol.Target.TargetInfo]\):[boolean]> Use this function to decide if Puppeteer should connect to the given target. If a `targetFilter` is provided, Puppeteer only connects to targets for which `targetFilter` returns `true`. By default, Puppeteer connects to all available targets.
- `waitForInitialPage` <[boolean]> Whether to wait for the initial page to be ready. Defaults to `true`.
- returns: <[Promise]<[Browser]>> Promise which resolves to browser instance. - returns: <[Promise]<[Browser]>> Promise which resolves to browser instance.
You can use `ignoreDefaultArgs` to filter out `--mute-audio` from default arguments: You can use `ignoreDefaultArgs` to filter out `--mute-audio` from default arguments:

View File

@ -110,6 +110,12 @@ export interface LaunchOptions {
* {@link https://developer.mozilla.org/en-US/docs/Mozilla/Preferences/Preference_reference | Additional preferences } that can be passed when launching with Firefox. * {@link https://developer.mozilla.org/en-US/docs/Mozilla/Preferences/Preference_reference | Additional preferences } that can be passed when launching with Firefox.
*/ */
extraPrefsFirefox?: Record<string, unknown>; extraPrefsFirefox?: Record<string, unknown>;
/**
* Whether to wait for the initial page to be ready.
* Useful when a user explicitly disables that (e.g. `--no-startup-window` for Chrome).
* @defaultValue true
*/
waitForInitialPage?: boolean;
} }
/** /**

View File

@ -75,6 +75,7 @@ class ChromeLauncher implements ProductLauncher {
defaultViewport = { width: 800, height: 600 }, defaultViewport = { width: 800, height: 600 },
slowMo = 0, slowMo = 0,
timeout = 30000, timeout = 30000,
waitForInitialPage = true,
} = options; } = options;
const profilePath = path.join(os.tmpdir(), 'puppeteer_dev_chrome_profile-'); const profilePath = path.join(os.tmpdir(), 'puppeteer_dev_chrome_profile-');
@ -147,6 +148,7 @@ class ChromeLauncher implements ProductLauncher {
runner.proc, runner.proc,
runner.close.bind(runner) runner.close.bind(runner)
); );
if (waitForInitialPage)
await browser.waitForTarget((t) => t.type() === 'page'); await browser.waitForTarget((t) => t.type() === 'page');
return browser; return browser;
} catch (error) { } catch (error) {
@ -245,6 +247,7 @@ class FirefoxLauncher implements ProductLauncher {
slowMo = 0, slowMo = 0,
timeout = 30000, timeout = 30000,
extraPrefsFirefox = {}, extraPrefsFirefox = {},
waitForInitialPage = true,
} = options; } = options;
const firefoxArguments = []; const firefoxArguments = [];
@ -313,6 +316,7 @@ class FirefoxLauncher implements ProductLauncher {
runner.proc, runner.proc,
runner.close.bind(runner) runner.close.bind(runner)
); );
if (waitForInitialPage)
await browser.waitForTarget((t) => t.type() === 'page'); await browser.waitForTarget((t) => t.type() === 'page');
return browser; return browser;
} catch (error) { } catch (error) {

View File

@ -21,6 +21,7 @@ import { promisify } from 'util';
import Protocol from 'devtools-protocol'; import Protocol from 'devtools-protocol';
import { import {
getTestState, getTestState,
itChromeOnly,
itFailsFirefox, itFailsFirefox,
itOnlyRegularInstall, itOnlyRegularInstall,
} from './mocha-utils'; // eslint-disable-line import/extensions } from './mocha-utils'; // eslint-disable-line import/extensions
@ -430,6 +431,24 @@ describe('Launcher specs', function () {
expect(screenshot).toBeInstanceOf(Buffer); expect(screenshot).toBeInstanceOf(Buffer);
await browser.close(); await browser.close();
}); });
itChromeOnly(
'should launch Chrome properly with --no-startup-window and waitForInitialPage=false',
async () => {
const { defaultBrowserOptions, puppeteer } = getTestState();
const options = {
args: ['--no-startup-window'],
waitForInitialPage: false,
// This is needed to prevent Puppeteer from adding an initial blank page.
// See also https://github.com/puppeteer/puppeteer/blob/ad6b736039436fcc5c0a262e5b575aa041427be3/src/node/Launcher.ts#L200
ignoreDefaultArgs: true,
...defaultBrowserOptions,
};
const browser = await puppeteer.launch(options);
const pages = await browser.pages();
expect(pages.length).toBe(0);
await browser.close();
}
);
}); });
describe('Puppeteer.launch', function () { describe('Puppeteer.launch', function () {