Skip to content

Commit

Permalink
New: Add flag to use default user profile instead
Browse files Browse the repository at this point in the history
Fix GoogleChrome#47

	random-port.js
  • Loading branch information
molant committed Nov 14, 2017
1 parent b08cd10 commit fe5d8fb
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 9 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,9 @@ npm install chrome-launcher
// * Otherwise, a detected Chrome (stable) will be used
chromePath: string;

// (optional) Chrome profile path to use
// (optional) Chrome profile path to use, if set to `false` then the default profile will be used.
// By default, a fresh Chrome profile will be created
userDataDir: string;
userDataDir: string | boolean;

// (optional) Starting URL to open the browser with
// Default: `about:blank`
Expand Down
28 changes: 21 additions & 7 deletions chrome-launcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export interface Options {
port?: number;
handleSIGINT?: boolean;
chromePath?: string;
userDataDir?: string;
userDataDir?: string|boolean;
logLevel?: string;
enableExtensions?: boolean;
connectionPollInterval?: number;
Expand Down Expand Up @@ -98,6 +98,7 @@ export class Launcher {
private fs: typeof fs;
private rimraf: typeof rimraf;
private spawn: typeof childProcess.spawn;
private useDefaultProfile: boolean;

userDataDir?: string;
port?: number;
Expand All @@ -118,15 +119,28 @@ export class Launcher {
this.enableExtensions = defaults(this.opts.enableExtensions, false);
this.connectionPollInterval = defaults(this.opts.connectionPollInterval, 500);
this.maxConnectionRetries = defaults(this.opts.maxConnectionRetries, 50);

if (typeof this.opts.userDataDir === 'boolean') {
if (!this.opts.userDataDir) {
this.useDefaultProfile = true;
this.userDataDir = undefined;
} else {
throw new Error('userDataDir must be false or a path');
}
} else {
this.useDefaultProfile = false;
this.userDataDir = this.opts.userDataDir;
}
}

private get flags() {
let flags = DEFAULT_FLAGS.concat([
`--remote-debugging-port=${this.port}`,
// Place Chrome profile in a custom location we'll rm -rf later
let flags = DEFAULT_FLAGS.concat([`--remote-debugging-port=${this.port}`]);

// Place Chrome profile in a custom location we'll rm -rf later
if (!this.useDefaultProfile) {
// If in WSL, we need to use the Windows format
`--user-data-dir=${isWsl ? toWinDirFormat(this.userDataDir) : this.userDataDir}`
]);
flags.push(`--user-data-dir=${isWsl ? toWinDirFormat(this.userDataDir) : this.userDataDir}`);
}

if (this.enableExtensions) {
flags = flags.filter(flag => flag !== '--disable-extensions');
Expand All @@ -153,7 +167,7 @@ export class Launcher {
throw new Error(`Platform ${platform} is not supported`);
}

this.userDataDir = this.opts.userDataDir || this.makeTmpDir();
this.userDataDir = this.userDataDir || this.makeTmpDir();
this.outFile = this.fs.openSync(`${this.userDataDir}/chrome-out.log`, 'a');
this.errFile = this.fs.openSync(`${this.userDataDir}/chrome-err.log`, 'a');

Expand Down
20 changes: 20 additions & 0 deletions test/chrome-launcher-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,26 @@ describe('Launcher', () => {
assert.ok(!chromeFlags.includes('--disable-extensions'));
});

it('removes --user-data-dir if userDataDir is false', async () => {
const spawnStub = stub().returns({pid: 'some_pid'});

const chromeInstance = new Launcher(
{userDataDir: false},
{fs: fsMock as any, rimraf: spy() as any, spawn: spawnStub as any});
stub(chromeInstance, 'waitUntilReady').returns(Promise.resolve());

chromeInstance.prepare();

try {
await chromeInstance.launch();
} catch (err) {
return Promise.reject(err);
}

const chromeFlags = spawnStub.getCall(0).args[1] as string[];
assert.ok(!chromeFlags.includes('--user-data-dir'));
});

it('throws an error when chromePath is empty', (done) => {
const chromeInstance = new Launcher({chromePath: ''});
chromeInstance.launch().catch(() => done());
Expand Down

0 comments on commit fe5d8fb

Please sign in to comment.