SlideShare a Scribd company logo
Puppeteer
Headless Chrome Node API
Wilson Su
What Is a Headless Browser?
A headless browser is a web browser without
a graphical user interface. Headless browsers
provide automated control of a web page in
an environment similar to popular web
browsers, but are executed via a
command-line interface or using network
communication. – Wiki
Headless Browser Command Line Features
1. // Taking screenshots
2. $ google-chrome --headless --disable-gpu --screenshot
https://example.com/
3.
4. // Creating a PDF
5. $ google-chrome --headless --disable-gpu --print-to-pdf
https://example.com/
Puppeteer
https://github.com/GoogleChrome/puppeteer
Use Cases
✓ Take screenshots
✓ Generate PDFs
✓ Scrape content
✓ Automate UI testing
✓ Capture a timeline trace
✗ DDoS attack
✗ Fake page loads
✗ Credential stuffing
Examples
1. Take screenshosts
2. Generate PDFs
3. Automate interactions
4. Scrape content from websites
5. Emulate device metrics and user agent
6. Handle events
7. Create trace files
Taking Screenshots
1. const puppeteer = require('puppeteer');
2. (async () => {
3. const browser = await puppeteer.launch();
4. const page = await browser.newPage();
5. await page.setViewport({ width: 767, height: 1024 });
6. await page.goto('https://getbootstrap.com/');
7. await page.screenshot({
8. path: 'bootstrap.png',
9. fullPage: true
10. });
11. browser.close();
12. })();
Generating PDFs
1. const puppeteer = require('puppeteer');
2. (async () => {
3. const browser = await puppeteer.launch();
4. const page = await browser.newPage();
5. await page.goto('https://github.com/');
6. await page.pdf({ path: 'github.pdf', format: 'A4' });
7. browser.close();
8. })();
Automating Interactions
1. const puppeteer = require('puppeteer');
2. (async () => {
3. const browser = await puppeteer.launch();
4. const page = await browser.newPage();
5. await page.goto('https://www.npmjs.com');
6. await page.focus('#site-search');
7. await page.type('react');
8. await page.click('#npm-search > button');
9. await page.waitForNavigation();
10. await page.screenshot({ path: 'npmjs.png' });
11. browser.close();
12. })();
Scraping Content From Websites
1. const puppeteer = require('puppeteer');
2. (async () => {
3. const browser = await puppeteer.launch();
4. const page = await browser.newPage();
5. await page.goto('https://www.npmjs.com/search?q=react');
6. const packages = await page.evaluate(() => {
7. var nodes = document.querySelectorAll('.packageName');
8. return [...nodes].map(el => el.textContent);
9. });
10. console.log(packages);
11. browser.close();
12. })();
Emulating Device Metrics and User Agent
1. const puppeteer = require('puppeteer');
2. const devices = require('puppeteer/DeviceDescriptors');
3. const iphone6 = devices['iPhone 6'];
4. (async () => {
5. const browser = await puppeteer.launch();
6. const page = await browser.newPage();
7. await page.emulate(iphone6);
8. await page.goto('https://www.facebook.com/');
9. await page.screenshot({ path: 'facebook.png' });
10. const host = await page.evaluate(() => location.host);
11. console.log(host); // 'm.facebook.com'
12. browser.close();
13. })();
Handling Events
1. const puppeteer = require('puppeteer');
2. (async () => {
3. const browser = await puppeteer.launch();
4. const page = await browser.newPage();
5. page.on('console', (...args) => {
6. console.log('[Browser]', ...args);
7. });
8. await page.goto('http://jsbin.com');
9. browser.close();
10. })();
Creating Trace Files
1. const puppeteer = require('puppeteer');
2. (async () => {
3. const browser = await puppeteer.launch();
4. const page = await browser.newPage();
5. await page.tracing.start({ path: 'trace.json' });
6. await page.goto('https://webpack.js.org/', {
7. waitUntil: 'networkidle'
8. });
9. await page.tracing.stop();
10. browser.close();
11. })();
Get Samples
https://github.com/sweekson/puppeteer-samples
Further Information
● Selenium - Web Browser Automation
● WebdriverIO - WebDriver bindings for Node.js
● PhantomJS
● SlimerJS
● Chromeless
Reference
● Headless browser - Wikipedia
● Getting Started with Headless Chrome  |  Web  |  Google Developers
● What Is a Headless Browser, and What's It Good For?
● Jack Histon - Making a Master Puppeteer

