SlideShare uma empresa Scribd logo
1 de 37
Puppeteer
Headless Chrome Node API
Volkan Özdamar - QA Analyst @SONY
Schedule
● What is NodeJS ?
● What is Puppeteer ?
● FAQ
● Installation
● Dive into ...
NodeJS or node is a runtime environment for executing JavaScript Codes.
Browser
V8 V8
Node
Simple Architecture
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.
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.
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.
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.
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).
● 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.
GoogleChrome/puppeteerhttps://pptr.dev
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
Breaking News ...
Announced
Chrome Dev Summit 2017
San Francisco, October 23-24, 2017
First Release
Microsoft is rebuilding its Edge browser on Chromium
Not Official but it is support Firefox too...
It’s is spreading out
Dive into ...
Simple Example
const puppeteer = require('puppeteer');
(async () => {
const browser = await puppeteer.launch();
const page = await browser.newPage();
await page.goto('https://volkanozdamar.com');
await page.screenshot({path: 'page.png'});
await browser.close();
})();
Launch without built-in Chromium
await puppeteer.launch({executablePath: '/path/to/Chrome'});
Simple Example
const puppeteer = require('puppeteer');
(async () => {
const browser = await puppeteer.launch();
const page = await browser.newPage();
await page.goto('https://volkanozdamar.com');
await page.screenshot({path: 'page.png'});
await browser.close();
})();
Disable Headless Mode
await puppeteer.launch({headless: false});
Generating PDF
const puppeteer = require('puppeteer');
(async () => {
const browser = await puppeteer.launch();
const page = await browser.newPage();
await page.goto('https://volkanozdamar.com');
await page.pdf({path: 'printout.pdf',format: 'A4'});
await browser.close();
})();
Generating a pdf is currently only
supported in Chrome headless.
Setting Viewport
const puppeteer = require('puppeteer');
(async () => {
const browser = await puppeteer.launch();
const page = await browser.newPage();
await page.setViewport({
width: 1920,
height: 1080
});
await page.goto('https://volkanozdamar.com');
await page.screenshot({path: 'page.png'});
await browser.close();
})();
Puppeteer sets an initial page size to 800px x 600px, which
defines the screenshot size.
Emulate Mobile Devices
const puppeteer = require('puppeteer');
const devices = require('puppeteer/DeviceDescriptors');
const pixeltwo = devices['Pixel 2'];
puppeteer.launch().then(async browser => {
const page = await browser.newPage();
await page.emulate(pixeltwo);
await page.goto('https://www.sonymobile.com/gb');
await page.screenshot({path: 'mainpage.png',fullPage:false});
await browser.close();
});
Puppeteer has predefined device sizes inside.You
can find those devices in“DeviceDescriptors.js”.
Also you can create your own devices.
DeviceDescriptors.js
Interacting with Page Elements
const puppeteer = require('puppeteer');
(async () => {
const browser = await puppeteer.launch();
const page = await browser.newPage();
await page.goto('https://google.com');
await page.type('#lst-ib','Puppeteer');
await page.click('input[name="btnK"]');
await browser.close();
})();
Web Scraping
const puppeteer = require('puppeteer');
(async () => {
const browser = await puppeteer.launch({headless: false});
const page = await browser.newPage();
await page.goto('https://sonymobile.com/gb');
await page.click('#menu-primary-menu > li:nth-child(2) > a');
await page.waitForSelector('.btn-alt-special');
await page.$('.btn-alt-special');
await page.click('.btn-alt-special');
await page.click('#phones-display_size-lt_5');
const titles = await page.evaluate(
() =>
Array.from(page.$$('div.product-name-wrap a strong'))
.map(partner=>partner.innerText.trim()
)
);
console.log(titles);
await browser.close();
})();
document.querySelectorAll() == page.$$
Snip an element
const puppeteer = require('puppeteer');
(async () => {
const browser = await puppeteer.launch()
const page = await browser.newPage()
await page.goto('https://twitter.com/volkanozdamar')
const timeline = await page.$('.ProfileHeaderCard');
await timeline.screenshot({
path:"profileHeader.png"
});
browser.close();
})();
Add some Coffeé
const puppeteer = require('puppeteer');
describe( 'Test Website', async () => {
it( 'Browser Opens Successfully', async () => {
browser = await puppeteer.launch();
});
it( 'Page Open Successfully', async () => {
page = await browser.newPage();
});
it( 'Website Should Load', async () => {
const response = await page.goto('https://sonymobile.com/gb', {
waitUntil:'domcontentloaded'});
}).timeout(0);
it( 'Browser Closes Successfully', async () => {
await browser.close();
});
});
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.
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
Disable images
await page.setRequestInterception(true);
page.on('request', (req) => {
if(req.resourceType() === 'image'){
req.abort();
}
else {
req.continue();
}
});
Disable CSS and Font
await page.setRequestInterception(true);
page.on('request', (req) => {
if(req.resourceType() == 'stylesheet' || req.resourceType() == 'font' ||
req.resourceType() == 'image'){
req.abort();
}
else {
req.continue();
}
});
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
Accessibility Tree
const puppeteer = require('puppeteer');
const fs = require('fs');
(async () => {
const browser = await puppeteer.launch();
const page = await browser.newPage();
await page.goto('https://www.sonymobile.com/gb');
const snapshot = await page.accessibility.snapshot();
fs.writeFile("snapshot.txt",JSON.stringify(snapshot), function(err)
{
if(err) {
return console.log(err);
}
console.log("The file was saved!");
});
})();
See Also : Accessibility Tree
Accessibility with pa11y
const puppeteer = require('puppeteer');
const pa11y = require('pa11y');
async function runPa11y() {
try {
const browser = await puppeteer.launch({
ignoreHTTPSErrors: true
});
const results =await pa11y('https://sonymobile.com/gb', {
browser: browser
});
browser.close();
// Do something with the results
console.log(results);
} catch (error) {
// Handle the error
}
}
runPa11y();
Google I/0 18
Thanks!
Contact me:
@volkanozdamar
volkan@volkanozdamar.com
https://github.com/volkanozdamar/slides.git

