docs(api.md): Note that Browser and Page extend from EventEmitter (#1541)

Adds notes to the API documentation that Browser and Page classes
are EventEmitters. Provides an example of using `.once` for Page.

Fixes #1231
This commit is contained in:
Trent Willis 2017-12-05 23:53:27 -08:00 committed by Andrey Lushnikov
parent 16320b7ac2
commit 696f59e890

View File

@ -268,6 +268,8 @@ The method launches a browser instance with given arguments. The browser will be
### class: Browser ### class: Browser
* extends: [`EventEmitter`](https://nodejs.org/api/events.html#events_class_eventemitter)
A Browser is created when Puppeteer connects to a Chromium instance, either through [`puppeteer.launch`](#puppeteerlaunchoptions) or [`puppeteer.connect`](#puppeteerconnectoptions). A Browser is created when Puppeteer connects to a Chromium instance, either through [`puppeteer.launch`](#puppeteerlaunchoptions) or [`puppeteer.connect`](#puppeteerconnectoptions).
An example of using a [Browser] to create a [Page]: An example of using a [Browser] to create a [Page]:
@ -350,6 +352,8 @@ You can find the `webSocketDebuggerUrl` from `http://${host}:${port}/json/versio
### class: Page ### class: Page
* extends: [`EventEmitter`](https://nodejs.org/api/events.html#events_class_eventemitter)
Page provides methods to interact with a single tab in Chromium. One [Browser] instance might have multiple [Page] instances. Page provides methods to interact with a single tab in Chromium. One [Browser] instance might have multiple [Page] instances.
This example creates a page, navigates it to a URL, and then saves a screenshot: This example creates a page, navigates it to a URL, and then saves a screenshot:
@ -364,6 +368,13 @@ puppeteer.launch().then(async browser => {
}); });
``` ```
The Page class emits various events (described below) which can be handled using any of Node's native EventEmitter methods, such as `on` or `once`.
This example logs a message for a single page `load` event:
```js
page.once('load', () => console.log('Page loaded!'));
```
#### event: 'console' #### event: 'console'
- <[ConsoleMessage]> - <[ConsoleMessage]>
@ -1334,7 +1345,7 @@ await page.tracing.stop();
- `options` <[Object]> - `options` <[Object]>
- `path` <[string]> A path to write the trace file to. **required** - `path` <[string]> A path to write the trace file to. **required**
- `screenshots` <[boolean]> captures screenshots in the trace. - `screenshots` <[boolean]> captures screenshots in the trace.
- `categories` <[Array]<[string]>> specify custom categories to use instead of default. - `categories` <[Array]<[string]>> specify custom categories to use instead of default.
- returns: <[Promise]> - returns: <[Promise]>
Only one trace can be active at a time per browser. Only one trace can be active at a time per browser.