From b3e61503bc120d0090bb30865155dd2231ca98e2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ant=C3=B3n=20Molleda?= Date: Mon, 13 Nov 2017 20:36:40 -0800 Subject: [PATCH] New: Add flag to use default user profile instead Fix #47 --- README.md | 4 ++-- chrome-launcher.ts | 28 +++++++++++++++++++++------- test/chrome-launcher-test.ts | 19 +++++++++++++++++++ 3 files changed, 42 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index 53dff49ec..fe905d516 100644 --- a/README.md +++ b/README.md @@ -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` diff --git a/chrome-launcher.ts b/chrome-launcher.ts index d922eb1df..0fbde6700 100644 --- a/chrome-launcher.ts +++ b/chrome-launcher.ts @@ -32,7 +32,7 @@ export interface Options { port?: number; handleSIGINT?: boolean; chromePath?: string; - userDataDir?: string; + userDataDir?: string|boolean; logLevel?: string; enableExtensions?: boolean; connectionPollInterval?: number; @@ -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; @@ -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'); @@ -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'); diff --git a/test/chrome-launcher-test.ts b/test/chrome-launcher-test.ts index 4e5692626..98991dae6 100644 --- a/test/chrome-launcher-test.ts +++ b/test/chrome-launcher-test.ts @@ -123,6 +123,25 @@ 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());