More Related Content

What's hot (20)

PDF
Introduction to Robot Framework
Somkiat Puisungnoen
 
PDF
Docker For Windows | Setting Up Docker On Windows | Edureka
Edureka!
 
PPTX
Cypress Automation
Susantha Pathirana
 
PDF
CI/CD with Github Actions
Md. Minhazul Haque
 
PPTX
CI/CD on AWS
Bhargav Amin
 
PDF
Playwright: A New Test Automation Framework for the Modern Web
Applitools
 
PDF
Cypress-vs-Playwright: Let the Code Speak
Applitools
 
PDF
Golang
Felipe Mamud
 
PDF
The Rust Programming Language: an Overview
Roberto Casadei
 
PDF
Introduction to Github Actions
Knoldus Inc.
 
PDF
gRPC in Go
Almog Baku
 
ODP
Testing RESTful Webservices using the REST-assured framework
Micha Kops
 
PDF
OWASP SF - Reviewing Modern JavaScript Applications
Lewis Ardern
 
PPTX
Progressive Web App Testing With Cypress.io
Knoldus Inc.
 
PDF
The Rust Programming Language
Mario Alexandro Santini
 
PPTX
Introduction to go lang
Amal Mohan N
 
DOCX
UIAutomator
Sandip Ganguli
 
PDF
Katalon: Mobile and Browser-Based Automation | Quality Jam 2018
Katalon Studio
 
PDF
Appium basics
Syam Sasi
 
Introduction to Robot Framework
Somkiat Puisungnoen
 
Docker For Windows | Setting Up Docker On Windows | Edureka
Edureka!
 
Cypress Automation
Susantha Pathirana
 
CI/CD with Github Actions
Md. Minhazul Haque
 
CI/CD on AWS
Bhargav Amin
 
Playwright: A New Test Automation Framework for the Modern Web
Applitools
 
Cypress-vs-Playwright: Let the Code Speak
Applitools
 
Golang
Felipe Mamud
 
The Rust Programming Language: an Overview
Roberto Casadei
 
Introduction to Github Actions
Knoldus Inc.
 
gRPC in Go
Almog Baku
 
Testing RESTful Webservices using the REST-assured framework
Micha Kops
 
OWASP SF - Reviewing Modern JavaScript Applications
Lewis Ardern
 
Progressive Web App Testing With Cypress.io
Knoldus Inc.
 
The Rust Programming Language
Mario Alexandro Santini
 
Introduction to go lang
Amal Mohan N
 
UIAutomator
Sandip Ganguli
 
Katalon: Mobile and Browser-Based Automation | Quality Jam 2018
Katalon Studio
 
Appium basics
Syam Sasi
 

Similar to Puppeteer - Headless Chrome Node API (20)

PPTX
Puppeteer (JavaScript library for UI testing)
techticfaizan
 
PDF
Puppeteer can automate that! - Frontmania
Önder Ceylan
 
PDF
Puppeteer can automate that! - HolyJS Piter 2020
Önder Ceylan
 
PPTX
Headless browser: puppeteer and git client : GitKraken
SheikhMoonwaraAnjumM
 
PDF
Playwright vs. Puppeteer : Choosing the Right Browser Automation Library.pdf
Steve Wortham
 
PDF
Puppeteerのお話
Shinji Kobayashi
 
PDF
Intro to Puppeteer
Islam AlZatary
 
PPTX
How to start ui automation in 15 mins with Puppeteer
Oleksandr Pelykh
 
PDF
vodQA Pune (2019) - Browser automation using dev tools
vodQA
 
PPTX
APIs for Browser Automation (MoT Meetup 2024)
Boni García
 
PPTX
Drive chrome(headless) with puppeteer
VodqaBLR
 
