feat(Launcher): allow the default arguments to be overridden (#1623)

This patch:
- adds `puppeteer.defaultArgs()` method to get default arguments that are used to launch chrome
- adds `ignoreDefaultArgs` option to `puppeteer.launch` to avoid using default puppeteer arguments

Fixes #872
This commit is contained in:
JoelEinbinder 2017-12-19 17:51:21 -08:00 committed by Andrey Lushnikov
parent f8040cb2a2
commit 8a40cd5eef
4 changed files with 30 additions and 4 deletions

View File

@ -10,6 +10,7 @@
- [Environment Variables](#environment-variables)
- [class: Puppeteer](#class-puppeteer)
* [puppeteer.connect(options)](#puppeteerconnectoptions)
* [puppeteer.defaultArgs()](#puppeteerdefaultargs)
* [puppeteer.executablePath()](#puppeteerexecutablepath)
* [puppeteer.launch([options])](#puppeteerlaunchoptions)
- [class: Browser](#class-browser)
@ -246,6 +247,9 @@ puppeteer.launch().then(async browser => {
This methods attaches Puppeteer to an existing Chromium instance.
#### puppeteer.defaultArgs()
- returns: <[Array]<[string]>> The default flags that Chromium will be launched with.
#### puppeteer.executablePath()
- returns: <[string]> A path where Puppeteer expects to find bundled Chromium. Chromium might not exist there if the download was skipped with [`PUPPETEER_SKIP_CHROMIUM_DOWNLOAD`](#environment-variables).
@ -256,6 +260,7 @@ This methods attaches Puppeteer to an existing Chromium instance.
- `executablePath` <[string]> Path to a Chromium or Chrome executable to run instead of bundled Chromium. If `executablePath` is a relative path, then it is resolved relative to [current working directory](https://nodejs.org/api/process.html#process_process_cwd).
- `slowMo` <[number]> Slows down Puppeteer operations by the specified amount of milliseconds. Useful so that you can see what is going on.
- `args` <[Array]<[string]>> Additional arguments to pass to the browser instance. List of Chromium flags can be found [here](http://peter.sh/experiments/chromium-command-line-switches/).
- `ignoreDefaultArgs` <[boolean]> Do not use [`puppeteer.defaultArgs()`](#puppeteerdefaultargs). Dangerous option; use with care. Defaults to `false`.
- `handleSIGINT` <[boolean]> Close browser process on Ctrl-C. Defaults to `true`.
- `handleSIGTERM` <[boolean]> Close browser process on SIGTERM. Defaults to `true`.
- `handleSIGHUP` <[boolean]> Close browser process on SIGHUP. Defaults to `true`.
@ -269,12 +274,12 @@ This methods attaches Puppeteer to an existing Chromium instance.
The method launches a browser instance with given arguments. The browser will be closed when the parent node.js process is closed.
> **NOTE** Puppeteer can also be used to control the Chrome browser, but it works best with the version of Chromium it is bundled with. There is no
guarantee it will work with any other version. Use `executablePath` option with extreme caution.
guarantee it will work with any other version. Use `executablePath` option with extreme caution.
If Google Chrome (rather than Chromium) is preferred, a [Chrome Canary](https://www.google.com/chrome/browser/canary.html) or [Dev Channel](https://www.chromium.org/getting-involved/dev-channel) build is suggested.
>
> In [puppeteer.launch([options])](#puppeteerlaunchoptions) above, any mention of Chromium also applies to Chrome.
>
> See [`this article`](https://www.howtogeek.com/202825/what%E2%80%99s-the-difference-between-chromium-and-chrome/) for a description
> See [`this article`](https://www.howtogeek.com/202825/what%E2%80%99s-the-difference-between-chromium-and-chrome/) for a description
of the differences between Chromium and Chrome. [`This article`](https://chromium.googlesource.com/chromium/src/+/lkcr/docs/chromium_browser_vs_google_chrome.md) describes some differences for Linux users.
### class: Browser

View File

@ -61,10 +61,13 @@ class Launcher {
static async launch(options) {
options = Object.assign({}, options || {});
let temporaryUserDataDir = null;
const chromeArguments = [].concat(DEFAULT_ARGS);
const chromeArguments = [];
if (!options.ignoreDefaultArgs)
chromeArguments.push(...DEFAULT_ARGS);
if (options.appMode)
options.headless = false;
else
else if (!options.ignoreDefaultArgs)
chromeArguments.push(...AUTOMATION_ARGS);
if (!options.args || !options.args.some(arg => arg.startsWith('--user-data-dir'))) {
@ -172,6 +175,13 @@ class Launcher {
}
}
/**
* @return {!Array<string>}
*/
static defaultArgs() {
return DEFAULT_ARGS.concat(AUTOMATION_ARGS);
}
/**
* @return {string}
*/

View File

@ -39,6 +39,13 @@ class Puppeteer {
static executablePath() {
return Launcher.executablePath();
}
/**
* @return {!Array<string>}
*/
static defaultArgs() {
return Launcher.defaultArgs();
}
}
module.exports = Puppeteer;

View File

@ -202,6 +202,10 @@ describe('Puppeteer', function() {
rm(userDataDir);
expect(cookie).toBe('foo=true');
});
it('should return the default chrome arguments', async() => {
const args = puppeteer.defaultArgs();
expect(args).toContain('--no-first-run');
});
});
describe('Puppeteer.connect', function() {
it('should be able to connect multiple times to the same browser', async({server}) => {