[api] Allow a custom userDataDir to be specified (#555)

This patch adds a `userDataDir` option to the `puppeteer.launch()` method.

Fixes #411.
This commit is contained in:
JoelEinbinder 2017-08-28 12:14:21 -07:00 committed by Andrey Lushnikov
parent 66912a7277
commit d5327e6a0f
3 changed files with 30 additions and 6 deletions

View File

@ -169,6 +169,7 @@ This methods attaches Puppeteer to an existing Chromium instance.
- `handleSIGINT` <[boolean]> Close chrome process on Ctrl-C. Defaults to `true`. - `handleSIGINT` <[boolean]> Close chrome process on Ctrl-C. Defaults to `true`.
- `timeout` <[number]> Maximum time in milliseconds to wait for the Chrome instance to start. Defaults to `30000` (30 seconds). Pass `0` to disable timeout. - `timeout` <[number]> Maximum time in milliseconds to wait for the Chrome instance to start. Defaults to `30000` (30 seconds). Pass `0` to disable timeout.
- `dumpio` <[boolean]> Whether to pipe browser process stdout and stderr into `process.stdout` and `process.stderr`. Defaults to `false`. - `dumpio` <[boolean]> Whether to pipe browser process stdout and stderr into `process.stdout` and `process.stderr`. Defaults to `false`.
- `userDataDir` <[string]> Path to a [User Data Directory](https://chromium.googlesource.com/chromium/src/+/master/docs/user_data_dir.md).
- returns: <[Promise]<[Browser]>> Promise which resolves to browser instance. - returns: <[Promise]<[Browser]>> Promise which resolves to browser instance.
The method launches a browser instance with given arguments. The browser will be closed when the parent node.js process is closed. The method launches a browser instance with given arguments. The browser will be closed when the parent node.js process is closed.

View File

@ -53,11 +53,14 @@ class Launcher {
*/ */
static async launch(options) { static async launch(options) {
options = options || {}; options = options || {};
const userDataDir = fs.mkdtempSync(CHROME_PROFILE_PATH); let temporaryUserDataDir = null;
const chromeArguments = [].concat(DEFAULT_ARGS);
if (!options.args.some(arg => arg.startsWith('--user-data-dir'))) {
if (!options.userDataDir)
temporaryUserDataDir = fs.mkdtempSync(CHROME_PROFILE_PATH);
const chromeArguments = DEFAULT_ARGS.concat([ chromeArguments.push(`--user-data-dir=${options.userDataDir || temporaryUserDataDir}`);
`--user-data-dir=${userDataDir}`, }
]);
if (typeof options.headless !== 'boolean' || options.headless) { if (typeof options.headless !== 'boolean' || options.headless) {
chromeArguments.push( chromeArguments.push(
'--headless', '--headless',
@ -84,7 +87,8 @@ class Launcher {
// Cleanup as processes exit. // Cleanup as processes exit.
let killed = false; let killed = false;
process.once('exit', killChrome); process.once('exit', killChrome);
chromeProcess.once('close', () => removeSync(userDataDir)); if (temporaryUserDataDir)
chromeProcess.once('close', () => removeSync(temporaryUserDataDir));
if (options.handleSIGINT !== false) if (options.handleSIGINT !== false)
process.once('SIGINT', killChrome); process.once('SIGINT', killChrome);

View File

@ -115,6 +115,25 @@ describe('Browser', function() {
expect(await page.evaluate(() => 7 * 8)).toBe(56); expect(await page.evaluate(() => 7 * 8)).toBe(56);
originalBrowser.close(); originalBrowser.close();
})); }));
it('userDataDir option', SX(async function() {
const userDataDir = fs.mkdtempSync(path.join(__dirname, 'test-user-data-dir'));
const options = Object.assign({userDataDir}, defaultBrowserOptions);
const browser = await puppeteer.launch(options);
expect(fs.readdirSync(userDataDir).length).toBeGreaterThan(0);
browser.close();
expect(fs.readdirSync(userDataDir).length).toBeGreaterThan(0);
rm(userDataDir);
}));
it('userDataDir argument', SX(async function() {
const userDataDir = fs.mkdtempSync(path.join(__dirname, 'test-user-data-dir'));
const options = Object.assign({}, defaultBrowserOptions);
options.args = [`--user-data-dir=${userDataDir}`].concat(options.args);
const browser = await puppeteer.launch(options);
expect(fs.readdirSync(userDataDir).length).toBeGreaterThan(0);
browser.close();
expect(fs.readdirSync(userDataDir).length).toBeGreaterThan(0);
rm(userDataDir);
}));
}); });
describe('Page', function() { describe('Page', function() {