Skip to main content
Introducing Puppeteer from Installation to Demonstration
Last Updated Jun 10, 2020 — Continuous Testing Expert

Introducing Puppeteer from Installation to Demonstration

Continuous Testing

In software testing, automation tools certainly play an important role. They help us ease the testing process. One such tool is the Puppeteer. Puppeteer is a NodeJS library that provides a high-level API to control Chrome or Chromium over the special DevTools Protocol. The Puppeteer runs headless (without a UI interface) by default, but can be configured to run full (non-headless) Chrome, Chromium. Let's take a look at how to install and start using Puppeteer, as well as demonstrate several ways to use it. For the examples, shown in this article, the workstation running under Ubuntu Linux was used. Install Puppeteer on Windows or Mac OS whichever you prefer.


Before we begin, we need to setup NodeJS. You can download and install it, using this command:

[js]~ sudo apt-get install nodejs[/js]  

Next, you need to install the npm packages manager.

[js]~ sudo apt-get install npm[/js]  

And then using npm we can install the "Puppeteer" library.

[js]~npm i Puppeteer[/js]  

Also you must install these dependencies for correct working, enlisted in the command below:

[js]~sudo apt-get install gconf-service libasound2 libatk1.0-0 libatk-bridge2.0-0 libc6 libcairo2 libcups2 libdbus-1-3 libexpat1 libfontconfig1 libgcc1 libgconf-2-4 libgdk-pixbuf2.0-0 libglib2.0-0 libgtk-3-0 libnspr4 libpango-1.0-0 libpangocairo-1.0-0 libstdc++6 libx11-6 libx11-xcb1 libxcb1 libxcomposite1 libxcursor1 libxdamage1 libxext6 libxfixes3 libxi6 libxrandr2 libxrender1 libxss1 libxtst6 ca-certificates fonts-liberation libappindicator1 libnss3 lsb-release xdg-utils wget[/js]  

For the examples used in this tutorial, we also need to install the Puppeteer recorder. Puppeteer recorder is a special extension for Google Chrome. With this extension, you can record your activities on any site in a NodeJS code. You can download this extension from the Chrome store. Later in the article, we are going to use this extension for demonstration purposes. The screenshots below show the installation of the Puppeteer recorder.

puppeteer recorder

Everything is ready, and you can now use Puppeteer.

Creation of the script using the Chrome Puppeteer extension.

After installing all the necessary libraries and components, we are going to record a simple script that allows you to open a browser and navigate to the Experitest website. Also, we will modify this autotest code and add a method to take a pdf document. Activate the Puppeteer recorder from the Chrome extensions.

[caption id="attachment_22544" align="alignnone" width="445"] puppeteer recorder 2[/caption]

Click on the "Record" button and open the site "" Scroll down to the bottom of the page and click on the "News" button in the page footer.


Click on the stop button in the Puppeteer extension.

puppeteer stop

After that, you can see the NodeJS script.

Then create the file Example.js and copy the NodeJS code into this file, by clicking on the copy to clipboard link of the recorder form and pasting it to the file.

Script analysis and modification

Let's examine this code line by line. Here we set up the Puppeteer library like a dependence.

[js]const Puppeteer = require(‘Puppeteer');[/js]  

The main function starts from this line. This function automates actions in the browser.

[js](async () => {[/js]  

Then we launch the browser and write a reference to it to the constant.

[js]const browser = await Puppeteer.launch();[/js]  

If we want to launch chromium in the non-headless mode (with UI) we need to set the option "headless: true". But this is where the problem may arise, because Puppeteer does not allow to generate pdf files in the non-headless mode. Therefore, we leave this method unchanged. With the following line of code we create a new page in the browser.

  [js]const page = await browser.newPage();[/js]  

Then we assign a method "page.waitForNavigation" to the constant "navigationPromise".

[js]const navigationPromise = page.waitForNavigation();[/js]  

Then we navigate to

[js]await page.goto(‘')[/js]  

This method makes Chromium set the viewport by inferring the current screen resolution.

[js]await page.setViewport({width: 1920, height: 563 });[/js]  

Then the program waits for the element which we need to click to appear.

[js]await page.waitForSelector('.row > .col-xl-2:nth-child(7) > .footer-menu-column > li:nth-child(5) > a');[/js]  

Here the click on the element is performed.

[js]await'.row > .col-xl-2:nth-child(7) > .footer-menu-column > li:nth-child(5) > a');[/js]  

And the browser is closed.

[js]await browser.close();[/js]  

Let's modify the code of the test by adding the page.pdf method to create a pdf document.

[js]await page.pdf({path:'example.pdf', format:'A4', printBackground:true });[/js]  

The "path" option specifies the name of the pdf file and its location. Also, for the correct display of the document add the option " waitUntil: ‘networkidle0'. This option forces navigation to finish if there have not been network connections for at least 500ms.

[js]const navigationPromise = page.waitForNavigation({ waitUntil: 'networkidle0' });[/js]  

After all modifications our code takes the following form:

So our script is ready. Go to the folder with the script and launch it with the following command.

[js]node example.js[/js]  

After executing the code, the file example.pdf will appear next to the file. Open it, you will see the pdf document of this site's contents.

puppeteer example

Creation of the script for scraping information from a site

Suppose we need to parse the first news headline on the site "". Let's create the file examplescrap.js and write code into it line by line. The beginning of this script is the same as the previous one. We set up the "Puppeteer" library like a dependence, launch the browser, and navigate to the site with news.

[js] const Puppeteer = require('Puppeteer'); (async () => { const url = ''; const browser = await Puppeteer.launch(); const page = await browser.newPage(); [/js]  

Then we assign the method page.evaluate() to the constant "data".

[js]const data = await page.evaluate(() => {[/js]

After that we add the document method querySelector(locator).innerText to the constant title. Text found by a specific locator is written into the constant title. Then this constant is returned in the method page.evaluate():

  [js]const title = document.querySelector('#content2 > div > ul > > h3').innerText; return { title } }); [/js]    

The returned constant date is displayed in the log and then the browser closes.

  [js]console.log(data); await browser.close(); })(); [/js]    

So everything is ready, now we can run this script.

puppeteer example

Great, this script provided us with the name of the first news article on the site


In this article, we have familiarized with the Puppeteer node js library. Namely, we discussed the installation of nodes and this library, the structure of the code. We acquainted with the methods of PDF document creation and web scraping. We also looked at the extension for Google Сhrome "Puppeteer", which allows you to record all your actions in the form of a Javascript code.

Guy Arieli - CTO

More from the Blog

View more
May 13, 2022

What tips would you give testers on how to ask better questions?

Continuous Testing
The nature of testing is not about confirmation. It is about questioni ...
Read More
Mar 23, 2022 Continuous Testing, First in Market to Support Android 13 DP

Continuous Testing is proud to announce that our Continuous Testing solution i ...
Read More
Mar 21, 2022

Eliminate inefficiencies in your enterprise with codeless and continuous automated testing

Continuous Testing
Today, terms like codeless, continuous automated testing and shift lef ...
Read More
Feb 07, 2022

These key factors will help you choose an automation tool

Continuous Testing
The year 2022 looks to be another banner year of growth in the softwar ...
Read More
Contact Us