Mais conteúdo relacionado

Mais procurados

Introduction to Progressive Web App
Introduction to Progressive Web AppIntroduction to Progressive Web App
Introduction to Progressive Web AppBinh Bui
 
Understanding REST
Understanding RESTUnderstanding REST
Understanding RESTNitin Pande
 
Introducing Async/Await
Introducing Async/AwaitIntroducing Async/Await
Introducing Async/AwaitValeri Karpov
 
Asynchronous javascript
 Asynchronous javascript Asynchronous javascript
Asynchronous javascriptEman Mohamed
 
React js programming concept
React js programming conceptReact js programming concept
React js programming conceptTariqul islam
 
Angular Data Binding
Angular Data BindingAngular Data Binding
Angular Data BindingDuy Khanh
 
Progressive web apps
Progressive web appsProgressive web apps
Progressive web appsAkshay Sharma
 
Reactjs workshop
Reactjs workshop Reactjs workshop
Reactjs workshop Ahmed rebai
 
Core Web Vitals - The Modern Web Experience
Core Web Vitals - The Modern Web ExperienceCore Web Vitals - The Modern Web Experience
Core Web Vitals - The Modern Web ExperienceCakra Danu Sedayu
 
ARIA_11_12_Practical_Perspective.pptx
ARIA_11_12_Practical_Perspective.pptxARIA_11_12_Practical_Perspective.pptx
ARIA_11_12_Practical_Perspective.pptxMarkSteadman7
 
Playwright: A New Test Automation Framework for the Modern Web
Playwright: A New Test Automation Framework for the Modern WebPlaywright: A New Test Automation Framework for the Modern Web
Playwright: A New Test Automation Framework for the Modern WebApplitools
 
Service Worker Presentation
Service Worker PresentationService Worker Presentation
Service Worker PresentationKyle Dorman
 

Mais procurados (20)

Introduction to Progressive Web App
Introduction to Progressive Web AppIntroduction to Progressive Web App
Introduction to Progressive Web App
 
Understanding REST
Understanding RESTUnderstanding REST
Understanding REST
 
React Native Workshop
React Native WorkshopReact Native Workshop
React Native Workshop
 
Introducing Async/Await
Introducing Async/AwaitIntroducing Async/Await
Introducing Async/Await
 
Asynchronous javascript
 Asynchronous javascript Asynchronous javascript
Asynchronous javascript
 
Api Testing
Api TestingApi Testing
Api Testing
 
React js programming concept
React js programming conceptReact js programming concept
React js programming concept
 
