5. What is Puppeteer ?
According to Wikipedia : A puppeteer is a person who
manipulates an inanimate object that might be shaped like a
human, animal or mythical creature, or another object to
create the illusion that the puppet is “alive”.
With Google Puppeteer, the same concept, Google Chrome
is the puppet that we will manipulate to do some tasks on
web.
6. Puppeteer is a Node library which provides a high-level API to control headless
Chrome or Chromium over the DevTools Protocol. It can also be configured to
use full (non-headless) Chrome or Chromium.
7. What Puppeteer can do ?
Most things that you can do manually in the
browser can be done using Puppeteer! Here
are a few examples to get you started:
● Generate screenshots and PDFs of pages.
● Crawl a SPA (Single-Page Application) and
generate pre-rendered content (i.e. "SSR"
(Server-Side Rendering)).
● Automate form submission, UI testing,
keyboard input, etc.
● 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.
● Capture a timeline trace of your site to
help diagnose performance issues.
● Test Chrome Extensions.
8. Frequently Asked Questions
Q: Who maintains Puppeteer?
The Chrome DevTools team maintains the library, but we'd
love your help and expertise on the project! See Contributing.
Q: Is Puppeteer replacing Selenium/WebDriver?
No. Both projects are valuable for very different reasons:
● Selenium/WebDriver focuses on cross-browser
automation; its value proposition is a single standard
API that works across all major browsers.
● Puppeteer focuses on Chromium; its value proposition
is richer functionality and higher reliability.
Q: What are Puppeteer’s goals and principles?
The goals of the project are:
● Provide a slim, canonical library that highlights the
capabilities of the DevTools Protocol.
● Provide a reference implementation for similar testing
libraries. Eventually, these other frameworks could
adopt Puppeteer as their foundational layer.
● Grow the adoption of headless/automated browser
testing.
● Help dogfood new DevTools Protocol features...and
catch bugs!
● Learn more about the pain points of automated
browser testing and help fill those gaps.
9. Frequently Asked Questions
Q: What features does Puppeteer not support?
You 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.) There
are two reasons for this:
● Puppeteer is bundled with Chromium--not Chrome--
and so by default, it inherits all of Chromium's media-
related limitations. 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. You should only use this
configuration if you need an official release of Chrome
that supports these media formats.)
● 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).
10. ● Puppeteer communicates with the browser using
DevTools Protocol.
● Browser instance can own multiple browser
contexts.
● BrowserContext instance defines a browsing
session and can own multiple pages.
● Page has at least one frame: main frame. There
might be other frames created by iframe or frame
tags.
● Frame has at least one execution context - the
default execution context - where the frame's
JavaScript is executed. A Frame might have
additional execution contexts that are associated
with extensions.
● Worker has a single execution context and
facilitates interacting with WebWorkers.
12. Installation
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, see Environment variables.
puppeteer-core
Since version 1.7.0 we publish the
puppeteer-core package, a version of
Puppeteer that doesn't download
Chromium by default.
puppeteer
npm i puppeteer
# or "yarn add puppeteer"
puppeteer-core
npm i puppeteer-core
# or "yarn add puppeteer-core"
for more : puppeteer vs. puppeteer-core
29. Tracing
const puppeteer = require('puppeteer');
(async () => {
const browser = await puppeteer.launch();
const page = await browser.newPage();
await page.tracing.start({path: 'trace.json'});
await page.goto('https://www.sonymobile.com/gb');
await page.tracing.stop();
await browser.close();
})();
You can use tracing.start and tracing.stop to create a trace file which can be opened in Chrome DevTools or timeline viewer.
30. Network Throttling Regular 4G vs Good 2G
const puppeteer = require('puppeteer')
puppeteer.launch().then(async browser => {
// Create a new tab
const page = await browser.newPage()
// Connect to Chrome DevTools
const client = await page.target().createCDPSession()
// Set throttling property
await client.send('Network.emulateNetworkConditions', {
'offline': false,
'downloadThroughput': 450 * 1024 / 8,
'uploadThroughput': 150 * 1024 / 8,
'latency': 150
})
// Navigate and take a screenshot
await page.tracing.start({path: 'trace.json'});
await page.goto('https://volkanozdamar.com')
await page.tracing.stop();
await page.screenshot({path: 'network.png'})
await browser.close()
})
const puppeteer = require('puppeteer')
puppeteer.launch().then(async browser => {
// Create a new tab
const page = await browser.newPage()
// Connect to Chrome DevTools
const client = await page.target().createCDPSession()
// Set throttling property
await client.send('Network.emulateNetworkConditions', {
'offline': false,
'downloadThroughput': 4 * 1024 * 1024 / 8,
'uploadThroughput': 3 * 1024 * 1024 / 8,
'latency': 150
})
// Navigate and take a screenshot
await page.tracing.start({path: 'trace.json'});
await page.goto('https://volkanozdamar.com')
await page.tracing.stop();
await page.screenshot({path: 'network.png'})
await browser.close()
})
Source
33. Load Test
npm install -g puppeteer-loadtest
puppeteer-loadtest --file=./test/sample.js --s=100 --c=25 --silent=true
--s flag is to mention sample size
--c flag is to mention number of concurrent executions per sample
--silent boolean to enable or disable logs
svenkatreddy/puppeteer-loadtest