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

New: Add flag to use default user profile instead #48

Merged
merged 1 commit into from
Nov 27, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
19 changes: 19 additions & 0 deletions test/chrome-launcher-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down