Angular Data Binding
Angular Data BindingAngular Data Binding
Angular Data Binding
 
Progressive web apps
Progressive web appsProgressive web apps
Progressive web apps
 
API for Beginners
API for BeginnersAPI for Beginners
API for Beginners
 
Reactjs workshop
Reactjs workshop Reactjs workshop
Reactjs workshop
 
Core Web Vitals - The Modern Web Experience
Core Web Vitals - The Modern Web ExperienceCore Web Vitals - The Modern Web Experience
Core Web Vitals - The Modern Web Experience
 
React Hooks
React HooksReact Hooks
React Hooks
 
Progressive Web App
Progressive Web AppProgressive Web App
Progressive Web App
 
ARIA_11_12_Practical_Perspective.pptx
ARIA_11_12_Practical_Perspective.pptxARIA_11_12_Practical_Perspective.pptx
ARIA_11_12_Practical_Perspective.pptx
 
Playwright: A New Test Automation Framework for the Modern Web
Playwright: A New Test Automation Framework for the Modern WebPlaywright: A New Test Automation Framework for the Modern Web
Playwright: A New Test Automation Framework for the Modern Web
 
SELENIUM PPT.pdf
SELENIUM PPT.pdfSELENIUM PPT.pdf
SELENIUM PPT.pdf
 
Expressjs
ExpressjsExpressjs
Expressjs
 
Service Worker Presentation
Service Worker PresentationService Worker Presentation
Service Worker Presentation
 
Web AR
Web ARWeb AR
Web AR
 

Semelhante a Puppeteer - Headless Chrome Node API

How to Develop Progressive Web Apps in Flutter – Step by Step Guide.pptx
How to Develop Progressive Web Apps in Flutter – Step by Step Guide.pptxHow to Develop Progressive Web Apps in Flutter – Step by Step Guide.pptx
How to Develop Progressive Web Apps in Flutter – Step by Step Guide.pptxBOSC Tech Labs
 
vodQA Pune (2019) - Browser automation using dev tools
vodQA Pune (2019) - Browser automation using dev toolsvodQA Pune (2019) - Browser automation using dev tools
vodQA Pune (2019) - Browser automation using dev toolsvodQA
 
Desktop apps with node webkit
Desktop apps with node webkitDesktop apps with node webkit
Desktop apps with node webkitPaul Jensen
 
Technical Tips: Visual Regression Testing and Environment Comparison with Bac...
Technical Tips: Visual Regression Testing and Environment Comparison with Bac...Technical Tips: Visual Regression Testing and Environment Comparison with Bac...
Technical Tips: Visual Regression Testing and Environment Comparison with Bac...Building Blocks
 
Headless browser: puppeteer and git client : GitKraken
Headless browser: puppeteer and git client : GitKrakenHeadless browser: puppeteer and git client : GitKraken
Headless browser: puppeteer and git client : GitKrakenSheikhMoonwaraAnjumM
 
DIY: Computer Vision with GWT.
DIY: Computer Vision with GWT.DIY: Computer Vision with GWT.
DIY: Computer Vision with GWT.JooinK
 
DIY- computer vision with GWT
DIY- computer vision with GWTDIY- computer vision with GWT
DIY- computer vision with GWTFrancesca Tosi
 
Lean Drupal Repositories with Composer and Drush
Lean Drupal Repositories with Composer and DrushLean Drupal Repositories with Composer and Drush
Lean Drupal Repositories with Composer and DrushPantheon
 
Infrastructure as code with Puppet and Apache CloudStack
Infrastructure as code with Puppet and Apache CloudStackInfrastructure as code with Puppet and Apache CloudStack
Infrastructure as code with Puppet and Apache CloudStackke4qqq
 
ApacheCloudStack
ApacheCloudStackApacheCloudStack
ApacheCloudStackPuppet
 
Phonegap android angualr material design
Phonegap android angualr material designPhonegap android angualr material design
Phonegap android angualr material designSrinadh Kanugala
 
The Superhero’s Method of Modern HTML5 Development by RapidValue Solutions
The Superhero’s Method of Modern HTML5 Development by RapidValue SolutionsThe Superhero’s Method of Modern HTML5 Development by RapidValue Solutions
The Superhero’s Method of Modern HTML5 Development by RapidValue SolutionsRapidValue
 