PDF
Export pdf with puppeteer
Knoldus Inc.
 
PDF
Detecting headless browsers
Sergey Shekyan
 
PDF
Puppeteer: Getting Started
HyunSeob Lee
 
PDF
Puppeteer : Is it time to ditch Selenium?
Mindfire LLC
 
PPTX
Scraping the web with Laravel, Dusk, Docker, and PHP
Paul Redmond
 
PDF
10 Best Puppeteer Alternatives in Web Scraping.pdf
X-Byte Enterprise Crawling
 
PPTX
BrowserArchitecture_ClientSide.pptx
MuhammadBilal187526
 
PDF
BrowserArchitecture_ClientSide.pdf
MuhammadBilal187526
 
Puppeteer (JavaScript library for UI testing)
techticfaizan
 
Puppeteer can automate that! - Frontmania
Önder Ceylan
 
Puppeteer can automate that! - HolyJS Piter 2020
Önder Ceylan
 
Headless browser: puppeteer and git client : GitKraken
SheikhMoonwaraAnjumM
 
Playwright vs. Puppeteer : Choosing the Right Browser Automation Library.pdf
Steve Wortham
 
Puppeteerのお話
Shinji Kobayashi
 
Intro to Puppeteer
Islam AlZatary
 
How to start ui automation in 15 mins with Puppeteer
Oleksandr Pelykh
 
vodQA Pune (2019) - Browser automation using dev tools
vodQA
 
APIs for Browser Automation (MoT Meetup 2024)
Boni García
 
Drive chrome(headless) with puppeteer
VodqaBLR
 
Export pdf with puppeteer
Knoldus Inc.
 
Detecting headless browsers
Sergey Shekyan
 
Puppeteer: Getting Started
HyunSeob Lee
 
Puppeteer : Is it time to ditch Selenium?
Mindfire LLC
 
Scraping the web with Laravel, Dusk, Docker, and PHP
Paul Redmond
 
10 Best Puppeteer Alternatives in Web Scraping.pdf
X-Byte Enterprise Crawling
 
BrowserArchitecture_ClientSide.pptx
MuhammadBilal187526
 
BrowserArchitecture_ClientSide.pdf
MuhammadBilal187526
 
Ad

More from Wilson Su (13)

PDF
Mirage For Beginners
Wilson Su
 
PDF
NestJS
Wilson Su
 
PDF
The Jira How-To Guide
Wilson Su
 
PDF
The Future of Web Development
Wilson Su
 
PDF
Web Usability
Wilson Su
 
PDF
Practical JavaScript Programming - Session 8/8
Wilson Su
 
PDF
Practical JavaScript Programming - Session 7/8
Wilson Su
 
PDF
Practical JavaScript Programming - Session 6/8
Wilson Su
 
PDF
Practical JavaScript Programming - Session 5/8
Wilson Su
 
PDF
Practical JavaScript Programming - Session 4/8
Wilson Su
 
PDF
Practical JavaScript Programming - Session 3/8
Wilson Su
 
PDF
Practical JavaScript Programming - Session 2/8
Wilson Su
 
PDF
Practical JavaScript Programming - Session 1/8
Wilson Su
 
Mirage For Beginners
Wilson Su
 
NestJS
Wilson Su
 
The Jira How-To Guide
Wilson Su
 
The Future of Web Development
Wilson Su
 
Web Usability
Wilson Su
 
Practical JavaScript Programming - Session 8/8
Wilson Su
 
Practical JavaScript Programming - Session 7/8
Wilson Su
 
Practical JavaScript Programming - Session 6/8
Wilson Su
 
Practical JavaScript Programming - Session 5/8
Wilson Su
 
Practical JavaScript Programming - Session 4/8
Wilson Su
 
Practical JavaScript Programming - Session 3/8
Wilson Su
 
Practical JavaScript Programming - Session 2/8
Wilson Su
 
Practical JavaScript Programming - Session 1/8
Wilson Su
 
Ad

Recently uploaded (20)

DOC
MRRS Strength and Durability of Concrete
CivilMythili
 
