From d5327e6a0fd384cd36bfd0b4a12b27ab362543a7 Mon Sep 17 00:00:00 2001 From: JoelEinbinder Date: Mon, 28 Aug 2017 12:14:21 -0700 Subject: [PATCH] [api] Allow a custom userDataDir to be specified (#555) This patch adds a `userDataDir` option to the `puppeteer.launch()` method. Fixes #411. --- docs/api.md | 3 ++- lib/Launcher.js | 14 +++++++++----- test/test.js | 19 +++++++++++++++++++ 3 files changed, 30 insertions(+), 6 deletions(-) diff --git a/docs/api.md b/docs/api.md index 6d9653e8..c5728049 100644 --- a/docs/api.md +++ b/docs/api.md @@ -1,4 +1,4 @@ -##### Released API: [v0.10.1](https://github.com/GoogleChrome/puppeteer/blob/v0.10.1/docs/api.md) | [v0.10.0](https://github.com/GoogleChrome/puppeteer/blob/v0.10.0/docs/api.md) | [v0.9.0](https://github.com/GoogleChrome/puppeteer/blob/v0.9.0/docs/api.md) +##### Released API: [v0.10.1](https://github.com/GoogleChrome/puppeteer/blob/v0.10.1/docs/api.md) | [v0.10.0](https://github.com/GoogleChrome/puppeteer/blob/v0.10.0/docs/api.md) | [v0.9.0](https://github.com/GoogleChrome/puppeteer/blob/v0.9.0/docs/api.md) # Puppeteer API v0.10.2-alpha @@ -169,6 +169,7 @@ This methods attaches Puppeteer to an existing Chromium instance. - `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. - `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. The method launches a browser instance with given arguments. The browser will be closed when the parent node.js process is closed. diff --git a/lib/Launcher.js b/lib/Launcher.js index 16b159f2..49f5e706 100644 --- a/lib/Launcher.js +++ b/lib/Launcher.js @@ -53,11 +53,14 @@ class Launcher { */ static async launch(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([ - `--user-data-dir=${userDataDir}`, - ]); + chromeArguments.push(`--user-data-dir=${options.userDataDir || temporaryUserDataDir}`); + } if (typeof options.headless !== 'boolean' || options.headless) { chromeArguments.push( '--headless', @@ -84,7 +87,8 @@ class Launcher { // Cleanup as processes exit. let killed = false; process.once('exit', killChrome); - chromeProcess.once('close', () => removeSync(userDataDir)); + if (temporaryUserDataDir) + chromeProcess.once('close', () => removeSync(temporaryUserDataDir)); if (options.handleSIGINT !== false) process.once('SIGINT', killChrome); diff --git a/test/test.js b/test/test.js index 69b979c7..6881700b 100644 --- a/test/test.js +++ b/test/test.js @@ -115,6 +115,25 @@ describe('Browser', function() { expect(await page.evaluate(() => 7 * 8)).toBe(56); 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() {