Skip to content

Commit

Permalink
feat: expose public interface for locating Chrome installations (#177)
Browse files Browse the repository at this point in the history
Add Launcher.getInstallations() public static method that returns an
array of paths to available Chrome binaries. Also, refactor
Launcher#launch() to use it.

Launcher.getInstallations() uses chrome-finder under the hood, so all
previous resolution logic is preserved:
- if CHROME_PATH env variable is set, it will be included
- if a Chrome Canary is detected, it will be included
- if a Chrome (stable) is detected, it will be included

Resolves #176
  • Loading branch information
knksmith57 authored and patrickhulce committed Oct 25, 2019
1 parent a5ccaa4 commit c4890ee
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 1 deletion.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,12 @@ Returns an `Array<string>` of the default [flags](docs/chrome-flags-for-tools.md

Note: This array will exclude the following flags: `--remote-debugging-port` `--disable-setuid-sandbox` `--user-data-dir`.

### `ChromeLauncher.getInstallations()`

Returns an `Array<string>` of paths to available Chrome installations. When `chromePath` is not provided to `.launch()`, the first installation returned from this method is used instead.

Note: This method performs synchronous I/O operations.

## Examples

#### Launching chrome:
Expand Down
6 changes: 5 additions & 1 deletion src/chrome-launcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,10 @@ class Launcher {
return DEFAULT_FLAGS.slice();
}

static getInstallations() {
return chromeFinder[getPlatform() as SupportedPlatforms]();
}

// Wrapper function to enable easy testing.
makeTmpDir() {
return makeTmpDir();
Expand Down Expand Up @@ -205,7 +209,7 @@ class Launcher {
}
}
if (this.chromePath === undefined) {
const installations = await chromeFinder[getPlatform() as SupportedPlatforms]();
const installations = Launcher.getInstallations();
if (installations.length === 0) {
throw new ChromeNotInstalledError();
}
Expand Down
6 changes: 6 additions & 0 deletions test/chrome-launcher-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,12 @@ describe('Launcher', () => {
assert.ok(!chromeFlags.includes('--disable-extensions'));
});

it('searches for available installations', async () => {
const installations = Launcher.getInstallations();
assert.ok(Array.isArray(installations));
assert.ok(installations.length >= 1);
});

it('removes --user-data-dir if userDataDir is false', async () => {
const spawnStub = await launchChromeWithOpts();
const chromeFlags = spawnStub.getCall(0).args[1] as string[];
Expand Down

0 comments on commit c4890ee

Please sign in to comment.