{"id":163,"date":"2017-12-07T20:01:37","date_gmt":"2017-12-07T20:01:37","guid":{"rendered":"http:\/\/loadfocus.com\/blog\/tech\/?p=163"},"modified":"2019-04-26T08:49:38","modified_gmt":"2019-04-26T08:49:38","slug":"how-to-take-screenshots-with-puppeteer-using-headless-chrome-browser","status":"publish","type":"post","link":"https:\/\/loadfocus.com\/blog\/tech\/2017\/12\/how-to-take-screenshots-with-puppeteer-using-headless-chrome-browser","title":{"rendered":"How to Take Screenshots with Puppeteer using Headless Chrome Browser"},"content":{"rendered":"<span class=\"span-reading-time rt-reading-time\" style=\"display: block;\"><span class=\"rt-label rt-prefix\"><\/span> <span class=\"rt-time\"> 3<\/span> <span class=\"rt-label rt-postfix\">minutes read<\/span><\/span><p class=\"lead\">What is Puppeteer? <a href=\"https:\/\/github.com\/GoogleChrome\/puppeteer\">Puppeteer<\/a> is Node library that you can use in order to control Headless Chrome with the DevTools Protocol.<\/p>\n<p>The <a href=\"https:\/\/chromedevtools.github.io\/devtools-protocol\/\">Chrome DevTools Protocol<\/a> allows for tools to instrument, inspect, debug and profile for&nbsp;<strong>Chromium <\/strong>and<strong> Chrome <\/strong>browsers.<\/p>\n<p><a href=\"https:\/\/github.com\/GoogleChrome\/puppeteer\">Puppeteer &#8211; Headless Chrome Node API<\/a>&nbsp;works only with <strong>Chrome<\/strong> and uses the latest versions of <strong>Chromium<\/strong>.<\/p>\n<p><a href=\"https:\/\/www.chromium.org\/getting-involved\/download-chromium\">Chromium<\/a> is an open-source browser project that forms the basis for the <a href=\"https:\/\/www.google.com\/chrome\/\">Chrome<\/a> web browser. One of the biggest differences between the two browsers is that, while Chrome is based on Chromium, Google adds some of proprietary features to Chrome, features like automatic updates and support for additional video formats. Other features like&nbsp;usage-tracking or \u201cuser metrics\u201d feature can be found only in Chrome browser.<\/p>\n<p>Note: Puppeteer requires at least <code>Node v6.4.0<\/code><strong>,&nbsp;<\/strong>but the examples below use async\/await which is only supported in <code>Node v7.6.0<\/code>&nbsp;or greater.<\/p>\n<p><strong>Node.js<\/strong> has a simple module loading system. In Node.js, files and modules are in one-to-one correspondence (each file is treated as a separate module).<\/p>\n<p>You can use&nbsp;<a style=\"font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 13px;\" href=\"https:\/\/loadfocus.com\/visual-regression-testing\">Visual Regression Testing<\/a>&nbsp;to take website screenshots and compare the generated images and identify differences pixel by pixel, a&nbsp;comparison image will be shown next to the result\u2019s screenshot that highlights the differences in red.<\/p>\n<p><img loading=\"lazy\" class=\"aligncenter size-medium\" src=\"https:\/\/d2woeiihr4s5r6.cloudfront.net\/screenshot-comparison\/differences-screenshot.png\" alt=\"visual regression testing tool\" width=\"3240\" height=\"1920\"><\/p>\n<p>&nbsp;<\/p>\n<h3>Install Puppeteer<\/h3>\n<p>Here is how to install puppeteer from <strong><a href=\"https:\/\/www.npmjs.com\/\">NPM Modules Registry<\/a> (npm <\/strong>is the package manager for JavaScript<strong>)<\/strong>:<\/p>\n<pre class=\"lang-java\"><code class=\"lang-java\">\nnpm i puppeteer\n<\/code><\/pre>\n<p><strong>Below are code snippets on how to use <a href=\"https:\/\/github.com\/GoogleChrome\/puppeteer\">Puppeteer<\/a> &#8211; Headless Chrome Node API in order to take screenshots of your website.<\/strong><\/p>\n<p>Example &#8211; navigating to https:\/\/example.com and saving a screenshot as a PNG file named <code>example.png<\/code>:<\/p>\n<h3>Generate screenshots with Puppetteer<\/h3>\n<pre class=\"lang-java\"><code class=\"lang-java\">\nconst puppeteer = require('puppeteer'); (async () = { const browser = await puppeteer.launch(); const page = await browser.newPage(); await page.goto('https:\/\/example.com'); await page.screenshot({path: 'example.png'}); await browser.close(); })();\n<\/code><\/pre>\n<pre class=\"lang-java\"><code class=\"lang-java\">\nawait page.goto('https:\/\/example.com'), {\ntimeout: 120000,\nslowMo: 2000,\nwaitUntil: ['load', 'networkidle'],\nnetworkIdleTimeout: 5000,\n});\n<\/code><\/pre>\n<p>By default, Puppeteer take screenshots of the viewport (the area which is visible by default when you open a website).<\/p>\n<p>In order to take a screenshot of the full web page, you need to add the <code>fullPage<\/code> parameter to the screenshot method:<\/p>\n<pre class=\"lang-java\"><code class=\"lang-java\">\nawait page.screenshot({ path: 'example.png', fullPage: true });\n<\/code><\/pre>\n<p>Here is an example on how to take a screenshot of a webpage with Puppeteer using a customer web page size.<br \/>\nYou just need to pass the <code>width<\/code><strong>&nbsp;<\/strong>and<strong>&nbsp;<\/strong><code>height<\/code><strong>&nbsp;<\/strong>of the viewport in order for the browser to resize the web page to the desired size.<\/p>\n<pre class=\"lang-java\"><code class=\"lang-java\">\nexport async function takeScreenshot(page, filename, width, height) {\nawait page.setViewport({ width, height });\n\nawait page.screenshot({ path: `${filename}-${width}-${height}.png`, fullPage: true });\n}\n<\/code><\/pre>\n<p>Here is how to call the above custom screenshot method:<\/p>\n<pre class=\"lang-java\"><code class=\"lang-java\">\nawait takeScreenshot(page, 'example.png', 320, 480);\n<\/code><\/pre>\n<p>Puppeteer, also provides a list of Mobile Devices as a list of objects called DeviceDescriptors.<br \/>\nIn order to emulate a web page in a mobile emulator, with specific characteristics, you can import the<br \/>\npre-defined list of mobile emulators from <code>puppeteer\/DeviceDescriptors<\/code><\/p>\n<pre class=\"lang-java\"><code class=\"lang-java\">\nconst devices from 'puppeteer\/DeviceDescriptors'; \n<\/code><\/pre>\n<p>Puppeteer&#8217;s API is very similar to Selenium WebDriver, but works only with Google Chrome, while WebDriver work with most popular browsers.<\/p>\n<p>More details on <a href=\"https:\/\/loadfocus.com\/blog\/2013\/09\/05\/how-to-locate-web-elements-with-selenium-webdriver\/\">how to locate elements to use in order to interact with Puppeteer or Selenium WebDriver<\/a>.<\/p>\n<h3>Debugging and Troubleshooting Puppeteer<\/h3>\n<p>1. Non Headless Mode &#8211; for debugging purposes, sometimes it&#8217;s useful to see what the browser is displaying. Instead of launching in headless mode, launch a full version of Chrome using <code>headless: false<\/code> when you launch the browser using Puppeteer:<\/p>\n<pre class=\"lang-java\"><code class=\"lang-java\">\nconst browser = await puppeteer.launch({headless: false});\n<\/code><\/pre>\n<p>2. Slow down screenshot generation &#8211; the <code>slowMo<\/code> option slows down Puppeteer operations by the specified amount of milliseconds. It&#8217;s another way to understand better what&#8217;s happening with the code you&#8217;ve written and debug easier.<\/p>\n<pre class=\"lang-java\"><code class=\"lang-java\">\nconst browser = await puppeteer.launch({\nheadless: false,\nslowMo: 250 \/\/ slow down by 250ms\n});\n<\/code><\/pre>\n<p>3. Capture browser&#8217;s console output<\/p>\n<pre class=\"lang-java\"><code class=\"lang-java\">\npage.on('console', msg =&amp;amp;amp;amp;amp;amp;gt; console.log('PAGE LOG:', ...msg.args));\n\nawait page.evaluate(() =&amp;amp;amp;amp;amp;amp;gt; console.log(`url is ${location.href}`));\n<\/code><\/pre>\n<p>4. Enable verbose logging &#8211; All public API calls and internal protocol traffic will be logged via the debug module under the puppeteer namespace.<\/p>\n<p># Basic verbose logging<\/p>\n<pre class=\"lang-java\"><code class=\"lang-java\">\nenv DEBUG=\"puppeteer:*\" node script.js\n<\/code><\/pre>\n<p>You can use&nbsp;<a style=\"font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 13px;\" href=\"https:\/\/loadfocus.com\/visual-regression-testing\">Visual Regression Testing<\/a>&nbsp;to take website screenshots and compare the generated images and identify differences pixel by pixel, a&nbsp;comparison image will be shown next to the result\u2019s screenshot that highlights the differences in red.<\/p>\n<p><a href=\"https:\/\/loadfocus.com\/\">LoadFocus<\/a>&nbsp;is a cloud testing platform for:<\/p>\n<ul>\n<li><a href=\"https:\/\/loadfocus.com\/load-testing\">Load Testing<\/a><\/li>\n<li><a href=\"https:\/\/loadfocus.com\/website-speed-testing\">Website Speed Testing<\/a><\/li>\n<li><a href=\"https:\/\/loadfocus.com\/mobile-emulation\">Mobile Testing<\/a><\/li>\n<li><a href=\"https:\/\/loadfocus.com\/automated-website-testing\">Automated Website Testing<\/a><\/li>\n<li><a href=\"https:\/\/loadfocus.com\/visual-regression-testing\">Visual Regression Testing<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p><span class=\"span-reading-time rt-reading-time\" style=\"display: block;\"><span class=\"rt-label rt-prefix\"><\/span> <span class=\"rt-time\"> 3<\/span> <span class=\"rt-label rt-postfix\">minutes read<\/span><\/span>What is Puppeteer? Puppeteer is Node library that you can use in order to control Headless Chrome with the DevTools Protocol. The Chrome DevTools Protocol allows for tools to instrument, inspect, debug and profile for&nbsp;Chromium and Chrome browsers. Puppeteer &#8211; Headless Chrome Node API&nbsp;works only with Chrome and uses the latest versions of Chromium. Chromium&#8230;  <a href=\"https:\/\/loadfocus.com\/blog\/tech\/2017\/12\/how-to-take-screenshots-with-puppeteer-using-headless-chrome-browser\" class=\"more-link\" title=\"Read How to Take Screenshots with Puppeteer using Headless Chrome Browser\">Read more &raquo;<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[25,32],"tags":[29,30,31],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/loadfocus.com\/blog\/tech\/wp-json\/wp\/v2\/posts\/163"}],"collection":[{"href":"https:\/\/loadfocus.com\/blog\/tech\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/loadfocus.com\/blog\/tech\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/loadfocus.com\/blog\/tech\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/loadfocus.com\/blog\/tech\/wp-json\/wp\/v2\/comments?post=163"}],"version-history":[{"count":23,"href":"https:\/\/loadfocus.com\/blog\/tech\/wp-json\/wp\/v2\/posts\/163\/revisions"}],"predecessor-version":[{"id":330,"href":"https:\/\/loadfocus.com\/blog\/tech\/wp-json\/wp\/v2\/posts\/163\/revisions\/330"}],"wp:attachment":[{"href":"https:\/\/loadfocus.com\/blog\/tech\/wp-json\/wp\/v2\/media?parent=163"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/loadfocus.com\/blog\/tech\/wp-json\/wp\/v2\/categories?post=163"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/loadfocus.com\/blog\/tech\/wp-json\/wp\/v2\/tags?post=163"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}