Export pdf with puppeteer
Export pdf with puppeteerExport pdf with puppeteer
Export pdf with puppeteerKnoldus Inc.
 
Panther loves Symfony apps
Panther loves Symfony appsPanther loves Symfony apps
Panther loves Symfony appsSimone D'Amico
 
Red Hat Forum Benelux 2015
Red Hat Forum Benelux 2015Red Hat Forum Benelux 2015
Red Hat Forum Benelux 2015Microsoft
 
Flutter vs Java Graphical User Interface Frameworks - text
Flutter vs Java Graphical User Interface Frameworks - textFlutter vs Java Graphical User Interface Frameworks - text
Flutter vs Java Graphical User Interface Frameworks - textToma Velev
 
PuppetConf 2017: Puppet Enterprise Roadmap 2017- Ryan Coleman, Puppet
PuppetConf 2017: Puppet Enterprise Roadmap 2017- Ryan Coleman, PuppetPuppetConf 2017: Puppet Enterprise Roadmap 2017- Ryan Coleman, Puppet
PuppetConf 2017: Puppet Enterprise Roadmap 2017- Ryan Coleman, PuppetPuppet
 
Using Groovy to empower WebRTC Network Systems
Using Groovy to empower WebRTC Network SystemsUsing Groovy to empower WebRTC Network Systems
Using Groovy to empower WebRTC Network Systemsantonry
 
Grunt training deck
Grunt training deckGrunt training deck
Grunt training deckJames Ford
 

Semelhante a Puppeteer - Headless Chrome Node API (20)

How to Develop Progressive Web Apps in Flutter – Step by Step Guide.pptx
How to Develop Progressive Web Apps in Flutter – Step by Step Guide.pptxHow to Develop Progressive Web Apps in Flutter – Step by Step Guide.pptx
How to Develop Progressive Web Apps in Flutter – Step by Step Guide.pptx
 
vodQA Pune (2019) - Browser automation using dev tools
vodQA Pune (2019) - Browser automation using dev toolsvodQA Pune (2019) - Browser automation using dev tools
vodQA Pune (2019) - Browser automation using dev tools
 
Desktop apps with node webkit
Desktop apps with node webkitDesktop apps with node webkit
Desktop apps with node webkit
 
Technical Tips: Visual Regression Testing and Environment Comparison with Bac...
Technical Tips: Visual Regression Testing and Environment Comparison with Bac...Technical Tips: Visual Regression Testing and Environment Comparison with Bac...
Technical Tips: Visual Regression Testing and Environment Comparison with Bac...
 
Headless browser: puppeteer and git client : GitKraken
Headless browser: puppeteer and git client : GitKrakenHeadless browser: puppeteer and git client : GitKraken
Headless browser: puppeteer and git client : GitKraken
 
DIY: Computer Vision with GWT.
DIY: Computer Vision with GWT.DIY: Computer Vision with GWT.
DIY: Computer Vision with GWT.
 
DIY- computer vision with GWT
DIY- computer vision with GWTDIY- computer vision with GWT
DIY- computer vision with GWT
 
Polymer Web Framework - Swecha Boot Camp
Polymer Web Framework - Swecha Boot CampPolymer Web Framework - Swecha Boot Camp
Polymer Web Framework - Swecha Boot Camp
 
Lean Drupal Repositories with Composer and Drush
Lean Drupal Repositories with Composer and DrushLean Drupal Repositories with Composer and Drush
Lean Drupal Repositories with Composer and Drush
 
Infrastructure as code with Puppet and Apache CloudStack
Infrastructure as code with Puppet and Apache CloudStackInfrastructure as code with Puppet and Apache CloudStack
Infrastructure as code with Puppet and Apache CloudStack
 
ApacheCloudStack
ApacheCloudStackApacheCloudStack
ApacheCloudStack
 
Phonegap android angualr material design
Phonegap android angualr material designPhonegap android angualr material design
Phonegap android angualr material design
 
The Superhero’s Method of Modern HTML5 Development by RapidValue Solutions
The Superhero’s Method of Modern HTML5 Development by RapidValue SolutionsThe Superhero’s Method of Modern HTML5 Development by RapidValue Solutions
The Superhero’s Method of Modern HTML5 Development by RapidValue Solutions
 
