• Home
  • History
  • Annotate
Name Date Size #Lines LOC

..30-Mar-2022-

.github/workflows/H30-Mar-2022-212185

examples/H03-May-2022-4330

mocha-config/H03-May-2022-

scripts/H30-Mar-2022-394280

src/H03-May-2022-20,12311,933

test/H03-May-2022-16,73314,211

test-browser/H03-May-2022-

test-ts-types/H30-Mar-2022-302256

utils/H03-May-2022-318264

vendor/H30-Mar-2022-479350

.editorconfigH A D30-Mar-2022147 108

.eslintignoreH A D30-Mar-2022412 1817

.npmrcH A D30-Mar-202214 21

.prettierignoreH A D30-Mar-2022102 119

CHANGELOG.mdH A D30-Mar-202232.8 KiB348203

CONTRIBUTING.mdH A D30-Mar-202213.8 KiB321219

LICENSEH A D30-Mar-202211.1 KiB203169

README.mdH A D30-Mar-202222.4 KiB482307

api-extractor.jsonH A D30-Mar-2022917 4739

moz.yamlH A D30-Mar-2022229 1110

package-lock.jsonH A D30-Mar-2022282.3 KiB7,3197,318

package.jsonH A D30-Mar-20224.5 KiB118117

tsconfig.base.jsonH A D30-Mar-2022264 1413

tsconfig.jsonH A D30-Mar-2022550 1716

README.md

