UNPKG

6.87 kBMarkdownView Raw
1# Puppeteer [![Build Status](https://travis-ci.com/GoogleChrome/puppeteer.svg?token=8jabovWqb8afz5RDcYqx&branch=master)](https://travis-ci.com/GoogleChrome/puppeteer) [![NPM puppeteer package](https://img.shields.io/npm/v/puppeteer.svg)](https://npmjs.org/package/puppeteer)
2
3<img src="https://user-images.githubusercontent.com/39191/29330067-dfc2be5a-81ac-11e7-9cc2-c569dd5f078c.png" height="200" align="right">
4
5###### [API](docs/api.md) | [FAQ](#faq) | [Contributing](https://github.com/GoogleChrome/puppeteer/blob/master/CONTRIBUTING.md)
6
7> Puppeteer is a Node library which provides a high-level API to control [headless](https://developers.google.com/web/updates/2017/04/headless-chrome) Chrome over the [DevTools Protocol](https://chromedevtools.github.io/devtools-protocol/). It can also be configured to use full (non-headless) Chrome.
8
9###### What can I do?
10
11Most things that you can do manually in the browser can be done using Puppeteer! Here are a few examples to get you started:
12
13* Generate screenshots and PDFs of pages.
14* Crawl a SPA and generate pre-rendered content (i.e. "SSR").
15* Scrape content from websites.
16* Automate form submission, UI testing, keyboard input, etc.
17* 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.
18* Capture a [timeline trace](https://developers.google.com/web/tools/chrome-devtools/evaluate-performance/reference) of your site to help diagnose performance issues.
19
20## Getting Started
21
22### Installation
23
24*Puppeteer requires Node version 7.10 or greater*
25
26To use Puppeteer in your project, run:
27```
28yarn add puppeteer
29# or "npm i puppeteer"
30```
31
32> **Note**: When you install Puppeteer, it downloads a recent version of Chromium (~71Mb Mac, ~90Mb Linux, ~110Mb Win) that is guaranteed to work with the API.
33
34### Usage
35
36Puppeteer will be familiar to using other browser testing frameworks. You create an instance
37of `Browser`, open pages, and then manipulate them with [Puppeteer's API](https://github.com/GoogleChrome/puppeteer/blob/master/docs/api.md#).
38
39**Example** - navigating to https://example.com and saving a screenshot as *example.png*:
40
41```js
42const puppeteer = require('puppeteer');
43
44(async() => {
45
46const browser = await puppeteer.launch();
47const page = await browser.newPage();
48await page.goto('https://example.com');
49await page.screenshot({path: 'example.png'});
50
51browser.close();
52})();
53```
54
55Puppeteer sets an initial page size to 800px x 600px, which defines the screenshot size. The page size can be customized with [`Page.setViewport()`](https://github.com/GoogleChrome/puppeteer/blob/master/docs/api.md#pagesetviewportviewport).
56
57**Example** - create a PDF.
58
59```js
60const puppeteer = require('puppeteer');
61
62(async() => {
63
64const browser = await puppeteer.launch();
65const page = await browser.newPage();
66await page.goto('https://news.ycombinator.com', {waitUntil: 'networkidle'});
67await page.pdf({path: 'hn.pdf', format: 'A4'});
68
69browser.close();
70})();
71```
72
73See [`Page.pdf()`](https://github.com/GoogleChrome/puppeteer/blob/master/docs/api.md#pagepdfoptions) for more information about creating pdfs.
74
75## Default runtime settings
76
77**1. Uses Headless mode**
78
79Puppeteer 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/GoogleChrome/puppeteer/blob/master/docs/api.md#puppeteerlaunchoptions) when creating a browser:
80
81```js
82const browser = await puppeteer.launch({headless: false});
83```
84
85**2. Runs a bundled version of Chromium**
86
87By default, Puppeteer downloads and uses a specific version of Chromium so its API
88is guaranteed to work out of the box. To use Puppeteer with a different version of Chrome,
89pass in the executable's path when creating a `Browser` instance:
90
91```js
92const browser = await puppeteer.launch({executablePath: '/path/to/Chrome'});
93```
94
95See [`Puppeteer.launch()`](https://github.com/GoogleChrome/puppeteer/blob/master/docs/api.md#puppeteerlaunchoptions) for more information.
96
97**3. Creates a fresh user profile**
98
99Puppeteer creates its own Chromium user profile which it **cleans up on every run**.
100
101## API Documentation
102
103Explore the [API documentation](docs/api.md) and [examples](https://github.com/GoogleChrome/puppeteer/tree/master/examples/) to learn more.
104
105## Contributing to Puppeteer
106
107Check out [contributing guide](https://github.com/GoogleChrome/puppeteer/blob/master/CONTRIBUTING.md) to get an overview of Puppeteer development.
108
109# FAQ
110
111#### Q: Which Chromium version does Puppeteer use?
112
113Look for `chromium_revision` in [package.json](https://github.com/GoogleChrome/puppeteer/blob/master/package.json).
114
115Puppeteer bundles Chromium to ensure that the latest features it uses are guaranteed to be available. As the DevTools protocol and browser improve over time, Puppeteer will be updated to depend on newer versions of Chromium.
116
117
118#### Q: What is the difference between Puppeteer, Selenium / WebDriver, and PhantomJS?
119
120Selenium / WebDriver is a well-established cross-browser API that is useful for testing cross-browser support.
121
122Puppeteer works only with Chrome. However, many teams only run unit tests with a single browser (e.g. PhantomJS). In non-testing use cases, Puppeteer provides a powerful but simple API because it's only targeting one browser that enables you to rapidly develop automation scripts.
123
124Puppeteer uses the latest versions of Chromium.
125
126
127#### Q: Who maintains Puppeteer?
128
129The Chrome DevTools team maintains the library, but we'd love your help and expertise on the project!
130See [Contributing](https://github.com/GoogleChrome/puppeteer/blob/master/CONTRIBUTING.md).
131
132#### Q: Why is the Chrome team building Puppeteer?
133
134The goals of the project are simple:
135
136- Provide a slim, canonical library that highlights the capabilities of the [DevTools Protocol](https://chromedevtools.github.io/devtools-protocol/).
137- Provide a reference implementation for similar testing libraries. Eventually, these
138other frameworks could adopt Puppeteer as their foundational layer.
139- Grow the adoption of headless/automated browser testing.
140- Help dogfood new DevTools Protocol features...and catch bugs!
141- Learn more about the pain points of automated browser testing and help fill those gaps.
142
143#### Q: How does Puppeteer compare with other headless Chrome projects?
144
145The past few months have brought [several new libraries for automating headless Chrome](https://medium.com/@kensoh/chromeless-chrominator-chromy-navalia-lambdium-ghostjs-autogcd-ef34bcd26907). As the team authoring the underlying DevTools Protocol, we're excited to witness and support this flourishing ecosystem.
146
147We've reached out to a number of these projects to see if there are opportunities for collaboration, and we're happy to do what we can to help.