Skip to content

Commit

Permalink
feat(launcher): add new launcher option waitForInitialPage (#7105)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
starrify committed May 6, 2021
1 parent c9978d2 commit 2605309
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 2 deletions.
1 change: 1 addition & 0 deletions docs/api.md
Expand Up @@ -625,6 +625,7 @@ try {
- `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`)
- `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.

You can use `ignoreDefaultArgs` to filter out `--mute-audio` from default arguments:
Expand Down
6 changes: 6 additions & 0 deletions src/node/LaunchOptions.ts
Expand Up @@ -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.
*/
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;
}

/**
Expand Down
8 changes: 6 additions & 2 deletions src/node/Launcher.ts
Expand Up @@ -75,6 +75,7 @@ class ChromeLauncher implements ProductLauncher {
defaultViewport = { width: 800, height: 600 },
slowMo = 0,
timeout = 30000,
waitForInitialPage = true,
} = options;

const profilePath = path.join(os.tmpdir(), 'puppeteer_dev_chrome_profile-');
Expand Down Expand Up @@ -147,7 +148,8 @@ class ChromeLauncher implements ProductLauncher {
runner.proc,
runner.close.bind(runner)
);
await browser.waitForTarget((t) => t.type() === 'page');
if (waitForInitialPage)
await browser.waitForTarget((t) => t.type() === 'page');
return browser;
} catch (error) {
runner.kill();
Expand Down Expand Up @@ -245,6 +247,7 @@ class FirefoxLauncher implements ProductLauncher {
slowMo = 0,
timeout = 30000,
extraPrefsFirefox = {},
waitForInitialPage = true,
} = options;

const firefoxArguments = [];
Expand Down Expand Up @@ -313,7 +316,8 @@ class FirefoxLauncher implements ProductLauncher {
runner.proc,
runner.close.bind(runner)
);
await browser.waitForTarget((t) => t.type() === 'page');
if (waitForInitialPage)
await browser.waitForTarget((t) => t.type() === 'page');
return browser;
} catch (error) {
runner.kill();
Expand Down
19 changes: 19 additions & 0 deletions test/launcher.spec.ts
Expand Up @@ -21,6 +21,7 @@ import { promisify } from 'util';
import Protocol from 'devtools-protocol';
import {
getTestState,
itChromeOnly,
itFailsFirefox,
itOnlyRegularInstall,
} from './mocha-utils'; // eslint-disable-line import/extensions
Expand Down Expand Up @@ -430,6 +431,24 @@ describe('Launcher specs', function () {
expect(screenshot).toBeInstanceOf(Buffer);
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 () {
Expand Down

0 comments on commit 2605309

Please sign in to comment.