PPTX
ISO/IEC JTC 1/WG 9 (MAR) Convenor Report
Kurata Takeshi
 
PPTX
site survey architecture student B.arch.
sri02032006
 
PDF
Introduction to Productivity and Quality
মোঃ ফুরকান উদ্দিন জুয়েল
 
PPTX
Hashing Introduction , hash functions and techniques
sailajam21
 
PPTX
Types of Bearing_Specifications_PPT.pptx
PranjulAgrahariAkash
 
PPTX
Pharmaceuticals and fine chemicals.pptxx
jaypa242004
 
PDF
Ethics and Trustworthy AI in Healthcare – Governing Sensitive Data, Profiling...
AlqualsaDIResearchGr
 
PPTX
Benefits_^0_Challigi😙🏡💐8fenges[1].pptx
akghostmaker
 
PDF
Book.pdf01_Intro.ppt algorithm for preperation stu used
archu26
 
PPTX
Green Building & Energy Conservation ppt
Sagar Sarangi
 
PPTX
REINFORCEMENT AS CONSTRUCTION MATERIALS.pptx
mohaiminulhaquesami
 
PPTX
Destructive Tests corrosion engineer (1).pptx
zeidali3
 
PPTX
Introduction to Design of Machine Elements
PradeepKumarS27
 
PPTX
Day2 B2 Best.pptx
helenjenefa1
 
PPTX
MPMC_Module-2 xxxxxxxxxxxxxxxxxxxxx.pptx
ShivanshVaidya5
 
PPTX
Solar Thermal Energy System Seminar.pptx
Gpc Purapuza
 
PPTX
Break Statement in Programming with 6 Real Examples
manojpoojary2004
 
PDF
MAD Unit - 1 Introduction of Android IT Department
JappanMavani
 
PDF
UNIT-4-FEEDBACK AMPLIFIERS AND OSCILLATORS (1).pdf
Sridhar191373
 
MRRS Strength and Durability of Concrete
CivilMythili
 
ISO/IEC JTC 1/WG 9 (MAR) Convenor Report
Kurata Takeshi
 
site survey architecture student B.arch.
sri02032006
 
Introduction to Productivity and Quality
মোঃ ফুরকান উদ্দিন জুয়েল
 
Hashing Introduction , hash functions and techniques
sailajam21
 
Types of Bearing_Specifications_PPT.pptx
PranjulAgrahariAkash
 
Pharmaceuticals and fine chemicals.pptxx
jaypa242004
 
Ethics and Trustworthy AI in Healthcare – Governing Sensitive Data, Profiling...
AlqualsaDIResearchGr
 
Benefits_^0_Challigi😙🏡💐8fenges[1].pptx
akghostmaker
 
Book.pdf01_Intro.ppt algorithm for preperation stu used
archu26
 
Green Building & Energy Conservation ppt
Sagar Sarangi
 
REINFORCEMENT AS CONSTRUCTION MATERIALS.pptx
mohaiminulhaquesami
 
Destructive Tests corrosion engineer (1).pptx
zeidali3
 
Introduction to Design of Machine Elements
PradeepKumarS27
 
Day2 B2 Best.pptx
helenjenefa1
 
MPMC_Module-2 xxxxxxxxxxxxxxxxxxxxx.pptx
ShivanshVaidya5
 
Solar Thermal Energy System Seminar.pptx
Gpc Purapuza
 
Break Statement in Programming with 6 Real Examples
manojpoojary2004
 
MAD Unit - 1 Introduction of Android IT Department
JappanMavani
 
UNIT-4-FEEDBACK AMPLIFIERS AND OSCILLATORS (1).pdf
Sridhar191373
 

