<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: rajnavadiya786</title>
    <description>The latest articles on DEV Community by rajnavadiya786 (@rajnavadiya786).</description>
    <link>https://dev.to/rajnavadiya786</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F464856%2F796616e9-0a9a-4124-8107-6d3b765fe098.png</url>
      <title>DEV Community: rajnavadiya786</title>
      <link>https://dev.to/rajnavadiya786</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/rajnavadiya786"/>
    <language>en</language>
    <item>
      <title>How to capture website screenshots using Node.js and Puppeteer</title>
      <dc:creator>rajnavadiya786</dc:creator>
      <pubDate>Mon, 07 Sep 2020 06:09:36 +0000</pubDate>
      <link>https://dev.to/rajnavadiya786/how-to-capture-website-screenshots-using-node-js-and-puppeteer-35i9</link>
      <guid>https://dev.to/rajnavadiya786/how-to-capture-website-screenshots-using-node-js-and-puppeteer-35i9</guid>
      <description>&lt;p&gt;Puppeteer is a Node.js library that provides an API for controlling Chrome and Chromium browsers. In this article, we’ll be using it to capture a screenshot of a website, but it can also be used to automate form submissions, perform UI testing, and help diagnose performance issues.&lt;/p&gt;

&lt;p&gt;To get started with Puppeteer you’ll first need to have Node.js (v10.18.1+) installed.&lt;/p&gt;

&lt;p&gt;With Node.js installed run the following terminal commands to create a project folder and install Puppeteer:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;mkdir screenshot
cd screenshot
npm install puppeteer
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;This can take a little while to install as Puppeteer downloads Chromium which is around 120mb in size. Once installed we’ll setup the script to capture the screenshot in a new file called &lt;code&gt;screenshot.js&lt;/code&gt; using Wikipedia as an example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const puppeteer = require("puppeteer");
const capture = async () =&amp;gt; {
  const browser = await puppeteer.launch();
  const page = await browser.newPage();
  await page.goto("https://www.wikipedia.org/");
  await page.screenshot({ path: "./screenshot.png" });
  await browser.close();
};
capture();
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;As we’re using the &lt;code&gt;await&lt;/code&gt; operator to wait for a Promise the code must be run inside an &lt;code&gt;async&lt;/code&gt; function. This allows asynchronous, promise-based behavior to be written in a cleaner style, avoiding having to explicitly configure promise chains.&lt;/p&gt;

&lt;p&gt;It’s now time to test the script out by running the following command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;node screenshot.js
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;f successful this will save an image named &lt;code&gt;screenshot.png&lt;/code&gt; in the same folder as the script is located. By default, the screenshot will be 800×600 pixels but you can set a specific viewport size by adding the following with the required dimensions:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;await page.setViewport({ width: 1024, height: 768 });
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Alternatively entire pages can be captured by modifying page.screenshot as follows:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;await page.screenshot({ path: 'screenshot.png', fullPage: true };
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Up until now, the screenshot is being saved as a .png but it’s also possible to save as .pdf by replacing the &lt;code&gt;page.screenshot&lt;/code&gt; line with the following:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;await page.pdf({ path: 'screenshot.pdf', format: 'A4' });
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



</description>
    </item>
  </channel>
</rss>
