Skip to content

Commit

Permalink
add failIfNoChromeAtPort option
Browse files Browse the repository at this point in the history
  • Loading branch information
connorjclark committed Sep 29, 2023
1 parent 55020d3 commit 19630ea
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 0 deletions.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,13 @@ npm install chrome-launcher
// Default: an available port is autoselected
port: number;

// (optional) When a port is specified but no Chrome is found at that port:
// * if this option is true and there is no Chrome with that debug port, throw an error
// * else, launch a new Chrome with that debug port
// This option is useful when you wish to explicitly connect to a running Chrome, such as on a mobile device via adb
// Default: false
failIfNoChromeAtPort: boolean;

// (optional) Additional flags to pass to Chrome, for example: ['--headless', '--disable-gpu']
// See: https://github.com/GoogleChrome/chrome-launcher/blob/main/docs/chrome-flags-for-tools.md
// Do note, many flags are set by default: https://github.com/GoogleChrome/chrome-launcher/blob/main/src/flags.ts
Expand Down
7 changes: 7 additions & 0 deletions src/chrome-launcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ export interface Options {
chromeFlags?: Array<string>;
prefs?: Record<string, JSONLike>;
port?: number;
failIfNoChromeAtPort?: boolean;
handleSIGINT?: boolean;
chromePath?: string;
userDataDir?: string|boolean;
Expand Down Expand Up @@ -119,6 +120,7 @@ class Launcher {
private chromeFlags: string[];
private prefs: Record<string, JSONLike>;
private requestedPort?: number;
private failIfNoChromeAtPort?: boolean;
private connectionPollInterval: number;
private maxConnectionRetries: number;
private fs: typeof fs;
Expand All @@ -142,6 +144,7 @@ class Launcher {
this.chromeFlags = defaults(this.opts.chromeFlags, []);
this.prefs = defaults(this.opts.prefs, {});
this.requestedPort = defaults(this.opts.port, 0);
this.failIfNoChromeAtPort = opts.failIfNoChromeAtPort;
this.chromePath = this.opts.chromePath;
this.ignoreDefaultFlags = defaults(this.opts.ignoreDefaultFlags, false);
this.connectionPollInterval = defaults(this.opts.connectionPollInterval, 500);
Expand Down Expand Up @@ -263,6 +266,10 @@ class Launcher {
`Found existing Chrome already running using port ${this.port}, using that.`);
return;
} catch (err) {
if (this.failIfNoChromeAtPort) {
throw new Error(`found no Chrome at port ${this.requestedPort}`);
}

log.log(
'ChromeLauncher',
`No debugging port found on port ${this.port}, launching a new Chrome.`);
Expand Down

0 comments on commit 19630ea

Please sign in to comment.