Bug 1886451: Add missing ifdef Nightly guards. r=dminor
[gecko.git] / remote / test / puppeteer / README.md
blob288a5b623c20080f9426bcdfa2280009617782f7
1 # Puppeteer
3 [![Build status](https://github.com/puppeteer/puppeteer/workflows/CI/badge.svg)](https://github.com/puppeteer/puppeteer/actions?query=workflow%3ACI)
4 [![npm puppeteer package](https://img.shields.io/npm/v/puppeteer.svg)](https://npmjs.org/package/puppeteer)
6 <img src="https://user-images.githubusercontent.com/10379601/29446482-04f7036a-841f-11e7-9872-91d1fc2ea683.png" height="200" align="right"/>
8 #### [Guides](https://pptr.dev/category/guides) | [API](https://pptr.dev/api) | [FAQ](https://pptr.dev/faq) | [Contributing](https://pptr.dev/contributing) | [Troubleshooting](https://pptr.dev/troubleshooting)
10 > Puppeteer is a Node.js library which provides a high-level API to control
11 > Chrome/Chromium over the
12 > [DevTools Protocol](https://chromedevtools.github.io/devtools-protocol/).
13 > Puppeteer runs in
14 > [headless](https://developer.chrome.com/articles/new-headless/)
15 > mode by default, but can be configured to run in full ("headful")
16 > Chrome/Chromium.
18 #### What can I do?
20 Most things that you can do manually in the browser can be done using Puppeteer!
21 Here are a few examples to get you started:
23 - Generate screenshots and PDFs of pages.
24 - Crawl a SPA (Single-Page Application) and generate pre-rendered content (i.e.
25   "SSR" (Server-Side Rendering)).
26 - Automate form submission, UI testing, keyboard input, etc.
27 - Create an automated testing environment using the latest JavaScript and
28   browser features.
29 - Capture a
30   [timeline trace](https://developers.google.com/web/tools/chrome-devtools/evaluate-performance/reference)
31   of your site to help diagnose performance issues.
32 - [Test Chrome Extensions](https://pptr.dev/guides/chrome-extensions).
34 ## Getting Started
36 ### Installation
38 To use Puppeteer in your project, run:
40 ```bash
41 npm i puppeteer
42 # or using yarn
43 yarn add puppeteer
44 # or using pnpm
45 pnpm i puppeteer
46 ```
48 When you install Puppeteer, it automatically downloads a recent version of
49 [Chrome for Testing](https://developer.chrome.com/blog/chrome-for-testing/) (~170MB macOS, ~282MB Linux, ~280MB Windows) and a `chrome-headless-shell` binary (starting with Puppeteer v21.6.0) that is [guaranteed to
50 work](https://pptr.dev/faq#q-why-doesnt-puppeteer-vxxx-work-with-chromium-vyyy)
51 with Puppeteer. The browser is downloaded to the `$HOME/.cache/puppeteer` folder
52 by default (starting with Puppeteer v19.0.0). See [configuration](https://pptr.dev/api/puppeteer.configuration) for configuration options and environmental variables to control the download behavor.
54 If you deploy a project using Puppeteer to a hosting provider, such as Render or
55 Heroku, you might need to reconfigure the location of the cache to be within
56 your project folder (see an example below) because not all hosting providers
57 include `$HOME/.cache` into the project's deployment.
59 For a version of Puppeteer without the browser installation, see
60 [`puppeteer-core`](#puppeteer-core).
62 If used with TypeScript, the minimum supported TypeScript version is `4.7.4`.
64 #### Configuration
66 Puppeteer uses several defaults that can be customized through configuration
67 files.
69 For example, to change the default cache directory Puppeteer uses to install
70 browsers, you can add a `.puppeteerrc.cjs` (or `puppeteer.config.cjs`) at the
71 root of your application with the contents
73 ```js
74 const {join} = require('path');
76 /**
77  * @type {import("puppeteer").Configuration}
78  */
79 module.exports = {
80   // Changes the cache location for Puppeteer.
81   cacheDirectory: join(__dirname, '.cache', 'puppeteer'),
83 ```
85 After adding the configuration file, you will need to remove and reinstall
86 `puppeteer` for it to take effect.
88 See the [configuration guide](https://pptr.dev/guides/configuration) for more
89 information.
91 #### `puppeteer-core`
93 For every release since v1.7.0 we publish two packages:
95 - [`puppeteer`](https://www.npmjs.com/package/puppeteer)
96 - [`puppeteer-core`](https://www.npmjs.com/package/puppeteer-core)
98 `puppeteer` is a _product_ for browser automation. When installed, it downloads
99 a version of Chrome, which it then drives using `puppeteer-core`. Being an
100 end-user product, `puppeteer` automates several workflows using reasonable
101 defaults [that can be customized](https://pptr.dev/guides/configuration).
103 `puppeteer-core` is a _library_ to help drive anything that supports DevTools
104 protocol. Being a library, `puppeteer-core` is fully driven through its
105 programmatic interface implying no defaults are assumed and `puppeteer-core`
106 will not download Chrome when installed.
108 You should use `puppeteer-core` if you are
109 [connecting to a remote browser](https://pptr.dev/api/puppeteer.puppeteer.connect)
110 or [managing browsers yourself](https://pptr.dev/browsers-api/).
111 If you are managing browsers yourself, you will need to call
112 [`puppeteer.launch`](https://pptr.dev/api/puppeteer.puppeteernode.launch) with
113 an explicit
114 [`executablePath`](https://pptr.dev/api/puppeteer.launchoptions)
115 (or [`channel`](https://pptr.dev/api/puppeteer.launchoptions) if it's
116 installed in a standard location).
118 When using `puppeteer-core`, remember to change the import:
120 ```ts
121 import puppeteer from 'puppeteer-core';
124 ### Usage
126 Puppeteer follows the latest
127 [maintenance LTS](https://github.com/nodejs/Release#release-schedule) version of
128 Node.
130 Puppeteer will be familiar to people using other browser testing frameworks. You
131 [launch](https://pptr.dev/api/puppeteer.puppeteernode.launch)/[connect](https://pptr.dev/api/puppeteer.puppeteernode.connect)
132 a [browser](https://pptr.dev/api/puppeteer.browser),
133 [create](https://pptr.dev/api/puppeteer.browser.newpage) some
134 [pages](https://pptr.dev/api/puppeteer.page), and then manipulate them with
135 [Puppeteer's API](https://pptr.dev/api).
137 For more in-depth usage, check our [guides](https://pptr.dev/category/guides)
138 and [examples](https://github.com/puppeteer/puppeteer/tree/main/examples).
140 #### Example
142 The following example searches [developer.chrome.com](https://developer.chrome.com/) for blog posts with text "automate beyond recorder", click on the first result and print the full title of the blog post.
144 ```ts
145 import puppeteer from 'puppeteer';
147 (async () => {
148   // Launch the browser and open a new blank page
149   const browser = await puppeteer.launch();
150   const page = await browser.newPage();
152   // Navigate the page to a URL
153   await page.goto('https://developer.chrome.com/');
155   // Set screen size
156   await page.setViewport({width: 1080, height: 1024});
158   // Type into search box
159   await page.type('.devsite-search-field', 'automate beyond recorder');
161   // Wait and click on first result
162   const searchResultSelector = '.devsite-result-item-link';
163   await page.waitForSelector(searchResultSelector);
164   await page.click(searchResultSelector);
166   // Locate the full title with a unique string
167   const textSelector = await page.waitForSelector(
168     'text/Customize and automate'
169   );
170   const fullTitle = await textSelector?.evaluate(el => el.textContent);
172   // Print the full title
173   console.log('The title of this blog post is "%s".', fullTitle);
175   await browser.close();
176 })();
179 ### Default runtime settings
181 **1. Uses Headless mode**
183 By default Puppeteer launches Chrome in
184 [the Headless mode](https://developer.chrome.com/articles/new-headless/).
186 ```ts
187 const browser = await puppeteer.launch();
188 // Equivalent to
189 const browser = await puppeteer.launch({headless: true});
192 Before v22, Puppeteer launched the [old Headless mode](https://developer.chrome.com/articles/new-headless/) by default.
193 The old headless mode is now known as
194 [`chrome-headless-shell`](https://developer.chrome.com/blog/chrome-headless-shell)
195 and ships as a separate binary. `chrome-headless-shell` does not match the
196 behavior of the regular Chrome completely but it is currently more performant
197 for automation tasks where the complete Chrome feature set is not needed. If the performance
198 is more important for your use case, switch to `chrome-headless-shell` as following:
200 ```ts
201 const browser = await puppeteer.launch({headless: 'shell'});
204 To launch a "headful" version of Chrome, set the
205 [`headless`](https://pptr.dev/api/puppeteer.browserlaunchargumentoptions) to `false`
206 option when launching a browser:
208 ```ts
209 const browser = await puppeteer.launch({headless: false});
212 **2. Runs a bundled version of Chrome**
214 By default, Puppeteer downloads and uses a specific version of Chrome so its
215 API is guaranteed to work out of the box. To use Puppeteer with a different
216 version of Chrome or Chromium, pass in the executable's path when creating a
217 `Browser` instance:
219 ```ts
220 const browser = await puppeteer.launch({executablePath: '/path/to/Chrome'});
223 You can also use Puppeteer with Firefox. See
224 [status of cross-browser support](https://pptr.dev/faq/#q-what-is-the-status-of-cross-browser-support) for
225 more information.
228 [`this article`](https://www.howtogeek.com/202825/what%E2%80%99s-the-difference-between-chromium-and-chrome/)
229 for a description of the differences between Chromium and Chrome.
230 [`This article`](https://chromium.googlesource.com/chromium/src/+/refs/heads/main/docs/chromium_browser_vs_google_chrome.md)
231 describes some differences for Linux users.
233 **3. Creates a fresh user profile**
235 Puppeteer creates its own browser user profile which it **cleans up on every
236 run**.
238 #### Using Docker
240 See our [Docker guide](https://pptr.dev/guides/docker).
242 #### Using Chrome Extensions
244 See our [Chrome extensions guide](https://pptr.dev/guides/chrome-extensions).
246 ## Resources
248 - [API Documentation](https://pptr.dev/api)
249 - [Guides](https://pptr.dev/category/guides)
250 - [Examples](https://github.com/puppeteer/puppeteer/tree/main/examples)
251 - [Community list of Puppeteer resources](https://github.com/transitive-bullshit/awesome-puppeteer)
253 ## Contributing
255 Check out our [contributing guide](https://pptr.dev/contributing) to get an
256 overview of Puppeteer development.
258 ## FAQ
260 Our [FAQ](https://pptr.dev/faq) has migrated to
261 [our site](https://pptr.dev/faq).