Puppeteer - Headless Chrome Node API

  • 2. What Is a Headless Browser? A headless browser is a web browser without a graphical user interface. Headless browsers provide automated control of a web page in an environment similar to popular web browsers, but are executed via a command-line interface or using network communication. – Wiki
  • 3. Headless Browser Command Line Features 1. // Taking screenshots 2. $ google-chrome --headless --disable-gpu --screenshot https://example.com/ 3. 4. // Creating a PDF 5. $ google-chrome --headless --disable-gpu --print-to-pdf https://example.com/
  • 5. Use Cases ✓ Take screenshots ✓ Generate PDFs ✓ Scrape content ✓ Automate UI testing ✓ Capture a timeline trace ✗ DDoS attack ✗ Fake page loads ✗ Credential stuffing
  • 6. Examples 1. Take screenshosts 2. Generate PDFs 3. Automate interactions 4. Scrape content from websites 5. Emulate device metrics and user agent 6. Handle events 7. Create trace files
  • 7. Taking Screenshots 1. const puppeteer = require('puppeteer'); 2. (async () => { 3. const browser = await puppeteer.launch(); 4. const page = await browser.newPage(); 5. await page.setViewport({ width: 767, height: 1024 }); 6. await page.goto('https://getbootstrap.com/'); 7. await page.screenshot({ 8. path: 'bootstrap.png', 9. fullPage: true 10. }); 11. browser.close(); 12. })();
  • 8. Generating PDFs 1. const puppeteer = require('puppeteer'); 2. (async () => { 3. const browser = await puppeteer.launch(); 4. const page = await browser.newPage(); 5. await page.goto('https://github.com/'); 6. await page.pdf({ path: 'github.pdf', format: 'A4' }); 7. browser.close(); 8. })();
  • 9. Automating Interactions 1. const puppeteer = require('puppeteer'); 2. (async () => { 3. const browser = await puppeteer.launch(); 4. const page = await browser.newPage(); 5. await page.goto('https://www.npmjs.com'); 6. await page.focus('#site-search'); 7. await page.type('react'); 8. await page.click('#npm-search > button'); 9. await page.waitForNavigation(); 10. await page.screenshot({ path: 'npmjs.png' }); 11. browser.close(); 12. })();
  • 10. Scraping Content From Websites 1. const puppeteer = require('puppeteer'); 2. (async () => { 3. const browser = await puppeteer.launch(); 4. const page = await browser.newPage(); 5. await page.goto('https://www.npmjs.com/search?q=react'); 6. const packages = await page.evaluate(() => { 7. var nodes = document.querySelectorAll('.packageName'); 8. return [...nodes].map(el => el.textContent); 9. }); 10. console.log(packages); 11. browser.close(); 12. })();
  • 11. Emulating Device Metrics and User Agent 1. const puppeteer = require('puppeteer'); 2. const devices = require('puppeteer/DeviceDescriptors'); 3. const iphone6 = devices['iPhone 6']; 4. (async () => { 5. const browser = await puppeteer.launch(); 6. const page = await browser.newPage(); 7. await page.emulate(iphone6); 8. await page.goto('https://www.facebook.com/'); 9. await page.screenshot({ path: 'facebook.png' }); 10. const host = await page.evaluate(() => location.host); 11. console.log(host); // 'm.facebook.com' 12. browser.close(); 13. })();
  • 12. Handling Events 1. const puppeteer = require('puppeteer'); 2. (async () => { 3. const browser = await puppeteer.launch(); 4. const page = await browser.newPage(); 5. page.on('console', (...args) => { 6. console.log('[Browser]', ...args); 7. }); 8. await page.goto('http://jsbin.com'); 9. browser.close(); 10. })();
  • 13. Creating Trace Files 1. const puppeteer = require('puppeteer'); 2. (async () => { 3. const browser = await puppeteer.launch(); 4. const page = await browser.newPage(); 5. await page.tracing.start({ path: 'trace.json' }); 6. await page.goto('https://webpack.js.org/', { 7. waitUntil: 'networkidle' 8. }); 9. await page.tracing.stop(); 10. browser.close(); 11. })();
  • 15. Further Information ● Selenium - Web Browser Automation ● WebdriverIO - WebDriver bindings for Node.js ● PhantomJS ● SlimerJS ● Chromeless
  • 16. Reference ● Headless browser - Wikipedia ● Getting Started with Headless Chrome  |  Web  |  Google Developers ● What Is a Headless Browser, and What's It Good For? ● Jack Histon - Making a Master Puppeteer