1# Puppeteer
2
3<!-- [START badges] -->
4
5[![Build status](https://github.com/puppeteer/puppeteer/workflows/run-checks/badge.svg)](https://github.com/puppeteer/puppeteer/actions?query=workflow%3Arun-checks) [![npm puppeteer package](https://img.shields.io/npm/v/puppeteer.svg)](https://npmjs.org/package/puppeteer)
6
7<!-- [END badges] -->
8
9<img src="https://user-images.githubusercontent.com/10379601/29446482-04f7036a-841f-11e7-9872-91d1fc2ea683.png" height="200" align="right">
10
11###### [API](https://github.com/puppeteer/puppeteer/blob/v13.0.1/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)
12
13> 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.
14
15<!-- [START usecases] -->
16
17###### What can I do?
18
19Most things that you can do manually in the browser can be done using Puppeteer! Here are a few examples to get you started:
20
21- Generate screenshots and PDFs of pages.
22- Crawl a SPA (Single-Page Application) and generate pre-rendered content (i.e. "SSR" (Server-Side Rendering)).
23- Automate form submission, UI testing, keyboard input, etc.
24- 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.
25- Capture a [timeline trace](https://developers.google.com/web/tools/chrome-devtools/evaluate-performance/reference) of your site to help diagnose performance issues.
26- Test Chrome Extensions.
27<!-- [END usecases] -->
28
29<!-- [START getstarted] -->
30
31## Getting Started
32
33### Installation
34
35To use Puppeteer in your project, run:
36
37```bash
38npm i puppeteer
39# or "yarn add puppeteer"
40```
41
42Note: 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, download into another path, or download a different browser, see [Environment variables](https://github.com/puppeteer/puppeteer/blob/v13.0.1/docs/api.md#environment-variables).
43
44### puppeteer-core
45
46Since version 1.7.0 we publish the [`puppeteer-core`](https://www.npmjs.com/package/puppeteer-core) package,
47a version of Puppeteer that doesn't download any browser by default.
48
49```bash
50npm i puppeteer-core
51# or "yarn add puppeteer-core"
52```
53
54`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
55browser you intend to connect to.
56
57See [puppeteer vs puppeteer-core](https://github.com/puppeteer/puppeteer/blob/main/docs/api.md#puppeteer-vs-puppeteer-core).
58
59### Usage
60
61Puppeteer follows the latest [maintenance LTS](https://github.com/nodejs/Release#release-schedule) version of Node.
62
63Note: Prior to v1.18.1, Puppeteer required at least Node v6.4.0. Versions from v1.18.1 to v2.1.0 rely on
64Node 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.
65
66Puppeteer will be familiar to people using other browser testing frameworks. You create an instance
67of `Browser`, open pages, and then manipulate them with [Puppeteer's API](https://github.com/puppeteer/puppeteer/blob/v13.0.1/docs/api.md#).
68
69**Example** - navigating to https://example.com and saving a screenshot as _example.png_:
70
71Save file as **example.js**
72
73```js
74const puppeteer = require('puppeteer');
75
76(async () => {
77  const browser = await puppeteer.launch();
78  const page = await browser.newPage();
79  await page.goto('https://example.com');
80  await page.screenshot({ path: 'example.png' });
81
82  await browser.close();
83})();
84```
85
86Execute script on the command line
87
88```bash
89node example.js
90```
91
92Puppeteer 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/v13.0.1/docs/api.md#pagesetviewportviewport).
93
94**Example** - create a PDF.
95
96Save file as **hn.js**
97
98```js
99const puppeteer = require('puppeteer');
100
101(async () => {
102  const browser = await puppeteer.launch();
103  const page = await browser.newPage();
104  await page.goto('https://news.ycombinator.com', {
105    waitUntil: 'networkidle2',
106  });
107  await page.pdf({ path: 'hn.pdf', format: 'a4' });
108
109  await browser.close();
110})();
111```
112
113Execute script on the command line
114
115```bash
116node hn.js
117```
118
119See [`Page.pdf()`](https://github.com/puppeteer/puppeteer/blob/v13.0.1/docs/api.md#pagepdfoptions) for more information about creating pdfs.
120
121**Example** - evaluate script in the context of the page
122
123Save file as **get-dimensions.js**
124
125```js
126const puppeteer = require('puppeteer');
127
128(async () => {
129  const browser = await puppeteer.launch();
130  const page = await browser.newPage();
131  await page.goto('https://example.com');
132
133  // Get the "viewport" of the page, as reported by the page.
134  const dimensions = await page.evaluate(() => {
135    return {
136      width: document.documentElement.clientWidth,
137      height: document.documentElement.clientHeight,
138      deviceScaleFactor: window.devicePixelRatio,
139    };
140  });
141
142  console.log('Dimensions:', dimensions);
143
144  await browser.close();
145})();
146```
147
148Execute script on the command line
149
150```bash
151node get-dimensions.js
152```
153
154See [`Page.evaluate()`](https://github.com/puppeteer/puppeteer/blob/v13.0.1/docs/api.md#pageevaluatepagefunction-args) for more information on `evaluate` and related methods like `evaluateOnNewDocument` and `exposeFunction`.
155
156<!-- [END getstarted] -->
157
158<!-- [START runtimesettings] -->
159
160## Default runtime settings
161
162**1. Uses Headless mode**
163
164Puppeteer 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/v13.0.1/docs/api.md#puppeteerlaunchoptions) when launching a browser:
165
166```js
167const browser = await puppeteer.launch({ headless: false }); // default is true
168```
169
170**2. Runs a bundled version of Chromium**
171
172By default, Puppeteer downloads and uses a specific version of Chromium so its API
173is guaranteed to work out of the box. To use Puppeteer with a different version of Chrome or Chromium,
174pass in the executable's path when creating a `Browser` instance:
175
176```js
177const browser = await puppeteer.launch({ executablePath: '/path/to/Chrome' });
178```
179
180You can also use Puppeteer with Firefox Nightly (experimental support). See [`Puppeteer.launch()`](https://github.com/puppeteer/puppeteer/blob/v13.0.1/docs/api.md#puppeteerlaunchoptions) for more information.
181
182See [`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/+/refs/heads/main/docs/chromium_browser_vs_google_chrome.md) describes some differences for Linux users.
183
184**3. Creates a fresh user profile**
185
186Puppeteer creates its own browser user profile which it **cleans up on every run**.
187
188<!-- [END runtimesettings] -->
189
190## Resources
191
192- [API Documentation](https://github.com/puppeteer/puppeteer/blob/v13.0.1/docs/api.md)
193- [Examples](https://github.com/puppeteer/puppeteer/tree/main/examples/)
194- [Community list of Puppeteer resources](https://github.com/transitive-bullshit/awesome-puppeteer)
195
196<!-- [START debugging] -->
197
198## Debugging tips
199
2001.  Turn off headless mode - sometimes it's useful to see what the browser is
201    displaying. Instead of launching in headless mode, launch a full version of
202    the browser using `headless: false`:
203
204    ```js
205    const browser = await puppeteer.launch({ headless: false });
206    ```
207
2082.  Slow it down - the `slowMo` option slows down Puppeteer operations by the
209    specified amount of milliseconds. It's another way to help see what's going on.
210
211    ```js
212    const browser = await puppeteer.launch({
213      headless: false,
214      slowMo: 250, // slow down by 250ms
215    });
216    ```
217
2183.  Capture console output - You can listen for the `console` event.
219    This is also handy when debugging code in `page.evaluate()`:
220
221    ```js
222    page.on('console', (msg) => console.log('PAGE LOG:', msg.text()));
223
224    await page.evaluate(() => console.log(`url is ${location.href}`));
225    ```
226
2274.  Use debugger in application code browser
228
229    There are two execution context: node.js that is running test code, and the browser
230    running application code being tested. This lets you debug code in the
231    application code browser; ie code inside `evaluate()`.
232
233    - Use `{devtools: true}` when launching Puppeteer:
234
235      ```js
236      const browser = await puppeteer.launch({ devtools: true });
237      ```
238
239    - Change default test timeout:
240
241      jest: `jest.setTimeout(100000);`
242
243      jasmine: `jasmine.DEFAULT_TIMEOUT_INTERVAL = 100000;`
244
245      mocha: `this.timeout(100000);` (don't forget to change test to use [function and not '=>'](https://stackoverflow.com/a/23492442))
246
247    - Add an evaluate statement with `debugger` inside / add `debugger` to an existing evaluate statement:
248
249      ```js
250      await page.evaluate(() => {
251        debugger;
252      });
253      ```
254
255      The test will now stop executing in the above evaluate statement, and chromium will stop in debug mode.
256
2575.  Use debugger in node.js
258
259    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.
260
261    Note that you won't be able to run `await page.click()` in
262    DevTools console due to this [Chromium bug](https://bugs.chromium.org/p/chromium/issues/detail?id=833928). So if
263    you want to try something out, you have to add it to your test file.
264
265    - Add `debugger;` to your test, eg:
266
267      ```js
268      debugger;
269      await page.click('a[target=_blank]');
270      ```
271
272    - Set `headless` to `false`
273    - Run `node --inspect-brk`, eg `node --inspect-brk node_modules/.bin/jest tests`
274    - In Chrome open `chrome://inspect/#devices` and click `inspect`
275    - In the newly opened test browser, type `F8` to resume test execution
276    - Now your `debugger` will be hit and you can debug in the test browser
277
2786.  Enable verbose logging - internal DevTools protocol traffic
279    will be logged via the [`debug`](https://github.com/visionmedia/debug) module under the `puppeteer` namespace.
280
281         # Basic verbose logging
282         env DEBUG="puppeteer:*" node script.js
283
284         # Protocol traffic can be rather noisy. This example filters out all Network domain messages
285         env DEBUG="puppeteer:*" env DEBUG_COLORS=true node script.js 2>&1 | grep -v '"Network'
286
2877.  Debug your Puppeteer (node) code easily, using [ndb](https://github.com/GoogleChromeLabs/ndb)
288
289- `npm install -g ndb` (or even better, use [npx](https://github.com/zkat/npx)!)
290
291- add a `debugger` to your Puppeteer (node) code
292
293- add `ndb` (or `npx ndb`) before your test command. For example:
294
295  `ndb jest` or `ndb mocha` (or `npx ndb jest` / `npx ndb mocha`)
296
297- debug your test inside chromium like a boss!
298
299<!-- [END debugging] -->
300
301<!-- [START typescript] -->
302
303## Usage with TypeScript
304
305We have recently completed a migration to move the Puppeteer source code from JavaScript to TypeScript and as of version 7.0.1 we ship our own built-in type definitions.
306
307If you are on a version older than 7, we recommend installing the Puppeteer type definitions from the [DefinitelyTyped](https://definitelytyped.org/) repository:
308
309```bash
310npm install --save-dev @types/puppeteer
311```
312
313The 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.
314
315<!-- [END typescript] -->
316
317## Contributing to Puppeteer
318
319Check out [contributing guide](https://github.com/puppeteer/puppeteer/blob/main/CONTRIBUTING.md) to get an overview of Puppeteer development.
320
321<!-- [START faq] -->
322
323# FAQ
324
325#### Q: Who maintains Puppeteer?
326
327The Chrome DevTools team maintains the library, but we'd love your help and expertise on the project!
328See [Contributing](https://github.com/puppeteer/puppeteer/blob/main/CONTRIBUTING.md).
329
330#### Q: What is the status of cross-browser support?
331
332Official 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.
333
334From Puppeteer v2.1.0 onwards you can specify [`puppeteer.launch({product: 'firefox'})`](https://github.com/puppeteer/puppeteer/blob/v13.0.1/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.
335
336We will continue to collaborate with other browser vendors to bring Puppeteer support to browsers such as Safari.
337This effort includes exploration of a standard for executing cross-browser commands (instead of relying on the non-standard DevTools Protocol used by Chrome).
338
339#### Q: What are Puppeteer’s goals and principles?
340
341The goals of the project are:
342
343- Provide a slim, canonical library that highlights the capabilities of the [DevTools Protocol](https://chromedevtools.github.io/devtools-protocol/).
344- Provide a reference implementation for similar testing libraries. Eventually, these other frameworks could adopt Puppeteer as their foundational layer.
345- Grow the adoption of headless/automated browser testing.
346- Help dogfood new DevTools Protocol features...and catch bugs!
347- Learn more about the pain points of automated browser testing and help fill those gaps.
348
349We adapt [Chromium principles](https://www.chromium.org/developers/core-principles) to help us drive product decisions:
350
351- **Speed**: Puppeteer has almost zero performance overhead over an automated page.
352- **Security**: Puppeteer operates off-process with respect to Chromium, making it safe to automate potentially malicious pages.
353- **Stability**: Puppeteer should not be flaky and should not leak memory.
354- **Simplicity**: Puppeteer provides a high-level API that’s easy to use, understand, and debug.
355
356#### Q: Is Puppeteer replacing Selenium/WebDriver?
357
358**No**. Both projects are valuable for very different reasons:
359
360- Selenium/WebDriver focuses on cross-browser automation; its value proposition is a single standard API that works across all major browsers.
361- Puppeteer focuses on Chromium; its value proposition is richer functionality and higher reliability.
362
363That 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:
364
365- 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.
366- Puppeteer has event-driven architecture, which removes a lot of potential flakiness. There’s no need for evil “sleep(1000)” calls in puppeteer scripts.
367- 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.
368- 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.
369
370#### Q: Why doesn’t Puppeteer v.XXX work with Chromium v.YYY?
371
372We 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.
373
374This is not an artificial constraint: A lot of work on Puppeteer is actually taking place in the Chromium repository. Here’s a typical story:
375
376- A Puppeteer bug is reported: https://github.com/puppeteer/puppeteer/issues/2709
377- 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
378- Once the upstream fix is landed, we roll updated Chromium into Puppeteer: https://github.com/puppeteer/puppeteer/pull/2769
379
380However, 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.
381
382For example, in order to drive Chrome 71 with puppeteer-core, use `chrome-71` npm tag:
383
384```bash
385npm install puppeteer-core@chrome-71
386```
387
388#### Q: Which Chromium version does Puppeteer use?
389
390Look 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.
391
392#### Q: Which Firefox version does Puppeteer use?
393
394Since 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.
395
396To fetch Firefox Nightly as part of Puppeteer installation:
397
398```bash
399PUPPETEER_PRODUCT=firefox npm i puppeteer
400# or "yarn add puppeteer"
401```
402
403#### Q: What’s considered a “Navigation”?
404
405From Puppeteer’s standpoint, **“navigation” is anything that changes a page’s URL**.
406Aside 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.
407
408With this definition of “navigation,” **Puppeteer works seamlessly with single-page applications.**
409
410#### Q: What’s the difference between a “trusted" and "untrusted" input event?
411
412In browsers, input events could be divided into two big groups: trusted vs. untrusted.
413
414- **Trusted events**: events generated by users interacting with the page, e.g. using a mouse or keyboard.
415- **Untrusted event**: events generated by Web APIs, e.g. `document.createEvent` or `element.click()` methods.
416
417Websites can distinguish between these two groups:
418
419- using an [`Event.isTrusted`](https://developer.mozilla.org/en-US/docs/Web/API/Event/isTrusted) event flag
420- sniffing for accompanying events. For example, every trusted `'click'` event is preceded by `'mousedown'` and `'mouseup'` events.
421
422For 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:
423
424```js
425await page.evaluate(() => {
426  document.querySelector('button[type=submit]').click();
427});
428```
429
430#### Q: What features does Puppeteer not support?
431
432You 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:
433
434- 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/v13.0.1/docs/api.md#puppeteerlaunchoptions). You should only use this configuration if you need an official release of Chrome that supports these media formats.)
435- 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).
436
437#### Q: I am having trouble installing / running Puppeteer in my test environment. Where should I look for help?
438
439We have a [troubleshooting](https://github.com/puppeteer/puppeteer/blob/main/docs/troubleshooting.md) guide for various operating systems that lists the required dependencies.
440
441#### Q: Chromium gets downloaded on every `npm ci` run. How can I cache the download?
442
443The default download path is `node_modules/puppeteer/.local-chromium`. However, you can change that path with the `PUPPETEER_DOWNLOAD_PATH` environment variable.
444
445Puppeteer uses that variable to resolve the Chromium executable location during launch, so you don’t need to specify `PUPPETEER_EXECUTABLE_PATH` as well.
446
447For example, if you wish to keep the Chromium download in `~/.npm/chromium`:
448
449```sh
450export PUPPETEER_DOWNLOAD_PATH=~/.npm/chromium
451npm ci
452
453# by default the Chromium executable path is inferred
454# from the download path
455npm test
456
457# a new run of npm ci will check for the existence of
458# Chromium in ~/.npm/chromium
459npm ci
460```
461
462#### Q: How do I try/test a prerelease version of Puppeteer?
463
464You can check out this repo or install the latest prerelease from npm:
465
466```bash
467npm i --save puppeteer@next
468```
469
470Please note that prerelease may be unstable and contain bugs.
471
472#### Q: I have more questions! Where do I ask?
473
474There are many ways to get help on Puppeteer:
475
476- [bugtracker](https://github.com/puppeteer/puppeteer/issues)
477- [Stack Overflow](https://stackoverflow.com/questions/tagged/puppeteer)
478
479Make sure to search these channels before posting your question.
480
481<!-- [END faq] -->
482