Export pdf with puppeteer
Export pdf with puppeteerExport pdf with puppeteer
Export pdf with puppeteer
 
Panther loves Symfony apps
Panther loves Symfony appsPanther loves Symfony apps
Panther loves Symfony apps
 
Red Hat Forum Benelux 2015
Red Hat Forum Benelux 2015Red Hat Forum Benelux 2015
Red Hat Forum Benelux 2015
 
Flutter vs Java Graphical User Interface Frameworks - text
Flutter vs Java Graphical User Interface Frameworks - textFlutter vs Java Graphical User Interface Frameworks - text
Flutter vs Java Graphical User Interface Frameworks - text
 
PuppetConf 2017: Puppet Enterprise Roadmap 2017- Ryan Coleman, Puppet
PuppetConf 2017: Puppet Enterprise Roadmap 2017- Ryan Coleman, PuppetPuppetConf 2017: Puppet Enterprise Roadmap 2017- Ryan Coleman, Puppet
PuppetConf 2017: Puppet Enterprise Roadmap 2017- Ryan Coleman, Puppet
 
Using Groovy to empower WebRTC Network Systems
Using Groovy to empower WebRTC Network SystemsUsing Groovy to empower WebRTC Network Systems
Using Groovy to empower WebRTC Network Systems
 
Grunt training deck
Grunt training deckGrunt training deck
Grunt training deck
 

Último

Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...MyIntelliSource, Inc.
 
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfLearn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfkalichargn70th171
 
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...kellynguyen01
 
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...harshavardhanraghave
 
Hand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptxHand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptxbodapatigopi8531
 
HR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comHR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comFatema Valibhai
 
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...OnePlan Solutions
 
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Steffen Staab
 
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️Delhi Call girls
 
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...ICS
 
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online ☂️
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online  ☂️CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online  ☂️
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online ☂️anilsa9823
 
Optimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVOptimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVshikhaohhpro
 
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected WorkerHow To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected WorkerThousandEyes
 
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...Health
 
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsUnveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsAlberto González Trastoy
 
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...MyIntelliSource, Inc.
 
Software Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsSoftware Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsArshad QA
 
Diamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with PrecisionDiamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with PrecisionSolGuruz
 

Último (20)

Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
 
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfLearn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
 
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
 
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
 
Hand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptxHand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptx
 
HR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comHR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.com
 
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
 
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
 
Microsoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdfMicrosoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdf
 
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
 
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
 
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online ☂️
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online  ☂️CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online  ☂️
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online ☂️
 
Optimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVOptimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTV
 
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected WorkerHow To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
 
Vip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS Live
Vip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS LiveVip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS Live
Vip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS Live
 
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
 
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsUnveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
 
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
 
Software Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsSoftware Quality Assurance Interview Questions
Software Quality Assurance Interview Questions
 
Diamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with PrecisionDiamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with Precision
 

