diff --git a/new-docs/puppeteer.md b/new-docs/puppeteer.md index e2017785165..38c4bad59be 100644 --- a/new-docs/puppeteer.md +++ b/new-docs/puppeteer.md @@ -29,7 +29,7 @@ | [Keyboard](./puppeteer.keyboard.md) | | | [Mouse](./puppeteer.mouse.md) | The Mouse class operates in main-frame CSS pixels relative to the top-left corner of the viewport. | | [Page](./puppeteer.page.md) | Page provides methods to interact with a single tab or \[extension background page\](https://developer.chrome.com/extensions/background\_pages) in Chromium. One \[Browser\] instance might have multiple \[Page\] instances. | -| [Puppeteer](./puppeteer.puppeteer.md) | The main Puppeteer class | +| [Puppeteer](./puppeteer.puppeteer.md) | The main Puppeteer class Puppeteer module provides a method to launch a browser instance. | | [SecurityDetails](./puppeteer.securitydetails.md) | The SecurityDetails class represents the security details of a response that was received over a secure connection. | | [Target](./puppeteer.target.md) | | | [TimeoutError](./puppeteer.timeouterror.md) | TimeoutError is emitted whenever certain operations are terminated due to timeout. | diff --git a/new-docs/puppeteer.puppeteer.__experimental_clearqueryhandlers.md b/new-docs/puppeteer.puppeteer.__experimental_clearqueryhandlers.md deleted file mode 100644 index 472cda91f80..00000000000 --- a/new-docs/puppeteer.puppeteer.__experimental_clearqueryhandlers.md +++ /dev/null @@ -1,15 +0,0 @@ - - -[Home](./index.md) > [puppeteer](./puppeteer.md) > [Puppeteer](./puppeteer.puppeteer.md) > [\_\_experimental\_clearQueryHandlers](./puppeteer.puppeteer.__experimental_clearqueryhandlers.md) - -## Puppeteer.\_\_experimental\_clearQueryHandlers() method - -Signature: - -```typescript -__experimental_clearQueryHandlers(): void; -``` -Returns: - -void - diff --git a/new-docs/puppeteer.puppeteer.__experimental_customqueryhandlers.md b/new-docs/puppeteer.puppeteer.__experimental_customqueryhandlers.md deleted file mode 100644 index a0a3a011db2..00000000000 --- a/new-docs/puppeteer.puppeteer.__experimental_customqueryhandlers.md +++ /dev/null @@ -1,15 +0,0 @@ - - -[Home](./index.md) > [puppeteer](./puppeteer.md) > [Puppeteer](./puppeteer.puppeteer.md) > [\_\_experimental\_customQueryHandlers](./puppeteer.puppeteer.__experimental_customqueryhandlers.md) - -## Puppeteer.\_\_experimental\_customQueryHandlers() method - -Signature: - -```typescript -__experimental_customQueryHandlers(): Map; -``` -Returns: - -Map<string, QueryHandler> - diff --git a/new-docs/puppeteer.puppeteer.__experimental_registercustomqueryhandler.md b/new-docs/puppeteer.puppeteer.__experimental_registercustomqueryhandler.md deleted file mode 100644 index a82909a0050..00000000000 --- a/new-docs/puppeteer.puppeteer.__experimental_registercustomqueryhandler.md +++ /dev/null @@ -1,23 +0,0 @@ - - -[Home](./index.md) > [puppeteer](./puppeteer.md) > [Puppeteer](./puppeteer.puppeteer.md) > [\_\_experimental\_registerCustomQueryHandler](./puppeteer.puppeteer.__experimental_registercustomqueryhandler.md) - -## Puppeteer.\_\_experimental\_registerCustomQueryHandler() method - -Signature: - -```typescript -__experimental_registerCustomQueryHandler(name: string, queryHandler: QueryHandler): void; -``` - -## Parameters - -| Parameter | Type | Description | -| --- | --- | --- | -| name | string | | -| queryHandler | QueryHandler | | - -Returns: - -void - diff --git a/new-docs/puppeteer.puppeteer.__experimental_unregistercustomqueryhandler.md b/new-docs/puppeteer.puppeteer.__experimental_unregistercustomqueryhandler.md deleted file mode 100644 index 84995244327..00000000000 --- a/new-docs/puppeteer.puppeteer.__experimental_unregistercustomqueryhandler.md +++ /dev/null @@ -1,22 +0,0 @@ - - -[Home](./index.md) > [puppeteer](./puppeteer.md) > [Puppeteer](./puppeteer.puppeteer.md) > [\_\_experimental\_unregisterCustomQueryHandler](./puppeteer.puppeteer.__experimental_unregistercustomqueryhandler.md) - -## Puppeteer.\_\_experimental\_unregisterCustomQueryHandler() method - -Signature: - -```typescript -__experimental_unregisterCustomQueryHandler(name: string): void; -``` - -## Parameters - -| Parameter | Type | Description | -| --- | --- | --- | -| name | string | | - -Returns: - -void - diff --git a/new-docs/puppeteer.puppeteer._constructor_.md b/new-docs/puppeteer.puppeteer._constructor_.md deleted file mode 100644 index 1c81db8fe1c..00000000000 --- a/new-docs/puppeteer.puppeteer._constructor_.md +++ /dev/null @@ -1,23 +0,0 @@ - - -[Home](./index.md) > [puppeteer](./puppeteer.md) > [Puppeteer](./puppeteer.puppeteer.md) > [(constructor)](./puppeteer.puppeteer._constructor_.md) - -## Puppeteer.(constructor) - -Constructs a new instance of the `Puppeteer` class - -Signature: - -```typescript -constructor(projectRoot: string, preferredRevision: string, isPuppeteerCore: boolean, productName: string); -``` - -## Parameters - -| Parameter | Type | Description | -| --- | --- | --- | -| projectRoot | string | | -| preferredRevision | string | | -| isPuppeteerCore | boolean | | -| productName | string | | - diff --git a/new-docs/puppeteer.puppeteer._launcher.md b/new-docs/puppeteer.puppeteer._launcher.md deleted file mode 100644 index df1596e2e32..00000000000 --- a/new-docs/puppeteer.puppeteer._launcher.md +++ /dev/null @@ -1,11 +0,0 @@ - - -[Home](./index.md) > [puppeteer](./puppeteer.md) > [Puppeteer](./puppeteer.puppeteer.md) > [\_launcher](./puppeteer.puppeteer._launcher.md) - -## Puppeteer.\_launcher property - -Signature: - -```typescript -get _launcher(): ProductLauncher; -``` diff --git a/new-docs/puppeteer.puppeteer._productname.md b/new-docs/puppeteer.puppeteer._productname.md deleted file mode 100644 index 9c00de81a9d..00000000000 --- a/new-docs/puppeteer.puppeteer._productname.md +++ /dev/null @@ -1,13 +0,0 @@ - - -[Home](./index.md) > [puppeteer](./puppeteer.md) > [Puppeteer](./puppeteer.puppeteer.md) > [\_productName](./puppeteer.puppeteer._productname.md) - -## Puppeteer.\_productName property - -Signature: - -```typescript -get _productName(): string; - -set _productName(name: string); -``` diff --git a/new-docs/puppeteer.puppeteer.connect.md b/new-docs/puppeteer.puppeteer.connect.md index 36baa350a0a..6431e3848f9 100644 --- a/new-docs/puppeteer.puppeteer.connect.md +++ b/new-docs/puppeteer.puppeteer.connect.md @@ -4,6 +4,8 @@ ## Puppeteer.connect() method +This method attaches Puppeteer to an existing browser instance. + Signature: ```typescript @@ -19,9 +21,14 @@ connect(options: BrowserOptions & { | Parameter | Type | Description | | --- | --- | --- | -| options | BrowserOptions & { browserWSEndpoint?: string; browserURL?: string; transport?: ConnectionTransport; product?: string; } | | +| options | BrowserOptions & { browserWSEndpoint?: string; browserURL?: string; transport?: ConnectionTransport; product?: string; } | Set of configurable options to set on the browser. | Returns: Promise<[Browser](./puppeteer.browser.md)> +Promise which resolves to browser instance. + +## Remarks + + diff --git a/new-docs/puppeteer.puppeteer.createbrowserfetcher.md b/new-docs/puppeteer.puppeteer.createbrowserfetcher.md index dffbddfa87d..0aecc97bec7 100644 --- a/new-docs/puppeteer.puppeteer.createbrowserfetcher.md +++ b/new-docs/puppeteer.puppeteer.createbrowserfetcher.md @@ -14,9 +14,11 @@ createBrowserFetcher(options: BrowserFetcherOptions): BrowserFetcher; | Parameter | Type | Description | | --- | --- | --- | -| options | [BrowserFetcherOptions](./puppeteer.browserfetcheroptions.md) | | +| options | [BrowserFetcherOptions](./puppeteer.browserfetcheroptions.md) | Set of configurable options to specify the settings of the BrowserFetcher. | Returns: [BrowserFetcher](./puppeteer.browserfetcher.md) +A new BrowserFetcher instance. + diff --git a/new-docs/puppeteer.puppeteer.defaultargs.md b/new-docs/puppeteer.puppeteer.defaultargs.md index ff0d72ee55c..f7e8cc269df 100644 --- a/new-docs/puppeteer.puppeteer.defaultargs.md +++ b/new-docs/puppeteer.puppeteer.defaultargs.md @@ -14,9 +14,11 @@ defaultArgs(options?: ChromeArgOptions): string[]; | Parameter | Type | Description | | --- | --- | --- | -| options | ChromeArgOptions | | +| options | ChromeArgOptions | Set of configurable options to set on the browser. | Returns: string\[\] +The default flags that Chromium will be launched with. + diff --git a/new-docs/puppeteer.puppeteer.devices.md b/new-docs/puppeteer.puppeteer.devices.md index 82469af5b85..9a5379dc443 100644 --- a/new-docs/puppeteer.puppeteer.devices.md +++ b/new-docs/puppeteer.puppeteer.devices.md @@ -9,3 +9,25 @@ ```typescript get devices(): DevicesMap; ``` + +## Remarks + + +## Example + + +```js +const puppeteer = require('puppeteer'); +const iPhone = puppeteer.devices['iPhone 6']; + +(async () => { + const browser = await puppeteer.launch(); + const page = await browser.newPage(); + await page.emulate(iPhone); + await page.goto('https://www.google.com'); + // other actions... + await browser.close(); +})(); + +``` + diff --git a/new-docs/puppeteer.puppeteer.errors.md b/new-docs/puppeteer.puppeteer.errors.md index a614dbd5b59..f81b4cb4189 100644 --- a/new-docs/puppeteer.puppeteer.errors.md +++ b/new-docs/puppeteer.puppeteer.errors.md @@ -9,3 +9,25 @@ ```typescript get errors(): PuppeteerErrors; ``` + +## Remarks + +Puppeteer methods might throw errors if they are unable to fulfill a request. For example, `page.waitForSelector(selector[, options])` might fail if the selector doesn't match any nodes during the given timeframe. + +For certain types of errors Puppeteer uses specific error classes. These classes are available via `puppeteer.errors` + +## Example + +An example of handling a timeout error: + +```js +try { + await page.waitForSelector('.foo'); +} catch (e) { + if (e instanceof puppeteer.errors.TimeoutError) { + // Do something if this is a timeout. + } +} + +``` + diff --git a/new-docs/puppeteer.puppeteer.executablepath.md b/new-docs/puppeteer.puppeteer.executablepath.md index 7e05dc3a81b..970f70a81d0 100644 --- a/new-docs/puppeteer.puppeteer.executablepath.md +++ b/new-docs/puppeteer.puppeteer.executablepath.md @@ -13,3 +13,9 @@ executablePath(): string; string +A path where Puppeteer expects to find the bundled browser. The browser binary might not be there if the download was skipped with the `PUPPETEER_SKIP_DOWNLOAD` environment variable. + +## Remarks + +\*\*NOTE\*\* `puppeteer.executablePath()` is affected by the `PUPPETEER_EXECUTABLE_PATH` and `PUPPETEER_CHROMIUM_REVISION` environment variables. + diff --git a/new-docs/puppeteer.puppeteer.launch.md b/new-docs/puppeteer.puppeteer.launch.md index 6b458e5c9c1..604cca0bc6a 100644 --- a/new-docs/puppeteer.puppeteer.launch.md +++ b/new-docs/puppeteer.puppeteer.launch.md @@ -4,6 +4,8 @@ ## Puppeteer.launch() method +Launches puppeteer and launches a browser instance with given arguments and options when specified. + Signature: ```typescript @@ -17,9 +19,26 @@ launch(options?: LaunchOptions & ChromeArgOptions & BrowserOptions & { | Parameter | Type | Description | | --- | --- | --- | -| options | LaunchOptions & ChromeArgOptions & BrowserOptions & { product?: string; extraPrefsFirefox?: {}; } | | +| options | LaunchOptions & ChromeArgOptions & BrowserOptions & { product?: string; extraPrefsFirefox?: {}; } | Set of configurable options to set on the browser. | Returns: Promise<[Browser](./puppeteer.browser.md)> +Promise which resolves to browser instance. + +## Remarks + + +## Example + +You can use `ignoreDefaultArgs` to filter out `--mute-audio` from default arguments: + +```js +const browser = await puppeteer.launch({ + ignoreDefaultArgs: ['--mute-audio'] +}); + +``` +\*\*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. 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])`, 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 of the differences between Chromium and Chrome. [This article](https://chromium.googlesource.com/chromium/src/+/lkgr/docs/chromium_browser_vs_google_chrome.md) describes some differences for Linux users. + diff --git a/new-docs/puppeteer.puppeteer.md b/new-docs/puppeteer.puppeteer.md index bfbf737dc2e..f27df977800 100644 --- a/new-docs/puppeteer.puppeteer.md +++ b/new-docs/puppeteer.puppeteer.md @@ -4,7 +4,7 @@ ## Puppeteer class -The main Puppeteer class +The main Puppeteer class Puppeteer module provides a method to launch a browser instance. Signature: @@ -12,11 +12,26 @@ The main Puppeteer class export declare class Puppeteer ``` -## Constructors +## Remarks -| Constructor | Modifiers | Description | -| --- | --- | --- | -| [(constructor)(projectRoot, preferredRevision, isPuppeteerCore, productName)](./puppeteer.puppeteer._constructor_.md) | | Constructs a new instance of the Puppeteer class | +The constructor for this class is marked as internal. Third-party code should not call the constructor directly or create subclasses that extend the `Puppeteer` class. + +## Example + +The following is a typical example of using Puppeteer to drive automation: + +```js +const puppeteer = require('puppeteer'); + +(async () => { + const browser = await puppeteer.launch(); + const page = await browser.newPage(); + await page.goto('https://www.google.com'); + // other actions... + await browser.close(); +})(); + +``` ## Properties @@ -25,10 +40,8 @@ export declare class Puppeteer | [\_\_productName](./puppeteer.puppeteer.__productname.md) | | string | | | [\_changedProduct](./puppeteer.puppeteer._changedproduct.md) | | boolean | | | [\_isPuppeteerCore](./puppeteer.puppeteer._ispuppeteercore.md) | | boolean | | -| [\_launcher](./puppeteer.puppeteer._launcher.md) | | ProductLauncher | | | [\_lazyLauncher](./puppeteer.puppeteer._lazylauncher.md) | | ProductLauncher | | | [\_preferredRevision](./puppeteer.puppeteer._preferredrevision.md) | | string | | -| [\_productName](./puppeteer.puppeteer._productname.md) | | string | | | [\_projectRoot](./puppeteer.puppeteer._projectroot.md) | | string | | | [devices](./puppeteer.puppeteer.devices.md) | | DevicesMap | | | [errors](./puppeteer.puppeteer.errors.md) | | [PuppeteerErrors](./puppeteer.puppeteererrors.md) | | @@ -38,13 +51,9 @@ export declare class Puppeteer | Method | Modifiers | Description | | --- | --- | --- | -| [\_\_experimental\_clearQueryHandlers()](./puppeteer.puppeteer.__experimental_clearqueryhandlers.md) | | | -| [\_\_experimental\_customQueryHandlers()](./puppeteer.puppeteer.__experimental_customqueryhandlers.md) | | | -| [\_\_experimental\_registerCustomQueryHandler(name, queryHandler)](./puppeteer.puppeteer.__experimental_registercustomqueryhandler.md) | | | -| [\_\_experimental\_unregisterCustomQueryHandler(name)](./puppeteer.puppeteer.__experimental_unregistercustomqueryhandler.md) | | | -| [connect(options)](./puppeteer.puppeteer.connect.md) | | | +| [connect(options)](./puppeteer.puppeteer.connect.md) | | This method attaches Puppeteer to an existing browser instance. | | [createBrowserFetcher(options)](./puppeteer.puppeteer.createbrowserfetcher.md) | | | | [defaultArgs(options)](./puppeteer.puppeteer.defaultargs.md) | | | | [executablePath()](./puppeteer.puppeteer.executablepath.md) | | | -| [launch(options)](./puppeteer.puppeteer.launch.md) | | | +| [launch(options)](./puppeteer.puppeteer.launch.md) | | Launches puppeteer and launches a browser instance with given arguments and options when specified. | diff --git a/new-docs/puppeteer.puppeteer.product.md b/new-docs/puppeteer.puppeteer.product.md index 93ef07315b4..465211c69b4 100644 --- a/new-docs/puppeteer.puppeteer.product.md +++ b/new-docs/puppeteer.puppeteer.product.md @@ -9,3 +9,8 @@ ```typescript get product(): string; ``` + +## Remarks + +The product is set by the `PUPPETEER_PRODUCT` environment variable or the `product` option in `puppeteer.launch([options])` and defaults to `chrome`. Firefox support is experimental. + diff --git a/src/common/Puppeteer.ts b/src/common/Puppeteer.ts index e81ca0af9a1..5996298cc60 100644 --- a/src/common/Puppeteer.ts +++ b/src/common/Puppeteer.ts @@ -37,6 +37,23 @@ import { /** * The main Puppeteer class + * Puppeteer module provides a method to launch a browser instance. + * + * @remarks + * + * @example + * The following is a typical example of using Puppeteer to drive automation: + * ```js + * const puppeteer = require('puppeteer'); + * + * (async () => { + * const browser = await puppeteer.launch(); + * const page = await browser.newPage(); + * await page.goto('https://www.google.com'); + * // other actions... + * await browser.close(); + * })(); + * ``` * @public */ export class Puppeteer { @@ -47,6 +64,9 @@ export class Puppeteer { __productName: string; _lazyLauncher: ProductLauncher; + /** + * @internal + */ constructor( projectRoot: string, preferredRevision: string, @@ -60,6 +80,31 @@ export class Puppeteer { this.__productName = productName; } + /** + * Launches puppeteer and launches a browser instance with given arguments + * and options when specified. + * + * @remarks + * + * @example + * You can use `ignoreDefaultArgs` to filter out `--mute-audio` from default arguments: + * ```js + * const browser = await puppeteer.launch({ + * ignoreDefaultArgs: ['--mute-audio'] + * }); + * ``` + * + * **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. + * If Google Chrome (rather than Chromium) is preferred, a {@link https://www.google.com/chrome/browser/canary.html | Chrome Canary} or {@link https://www.chromium.org/getting-involved/dev-channel | Dev Channel} build is suggested. + * In `puppeteer.launch([options])`, any mention of Chromium also applies to Chrome. + * See {@link https://www.howtogeek.com/202825/what%E2%80%99s-the-difference-between-chromium-and-chrome/ | this article} for a description of the differences between Chromium and Chrome. {@link https://chromium.googlesource.com/chromium/src/+/lkgr/docs/chromium_browser_vs_google_chrome.md | This article} describes some differences for Linux users. + * + * @param options - Set of configurable options to set on the browser. + * @returns Promise which resolves to browser instance. + */ launch( options: LaunchOptions & ChromeArgOptions & @@ -69,6 +114,14 @@ export class Puppeteer { return this._launcher.launch(options); } + /** + * This method attaches Puppeteer to an existing browser instance. + * + * @remarks + * + * @param options - Set of configurable options to set on the browser. + * @returns Promise which resolves to browser instance. + */ connect( options: BrowserOptions & { browserWSEndpoint?: string; @@ -81,19 +134,38 @@ export class Puppeteer { return this._launcher.connect(options); } + /** + * @internal + */ set _productName(name: string) { if (this.__productName !== name) this._changedProduct = true; this.__productName = name; } + /** + * @internal + */ get _productName(): string { return this.__productName; } + /** + * @remarks + * + * **NOTE** `puppeteer.executablePath()` is affected by the `PUPPETEER_EXECUTABLE_PATH` + * and `PUPPETEER_CHROMIUM_REVISION` environment variables. + * + * @returns A path where Puppeteer expects to find the bundled browser. + * The browser binary might not be there if the download was skipped with + * the `PUPPETEER_SKIP_DOWNLOAD` environment variable. + */ executablePath(): string { return this._launcher.executablePath(); } + /** + * @internal + */ get _launcher(): ProductLauncher { if ( !this._lazyLauncher || @@ -122,26 +194,89 @@ export class Puppeteer { return this._lazyLauncher; } + /** + * @returns The name of the browser that is under automation (`"chrome"` or `"firefox"`) + * + * @remarks + * The product is set by the `PUPPETEER_PRODUCT` environment variable or the `product` + * option in `puppeteer.launch([options])` and defaults to `chrome`. + * Firefox support is experimental. + */ get product(): string { return this._launcher.product; } + /** + * @remarks + * @example + * + * ```js + * const puppeteer = require('puppeteer'); + * const iPhone = puppeteer.devices['iPhone 6']; + * + * (async () => { + * const browser = await puppeteer.launch(); + * const page = await browser.newPage(); + * await page.emulate(iPhone); + * await page.goto('https://www.google.com'); + * // other actions... + * await browser.close(); + * })(); + * ``` + * + * @returns a list of devices to be used with `page.emulate(options)`. Actual list of devices can be found in {@link https://github.com/puppeteer/puppeteer/blob/main/src/DeviceDescriptors.ts | src/DeviceDescriptors.ts}. + */ get devices(): DevicesMap { return devicesMap; } + /** + * @remarks + * + * Puppeteer methods might throw errors if they are unable to fulfill a request. + * For example, `page.waitForSelector(selector[, options])` might fail if + * the selector doesn't match any nodes during the given timeframe. + * + * For certain types of errors Puppeteer uses specific error classes. + * These classes are available via `puppeteer.errors` + * @example + * An example of handling a timeout error: + * ```js + * try { + * await page.waitForSelector('.foo'); + * } catch (e) { + * if (e instanceof puppeteer.errors.TimeoutError) { + * // Do something if this is a timeout. + * } + * } + * ``` + */ get errors(): PuppeteerErrors { return puppeteerErrors; } + /** + * + * @param options Set of configurable options to set on the browser. + * @returns The default flags that Chromium will be launched with. + */ defaultArgs(options: ChromeArgOptions = {}): string[] { return this._launcher.defaultArgs(options); } + /** + * + * @param options Set of configurable options to specify the settings + * of the BrowserFetcher. + * @returns A new BrowserFetcher instance. + */ createBrowserFetcher(options: BrowserFetcherOptions): BrowserFetcher { return new BrowserFetcher(this._projectRoot, options); } + /** + * @internal + */ // eslint-disable-next-line @typescript-eslint/camelcase __experimental_registerCustomQueryHandler( name: string, @@ -150,16 +285,25 @@ export class Puppeteer { registerCustomQueryHandler(name, queryHandler); } + /** + * @internal + */ // eslint-disable-next-line @typescript-eslint/camelcase __experimental_unregisterCustomQueryHandler(name: string): void { unregisterCustomQueryHandler(name); } + /** + * @internal + */ // eslint-disable-next-line @typescript-eslint/camelcase __experimental_customQueryHandlers(): Map { return customQueryHandlers(); } + /** + * @internal + */ // eslint-disable-next-line @typescript-eslint/camelcase __experimental_clearQueryHandlers(): void { clearQueryHandlers();