[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:
parent
66912a7277
commit
d5327e6a0f
@ -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.
|
||||
|
@ -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);
|
||||
|
19
test/test.js
19
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() {
|
||||
|
Loading…
Reference in New Issue
Block a user