Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add custom debugging port option #4993

Merged
merged 5 commits into from Sep 29, 2021
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions docs/api.md
Expand Up @@ -568,6 +568,7 @@ This methods attaches Puppeteer to an existing browser instance.
- `args` <[Array]<[string]>> Additional arguments to pass to the browser instance. The list of Chromium flags can be found [here](http://peter.sh/experiments/chromium-command-line-switches/).
- `userDataDir` <[string]> Path to a [User Data Directory](https://chromium.googlesource.com/chromium/src/+/refs/heads/main/docs/user_data_dir.md).
- `devtools` <[boolean]> Whether to auto-open a DevTools panel for each tab. If this option is `true`, the `headless` option will be set `false`.
- `debuggingPort` <[number]> Specify custom debugging port. Pass `0` to discover a random port. Defaults to `0`.
- returns: <[Array]<[string]>>

The default flags that Chromium will be launched with.
Expand Down Expand Up @@ -648,6 +649,7 @@ try {
- `timeout` <[number]> Maximum time in milliseconds to wait for the browser instance to start. Defaults to `30000` (30 seconds). Pass `0` to disable timeout.
- `dumpio` <[boolean]> Whether to pipe the browser process stdout and stderr into `process.stdout` and `process.stderr`. Defaults to `false`.
- `userDataDir` <[string]> Path to a [User Data Directory](https://chromium.googlesource.com/chromium/src/+/refs/heads/main/docs/user_data_dir.md).
- `debuggingPort` <[number]> Specify custom debugging port. Pass `0` to discover a random port. Defaults to `0`.
- `env` <[Object]> Specify environment variables that will be visible to the browser. Defaults to `process.env`.
- `devtools` <[boolean]> Whether to auto-open a DevTools panel for each tab. If this option is `true`, the `headless` option will be set `false`.
- `pipe` <[boolean]> Connects to the browser over a pipe instead of a WebSocket. Defaults to `false`.
Expand Down
4 changes: 4 additions & 0 deletions src/node/LaunchOptions.ts
Expand Up @@ -40,6 +40,10 @@ export interface BrowserLaunchArgumentOptions {
* @defaultValue `false`
*/
devtools?: boolean;
/**
*
*/
debuggingPort?: number;
/**
* Additional command line arguments to pass to the browser instance.
*/
Expand Down
19 changes: 14 additions & 5 deletions src/node/Launcher.ts
Expand Up @@ -82,6 +82,7 @@ class ChromeLauncher implements ProductLauncher {
slowMo = 0,
timeout = 30000,
waitForInitialPage = true,
debuggingPort = null,
} = options;

const profilePath = path.join(tmpDir(), 'puppeteer_dev_chrome_profile-');
Expand All @@ -101,10 +102,17 @@ class ChromeLauncher implements ProductLauncher {
!chromeArguments.some((argument) =>
argument.startsWith('--remote-debugging-')
)
)
chromeArguments.push(
pipe ? '--remote-debugging-pipe' : '--remote-debugging-port=0'
);
) {
if (pipe) {
assert(
debuggingPort === null,
'Browser should be launched with either pipe or debugging port - not both.'
);
chromeArguments.push('--remote-debugging-pipe');
} else {
chromeArguments.push(`--remote-debugging-port=${debuggingPort || 0}`);
}
}
if (!chromeArguments.some((arg) => arg.startsWith('--user-data-dir'))) {
temporaryUserDataDir = await mkdtempAsync(profilePath);
chromeArguments.push(`--user-data-dir=${temporaryUserDataDir}`);
Expand Down Expand Up @@ -267,6 +275,7 @@ class FirefoxLauncher implements ProductLauncher {
timeout = 30000,
extraPrefsFirefox = {},
waitForInitialPage = true,
debuggingPort = null,
} = options;

const firefoxArguments = [];
Expand All @@ -284,7 +293,7 @@ class FirefoxLauncher implements ProductLauncher {
argument.startsWith('--remote-debugging-')
)
)
firefoxArguments.push('--remote-debugging-port=0');
firefoxArguments.push(`--remote-debugging-port=${debuggingPort || 0}`);

let temporaryUserDataDir = null;

Expand Down
25 changes: 25 additions & 0 deletions test/launcher.spec.ts
Expand Up @@ -440,6 +440,31 @@ describe('Launcher specs', function () {
expect(screenshot).toBeInstanceOf(Buffer);
await browser.close();
});
it('should set the debugging port', async () => {
const { puppeteer, defaultBrowserOptions } = getTestState();

const options = Object.assign({}, defaultBrowserOptions, {
defaultViewport: null,
debuggingPort: 9999,
});
const browser = await puppeteer.launch(options);
const url = new URL(browser.wsEndpoint());
await browser.close();
expect(url.port).toBe('9999');
});
it('should not allow setting debuggingPort and pipe', async () => {
const { puppeteer, defaultBrowserOptions } = getTestState();

const options = Object.assign({}, defaultBrowserOptions, {
defaultViewport: null,
debuggingPort: 9999,
pipe: true,
});

let error = null;
await puppeteer.launch(options).catch((error_) => (error = error_));
expect(error.message).toContain('either pipe or debugging port');
});
itChromeOnly(
'should launch Chrome properly with --no-startup-window and waitForInitialPage=false',
async () => {
Expand Down