Skip to content
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
3 changes: 2 additions & 1 deletion docs/api.md
Original file line number Diff line number Diff line change
@@ -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 v<!-- GEN:version -->0.10.2-alpha<!-- GEN:stop-->

Expand Down Expand Up @@ -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.
Expand Down
14 changes: 9 additions & 5 deletions lib/Launcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -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);
Expand Down
19 changes: 19 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down