UNPKG

22.4 kBMarkdownView Raw
1# Puppeteer
2
3<!-- [START badges] -->
4[![Build status](https://img.shields.io/travis/com/puppeteer/puppeteer/main.svg)](https://travis-ci.com/puppeteer/puppeteer) [![npm puppeteer package](https://img.shields.io/npm/v/puppeteer.svg)](https://npmjs.org/package/puppeteer) [![Issue resolution status](https://isitmaintained.com/badge/resolution/puppeteer/puppeteer.svg)](https://github.com/puppeteer/puppeteer/issues)
5<!-- [END badges] -->
6
7<img src="https://user-images.githubusercontent.com/10379601/29446482-04f7036a-841f-11e7-9872-91d1fc2ea683.png" height="200" align="right">
8
9###### [API](https://github.com/puppeteer/puppeteer/blob/v5.3.0/docs/api.md) | [FAQ](#faq) | [Contributing](https://github.com/puppeteer/puppeteer/blob/main/CONTRIBUTING.md) | [Troubleshooting](https://github.com/puppeteer/puppeteer/blob/main/docs/troubleshooting.md)
10
11> Puppeteer is a Node library which provides a high-level API to control Chrome or Chromium over the [DevTools Protocol](https://chromedevtools.github.io/devtools-protocol/). Puppeteer runs [headless](https://developers.google.com/web/updates/2017/04/headless-chrome) by default, but can be configured to run full (non-headless) Chrome or Chromium.
12
13<!-- [START usecases] -->
14###### What can I do?
15
16Most things that you can do manually in the browser can be done using Puppeteer! Here are a few examples to get you started:
17
18* Generate screenshots and PDFs of pages.
19* Crawl a SPA (Single-Page Application) and generate pre-rendered content (i.e. "SSR" (Server-Side Rendering)).
20* Automate form submission, UI testing, keyboard input, etc.
21* Create an up-to-date, automated testing environment. Run your tests directly in the latest version of Chrome using the latest JavaScript and browser features.
22* Capture a [timeline trace](https://developers.google.com/web/tools/chrome-devtools/evaluate-performance/reference) of your site to help diagnose performance issues.
23* Test Chrome Extensions.
24<!-- [END usecases] -->
25
26Give it a spin: https://try-puppeteer.appspot.com/
27
28<!-- [START getstarted] -->
29## Getting Started
30
31### Installation
32
33To use Puppeteer in your project, run:
34
35```bash
36npm i puppeteer
37# or "yarn add puppeteer"
38```
39
40Note: When you install Puppeteer, it downloads a recent version of Chromium (~170MB Mac, ~282MB Linux, ~280MB Win) that is guaranteed to work with the API. To skip the download, or to download a different browser, see [Environment variables](https://github.com/puppeteer/puppeteer/blob/v5.3.0/docs/api.md#environment-variables).
41
42
43### puppeteer-core
44
45Since version 1.7.0 we publish the [`puppeteer-core`](https://www.npmjs.com/package/puppeteer-core) package,
46a version of Puppeteer that doesn't download any browser by default.
47
48```bash
49npm i puppeteer-core
50# or "yarn add puppeteer-core"
51```
52
53`puppeteer-core` is intended to be a lightweight version of Puppeteer for launching an existing browser installation or for connecting to a remote one. Be sure that the version of puppeteer-core you install is compatible with the
54browser you intend to connect to.
55
56See [puppeteer vs puppeteer-core](https://github.com/puppeteer/puppeteer/blob/main/docs/api.md#puppeteer-vs-puppeteer-core).
57
58### Usage
59
60Puppeteer follows the latest [maintenance LTS](https://github.com/nodejs/Release#release-schedule) version of Node.
61
62Note: Prior to v1.18.1, Puppeteer required at least Node v6.4.0. Versions from v1.18.1 to v2.1.0 rely on
63Node 8.9.0+. Starting from v3.0.0 Puppeteer starts to rely on Node 10.18.1+. All examples below use async/await which is only supported in Node v7.6.0 or greater.
64
65Puppeteer will be familiar to people using other browser testing frameworks. You create an instance
66of `Browser`, open pages, and then manipulate them with [Puppeteer's API](https://github.com/puppeteer/puppeteer/blob/v5.3.0/docs/api.md#).
67
68**Example** - navigating to https://example.com and saving a screenshot as *example.png*:
69
70Save file as **example.js**
71
72```js
73const puppeteer = require('puppeteer');
74
75(async () => {
76 const browser = await puppeteer.launch();
77 const page = await browser.newPage();
78 await page.goto('https://example.com');
79 await page.screenshot({path: 'example.png'});
80
81 await browser.close();
82})();
83```
84
85Execute script on the command line
86
87```bash
88node example.js
89```
90
91Puppeteer sets an initial page size to 800×600px, which defines the screenshot size. The page size can be customized with [`Page.setViewport()`](https://github.com/puppeteer/puppeteer/blob/v5.3.0/docs/api.md#pagesetviewportviewport).
92
93**Example** - create a PDF.
94
95Save file as **hn.js**
96
97```js
98const puppeteer = require('puppeteer');
99
100(async () => {
101 const browser = await puppeteer.launch();
102 const page = await browser.newPage();
103 await page.goto('https://news.ycombinator.com', {waitUntil: 'networkidle2'});
104 await page.pdf({path: 'hn.pdf', format: 'A4'});
105
106 await browser.close();
107})();
108```
109
110Execute script on the command line
111
112```bash
113node hn.js
114```
115
116See [`Page.pdf()`](https://github.com/puppeteer/puppeteer/blob/v5.3.0/docs/api.md#pagepdfoptions) for more information about creating pdfs.
117
118**Example** - evaluate script in the context of the page
119
120Save file as **get-dimensions.js**
121
122```js
123const puppeteer = require('puppeteer');
124
125(async () => {
126 const browser = await puppeteer.launch();
127 const page = await browser.newPage();
128 await page.goto('https://example.com');
129
130 // Get the "viewport" of the page, as reported by the page.
131 const dimensions = await page.evaluate(() => {
132 return {
133 width: document.documentElement.clientWidth,
134 height: document.documentElement.clientHeight,
135 deviceScaleFactor: window.devicePixelRatio
136 };
137 });
138
139 console.log('Dimensions:', dimensions);
140
141 await browser.close();
142})();
143```
144
145Execute script on the command line
146
147```bash
148node get-dimensions.js
149```
150
151See [`Page.evaluate()`](https://github.com/puppeteer/puppeteer/blob/v5.3.0/docs/api.md#pageevaluatepagefunction-args) for more information on `evaluate` and related methods like `evaluateOnNewDocument` and `exposeFunction`.
152
153<!-- [END getstarted] -->
154
155<!-- [START runtimesettings] -->
156## Default runtime settings
157
158**1. Uses Headless mode**
159
160Puppeteer launches Chromium in [headless mode](https://developers.google.com/web/updates/2017/04/headless-chrome). To launch a full version of Chromium, set the [`headless` option](https://github.com/puppeteer/puppeteer/blob/v5.3.0/docs/api.md#puppeteerlaunchoptions) when launching a browser:
161
162```js
163const browser = await puppeteer.launch({headless: false}); // default is true
164```
165
166**2. Runs a bundled version of Chromium**
167
168By default, Puppeteer downloads and uses a specific version of Chromium so its API
169is guaranteed to work out of the box. To use Puppeteer with a different version of Chrome or Chromium,
170pass in the executable's path when creating a `Browser` instance:
171
172```js
173const browser = await puppeteer.launch({executablePath: '/path/to/Chrome'});
174```
175
176You can also use Puppeteer with Firefox Nightly (experimental support). See [`Puppeteer.launch()`](https://github.com/puppeteer/puppeteer/blob/v5.3.0/docs/api.md#puppeteerlaunchoptions) for more information.
177
178See [`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/+/master/docs/chromium_browser_vs_google_chrome.md) describes some differences for Linux users.
179
180**3. Creates a fresh user profile**
181
182Puppeteer creates its own browser user profile which it **cleans up on every run**.
183
184<!-- [END runtimesettings] -->
185
186## Resources
187
188- [API Documentation](https://github.com/puppeteer/puppeteer/blob/v5.3.0/docs/api.md)
189- [Examples](https://github.com/puppeteer/puppeteer/tree/main/examples/)
190- [Community list of Puppeteer resources](https://github.com/transitive-bullshit/awesome-puppeteer)
191
192
193<!-- [START debugging] -->
194
195## Debugging tips
196
1971. Turn off headless mode - sometimes it's useful to see what the browser is
198 displaying. Instead of launching in headless mode, launch a full version of
199 the browser using `headless: false`:
200
201 ```js
202 const browser = await puppeteer.launch({headless: false});
203 ```
204
2052. Slow it down - the `slowMo` option slows down Puppeteer operations by the
206 specified amount of milliseconds. It's another way to help see what's going on.
207
208 ```js
209 const browser = await puppeteer.launch({
210 headless: false,
211 slowMo: 250 // slow down by 250ms
212 });
213 ```
214
2153. Capture console output - You can listen for the `console` event.
216 This is also handy when debugging code in `page.evaluate()`:
217
218 ```js
219 page.on('console', msg => console.log('PAGE LOG:', msg.text()));
220
221 await page.evaluate(() => console.log(`url is ${location.href}`));
222 ```
223
2244. Use debugger in application code browser
225
226 There are two execution context: node.js that is running test code, and the browser
227 running application code being tested. This lets you debug code in the
228 application code browser; ie code inside `evaluate()`.
229
230 - Use `{devtools: true}` when launching Puppeteer:
231
232 ```js
233 const browser = await puppeteer.launch({devtools: true});
234 ```
235
236 - Change default test timeout:
237
238 jest: `jest.setTimeout(100000);`
239
240 jasmine: `jasmine.DEFAULT_TIMEOUT_INTERVAL = 100000;`
241
242 mocha: `this.timeout(100000);` (don't forget to change test to use [function and not '=>'](https://stackoverflow.com/a/23492442))
243
244 - Add an evaluate statement with `debugger` inside / add `debugger` to an existing evaluate statement:
245
246 ```js
247 await page.evaluate(() => {debugger;});
248 ```
249
250 The test will now stop executing in the above evaluate statement, and chromium will stop in debug mode.
251
2525. Use debugger in node.js
253
254 This will let you debug test code. For example, you can step over `await page.click()` in the node.js script and see the click happen in the application code browser.
255
256 Note that you won't be able to run `await page.click()` in
257 DevTools console due to this [Chromium bug](https://bugs.chromium.org/p/chromium/issues/detail?id=833928). So if
258 you want to try something out, you have to add it to your test file.
259
260 - Add `debugger;` to your test, eg:
261
262 ```js
263 debugger;
264 await page.click('a[target=_blank]');
265 ```
266
267 - Set `headless` to `false`
268 - Run `node --inspect-brk`, eg `node --inspect-brk node_modules/.bin/jest tests`
269 - In Chrome open `chrome://inspect/#devices` and click `inspect`
270 - In the newly opened test browser, type `F8` to resume test execution
271 - Now your `debugger` will be hit and you can debug in the test browser
272
273
2746. Enable verbose logging - internal DevTools protocol traffic
275 will be logged via the [`debug`](https://github.com/visionmedia/debug) module under the `puppeteer` namespace.
276
277 # Basic verbose logging
278 env DEBUG="puppeteer:*" node script.js
279
280 # Protocol traffic can be rather noisy. This example filters out all Network domain messages
281 env DEBUG="puppeteer:*" env DEBUG_COLORS=true node script.js 2>&1 | grep -v '"Network'
282
2837. Debug your Puppeteer (node) code easily, using [ndb](https://github.com/GoogleChromeLabs/ndb)
284
285 - `npm install -g ndb` (or even better, use [npx](https://github.com/zkat/npx)!)
286
287 - add a `debugger` to your Puppeteer (node) code
288
289 - add `ndb` (or `npx ndb`) before your test command. For example:
290
291 `ndb jest` or `ndb mocha` (or `npx ndb jest` / `npx ndb mocha`)
292
293 - debug your test inside chromium like a boss!
294
295<!-- [END debugging] -->
296
297
298<!-- [START typescript] -->
299## Usage with TypeScript
300
301We have recently completed a migration to move the Puppeteer source code from JavaScript to TypeScript and we're currently working on shipping type definitions for TypeScript with Puppeteer's npm package.
302
303Until this work is complete we recommend installing the Puppeteer type definitions from the [DefinitelyTyped](https://definitelytyped.org/) repository:
304
305```bash
306npm install --save-dev @types/puppeteer
307```
308
309The types that you'll see appearing in the Puppeteer source code are based off the great work of those who have contributed to the `@types/puppeteer` package. We really appreciate the hard work those people put in to providing high quality TypeScript definitions for Puppeteer's users.
310
311<!-- [END typescript] -->
312
313
314## Contributing to Puppeteer
315
316Check out [contributing guide](https://github.com/puppeteer/puppeteer/blob/main/CONTRIBUTING.md) to get an overview of Puppeteer development.
317
318<!-- [START faq] -->
319
320# FAQ
321
322#### Q: Who maintains Puppeteer?
323
324The Chrome DevTools team maintains the library, but we'd love your help and expertise on the project!
325See [Contributing](https://github.com/puppeteer/puppeteer/blob/main/CONTRIBUTING.md).
326
327#### Q: What is the status of cross-browser support?
328
329Official Firefox support is currently experimental. The ongoing collaboration with Mozilla aims to support common end-to-end testing use cases, for which developers expect cross-browser coverage. The Puppeteer team needs input from users to stabilize Firefox support and to bring missing APIs to our attention.
330
331From Puppeteer v2.1.0 onwards you can specify [`puppeteer.launch({product: 'firefox'})`](https://github.com/puppeteer/puppeteer/blob/v5.3.0/docs/api.md#puppeteerlaunchoptions) to run your Puppeteer scripts in Firefox Nightly, without any additional custom patches. While [an older experiment](https://www.npmjs.com/package/puppeteer-firefox) required a patched version of Firefox, [the current approach](https://wiki.mozilla.org/Remote) works with “stock” Firefox.
332
333We will continue to collaborate with other browser vendors to bring Puppeteer support to browsers such as Safari.
334This effort includes exploration of a standard for executing cross-browser commands (instead of relying on the non-standard DevTools Protocol used by Chrome).
335
336#### Q: What are Puppeteer’s goals and principles?
337
338The goals of the project are:
339
340- Provide a slim, canonical library that highlights the capabilities of the [DevTools Protocol](https://chromedevtools.github.io/devtools-protocol/).
341- Provide a reference implementation for similar testing libraries. Eventually, these other frameworks could adopt Puppeteer as their foundational layer.
342- Grow the adoption of headless/automated browser testing.
343- Help dogfood new DevTools Protocol features...and catch bugs!
344- Learn more about the pain points of automated browser testing and help fill those gaps.
345
346We adapt [Chromium principles](https://www.chromium.org/developers/core-principles) to help us drive product decisions:
347- **Speed**: Puppeteer has almost zero performance overhead over an automated page.
348- **Security**: Puppeteer operates off-process with respect to Chromium, making it safe to automate potentially malicious pages.
349- **Stability**: Puppeteer should not be flaky and should not leak memory.
350- **Simplicity**: Puppeteer provides a high-level API that’s easy to use, understand, and debug.
351
352#### Q: Is Puppeteer replacing Selenium/WebDriver?
353
354**No**. Both projects are valuable for very different reasons:
355- Selenium/WebDriver focuses on cross-browser automation; its value proposition is a single standard API that works across all major browsers.
356- Puppeteer focuses on Chromium; its value proposition is richer functionality and higher reliability.
357
358That said, you **can** use Puppeteer to run tests against Chromium, e.g. using the community-driven [jest-puppeteer](https://github.com/smooth-code/jest-puppeteer). While this probably shouldn’t be your only testing solution, it does have a few good points compared to WebDriver:
359
360- Puppeteer requires zero setup and comes bundled with the Chromium version it works best with, making it [very easy to start with](https://github.com/puppeteer/puppeteer/#getting-started). At the end of the day, it’s better to have a few tests running chromium-only, than no tests at all.
361- Puppeteer has event-driven architecture, which removes a lot of potential flakiness. There’s no need for evil “sleep(1000)” calls in puppeteer scripts.
362- Puppeteer runs headless by default, which makes it fast to run. Puppeteer v1.5.0 also exposes browser contexts, making it possible to efficiently parallelize test execution.
363- Puppeteer shines when it comes to debugging: flip the “headless” bit to false, add “slowMo”, and you’ll see what the browser is doing. You can even open Chrome DevTools to inspect the test environment.
364
365#### Q: Why doesn’t Puppeteer v.XXX work with Chromium v.YYY?
366
367We see Puppeteer as an **indivisible entity** with Chromium. Each version of Puppeteer bundles a specific version of Chromium – **the only** version it is guaranteed to work with.
368
369This is not an artificial constraint: A lot of work on Puppeteer is actually taking place in the Chromium repository. Here’s a typical story:
370- A Puppeteer bug is reported: https://github.com/puppeteer/puppeteer/issues/2709
371- It turned out this is an issue with the DevTools protocol, so we’re fixing it in Chromium: https://chromium-review.googlesource.com/c/chromium/src/+/1102154
372- Once the upstream fix is landed, we roll updated Chromium into Puppeteer: https://github.com/puppeteer/puppeteer/pull/2769
373
374However, oftentimes it is desirable to use Puppeteer with the official Google Chrome rather than Chromium. For this to work, you should install a `puppeteer-core` version that corresponds to the Chrome version.
375
376For example, in order to drive Chrome 71 with puppeteer-core, use `chrome-71` npm tag:
377```bash
378npm install puppeteer-core@chrome-71
379```
380
381#### Q: Which Chromium version does Puppeteer use?
382
383Look for the `chromium` entry in [revisions.ts](https://github.com/puppeteer/puppeteer/blob/main/src/revisions.ts). To find the corresponding Chromium commit and version number, search for the revision prefixed by an `r` in [OmahaProxy](https://omahaproxy.appspot.com/)'s "Find Releases" section.
384
385
386#### Q: Which Firefox version does Puppeteer use?
387
388Since Firefox support is experimental, Puppeteer downloads the latest [Firefox Nightly](https://wiki.mozilla.org/Nightly) when the `PUPPETEER_PRODUCT` environment variable is set to `firefox`. That's also why the value of `firefox` in [revisions.ts](https://github.com/puppeteer/puppeteer/blob/main/src/revisions.ts) is `latest` -- Puppeteer isn't tied to a particular Firefox version.
389
390To fetch Firefox Nightly as part of Puppeteer installation:
391
392```bash
393PUPPETEER_PRODUCT=firefox npm i puppeteer
394# or "yarn add puppeteer"
395```
396
397#### Q: What’s considered a “Navigation”?
398
399From Puppeteer’s standpoint, **“navigation” is anything that changes a page’s URL**.
400Aside from regular navigation where the browser hits the network to fetch a new document from the web server, this includes [anchor navigations](https://www.w3.org/TR/html5/single-page.html#scroll-to-fragid) and [History API](https://developer.mozilla.org/en-US/docs/Web/API/History_API) usage.
401
402With this definition of “navigation,” **Puppeteer works seamlessly with single-page applications.**
403
404#### Q: What’s the difference between a “trusted" and "untrusted" input event?
405
406In browsers, input events could be divided into two big groups: trusted vs. untrusted.
407
408- **Trusted events**: events generated by users interacting with the page, e.g. using a mouse or keyboard.
409- **Untrusted event**: events generated by Web APIs, e.g. `document.createEvent` or `element.click()` methods.
410
411Websites can distinguish between these two groups:
412- using an [`Event.isTrusted`](https://developer.mozilla.org/en-US/docs/Web/API/Event/isTrusted) event flag
413- sniffing for accompanying events. For example, every trusted `'click'` event is preceded by `'mousedown'` and `'mouseup'` events.
414
415For automation purposes it’s important to generate trusted events. **All input events generated with Puppeteer are trusted and fire proper accompanying events.** If, for some reason, one needs an untrusted event, it’s always possible to hop into a page context with `page.evaluate` and generate a fake event:
416
417```js
418await page.evaluate(() => {
419 document.querySelector('button[type=submit]').click();
420});
421```
422
423#### Q: What features does Puppeteer not support?
424
425You may find that Puppeteer does not behave as expected when controlling pages that incorporate audio and video. (For example, [video playback/screenshots is likely to fail](https://github.com/puppeteer/puppeteer/issues/291).) There are two reasons for this:
426
427* Puppeteer is bundled with Chromium — not Chrome — and so by default, it inherits all of [Chromium's media-related limitations](https://www.chromium.org/audio-video). This means that Puppeteer does not support licensed formats such as AAC or H.264. (However, it is possible to force Puppeteer to use a separately-installed version Chrome instead of Chromium via the [`executablePath` option to `puppeteer.launch`](https://github.com/puppeteer/puppeteer/blob/v5.3.0/docs/api.md#puppeteerlaunchoptions). You should only use this configuration if you need an official release of Chrome that supports these media formats.)
428* Since Puppeteer (in all configurations) controls a desktop version of Chromium/Chrome, features that are only supported by the mobile version of Chrome are not supported. This means that Puppeteer [does not support HTTP Live Streaming (HLS)](https://caniuse.com/#feat=http-live-streaming).
429
430#### Q: I am having trouble installing / running Puppeteer in my test environment. Where should I look for help?
431We have a [troubleshooting](https://github.com/puppeteer/puppeteer/blob/main/docs/troubleshooting.md) guide for various operating systems that lists the required dependencies.
432
433#### Q: How do I try/test a prerelease version of Puppeteer?
434
435You can check out this repo or install the latest prerelease from npm:
436
437```bash
438npm i --save puppeteer@next
439```
440
441Please note that prerelease may be unstable and contain bugs.
442
443#### Q: I have more questions! Where do I ask?
444
445There are many ways to get help on Puppeteer:
446- [bugtracker](https://github.com/puppeteer/puppeteer/issues)
447- [Stack Overflow](https://stackoverflow.com/questions/tagged/puppeteer)
448- [slack channel](https://join.slack.com/t/puppeteer/shared_invite/enQtMzU4MjIyMDA5NTM4LWI0YTE0MjM0NWQzYmE2MTRmNjM1ZTBkN2MxNmJmNTIwNTJjMmFhOWFjMGExMDViYjk2YjU2ZmYzMmE1NmExYzc)
449
450Make sure to search these channels before posting your question.
451
452
453<!-- [END faq] -->