Puppeteer - Headless Chrome Node API

  • 1. Puppeteer Headless Chrome Node API Volkan Özdamar - QA Analyst @SONY
  • 2. Schedule ● What is NodeJS ? ● What is Puppeteer ? ● FAQ ● Installation ● Dive into ...
  • 3. NodeJS or node is a runtime environment for executing JavaScript Codes.
  • 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
  • 14. Announced Chrome Dev Summit 2017 San Francisco, October 23-24, 2017
  • 16. Microsoft is rebuilding its Edge browser on Chromium Not Official but it is support Firefox too...
  • 19. Simple Example const puppeteer = require('puppeteer'); (async () => { const browser = await puppeteer.launch(); const page = await browser.newPage(); await page.goto('https://volkanozdamar.com'); await page.screenshot({path: 'page.png'}); await browser.close(); })(); Launch without built-in Chromium await puppeteer.launch({executablePath: '/path/to/Chrome'});
  • 20. Simple Example const puppeteer = require('puppeteer'); (async () => { const browser = await puppeteer.launch(); const page = await browser.newPage(); await page.goto('https://volkanozdamar.com'); await page.screenshot({path: 'page.png'}); await browser.close(); })(); Disable Headless Mode await puppeteer.launch({headless: false});
  • 21. Generating PDF const puppeteer = require('puppeteer'); (async () => { const browser = await puppeteer.launch(); const page = await browser.newPage(); await page.goto('https://volkanozdamar.com'); await page.pdf({path: 'printout.pdf',format: 'A4'}); await browser.close(); })(); Generating a pdf is currently only supported in Chrome headless.
  • 22. Setting Viewport const puppeteer = require('puppeteer'); (async () => { const browser = await puppeteer.launch(); const page = await browser.newPage(); await page.setViewport({ width: 1920, height: 1080 }); await page.goto('https://volkanozdamar.com'); await page.screenshot({path: 'page.png'}); await browser.close(); })(); Puppeteer sets an initial page size to 800px x 600px, which defines the screenshot size.
  • 23. Emulate Mobile Devices const puppeteer = require('puppeteer'); const devices = require('puppeteer/DeviceDescriptors'); const pixeltwo = devices['Pixel 2']; puppeteer.launch().then(async browser => { const page = await browser.newPage(); await page.emulate(pixeltwo); await page.goto('https://www.sonymobile.com/gb'); await page.screenshot({path: 'mainpage.png',fullPage:false}); await browser.close(); }); Puppeteer has predefined device sizes inside.You can find those devices in“DeviceDescriptors.js”. Also you can create your own devices.
  • 25. Interacting with Page Elements const puppeteer = require('puppeteer'); (async () => { const browser = await puppeteer.launch(); const page = await browser.newPage(); await page.goto('https://google.com'); await page.type('#lst-ib','Puppeteer'); await page.click('input[name="btnK"]'); await browser.close(); })();
  • 26. Web Scraping const puppeteer = require('puppeteer'); (async () => { const browser = await puppeteer.launch({headless: false}); const page = await browser.newPage(); await page.goto('https://sonymobile.com/gb'); await page.click('#menu-primary-menu > li:nth-child(2) > a'); await page.waitForSelector('.btn-alt-special'); await page.$('.btn-alt-special'); await page.click('.btn-alt-special'); await page.click('#phones-display_size-lt_5'); const titles = await page.evaluate( () => Array.from(page.$$('div.product-name-wrap a strong')) .map(partner=>partner.innerText.trim() ) ); console.log(titles); await browser.close(); })(); document.querySelectorAll() == page.$$
  • 27. Snip an element const puppeteer = require('puppeteer'); (async () => { const browser = await puppeteer.launch() const page = await browser.newPage() await page.goto('https://twitter.com/volkanozdamar') const timeline = await page.$('.ProfileHeaderCard'); await timeline.screenshot({ path:"profileHeader.png" }); browser.close(); })();
  • 28. Add some Coffeé const puppeteer = require('puppeteer'); describe( 'Test Website', async () => { it( 'Browser Opens Successfully', async () => { browser = await puppeteer.launch(); }); it( 'Page Open Successfully', async () => { page = await browser.newPage(); }); it( 'Website Should Load', async () => { const response = await page.goto('https://sonymobile.com/gb', { waitUntil:'domcontentloaded'}); }).timeout(0); it( 'Browser Closes Successfully', async () => { await browser.close(); }); });
  • 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
  • 31. Disable images await page.setRequestInterception(true); page.on('request', (req) => { if(req.resourceType() === 'image'){ req.abort(); } else { req.continue(); } });
  • 32. Disable CSS and Font await page.setRequestInterception(true); page.on('request', (req) => { if(req.resourceType() == 'stylesheet' || req.resourceType() == 'font' || req.resourceType() == 'image'){ req.abort(); } else { req.continue(); } });
  • 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
  • 34. Accessibility Tree const puppeteer = require('puppeteer'); const fs = require('fs'); (async () => { const browser = await puppeteer.launch(); const page = await browser.newPage(); await page.goto('https://www.sonymobile.com/gb'); const snapshot = await page.accessibility.snapshot(); fs.writeFile("snapshot.txt",JSON.stringify(snapshot), function(err) { if(err) { return console.log(err); } console.log("The file was saved!"); }); })(); See Also : Accessibility Tree
  • 35. Accessibility with pa11y const puppeteer = require('puppeteer'); const pa11y = require('pa11y'); async function runPa11y() { try { const browser = await puppeteer.launch({ ignoreHTTPSErrors: true }); const results =await pa11y('https://sonymobile.com/gb', { browser: browser }); browser.close(); // Do something with the results console.log(results); } catch (error) { // Handle the error } } runPa11y();

Notas do Editor

  1. //chrome://accessibility/