Selenium vs. Puppeteer: Which Is Better?
December 8, 2021

Selenium vs. Puppeteer: Which Is Better?

Open Source Automation

When comparing Selenium vs. Puppeteer, the first thing you will notice is that they are two popular open-source test automation frameworks used for automating browser testing (and more). 

Selenium is the more mature framework, created in 2004 and quickly positioning itself as the standard tool for automation testing, thanks to its broad capabilities and ability to support multiple languages. Puppeteer, on the other hand, was just released in 2018 by Google, but it quickly gained popularity among developers.

Nowadays, developers can automate almost any action they perform on browsers by using Puppeteer. In this article, we will compare the two frameworks in detail, so you can understand which framework suits your needs the best.

 

Back to top

 What's the Difference Between Selenium vs. Puppeteer?

The main difference between Selenium and Puppeteer is that Selenium is a longstanding popular test automation platform, while Puppeteer is a newcomer who is gaining popularity. While both can be used to automate tests for the Chrome browser, Selenium is more widely known in the test automation industry than Puppeteer. 

Having started its life in 2004, Selenium is probably the most widely used automation platform in the industry, from small independent developments to enterprise applications. Puppeteer, which was released in 2018, is relatively new to the industry yet has steadily grown in popularity over the years.

If we look at the Google Trends comparing these two platforms, we can clearly see that Selenium is still the test automation leader.

Selenium vs. Puppeteer Google Trends

Why Use Selenium?

Selenium is a time-tested tool, and the Selenium WebDriver has become a W3C standard when it comes to facilitating automation. This is because Selenium supports multiple programming languages and all major browsers. In addition, it has a massive community and a large extension library to integrate with third-party tools and platforms.  As a result, developers can use Selenium to create extensive test cases in their favorite languages and target a wide range of browsers from this single platform.

Selenium also provides advanced features like distributed testing and the ability to directly integrate with a framework like Ant or Maven. Developers can then build test workflows that complement and extend existing workflows while speeding up the development process. Additionally, Selenium offers inbuilt support for distributed testing environments with features like Grid, and it can be further extended to support mobile test automation using Appium.

Why Use Puppeteer?

With its high-level API control over Chrome and Chromium, Puppeteer offers great control over these browsers and is comparatively faster than other testing tools, even Selenium.  As an automation tool, developers can use Puppeteer for tasks like:

  • Automating most manual testing activities, like form submission, UI testing, and keyboard mouse inputs
  • Scraping websites
  • Crawling single-page applications
  • Running test on the latest Chromium versions
  • Gaining direct access to browser tools, like performance analytics and debugging
  • Creating PDFs and screenshots of pages 
  • Testing Chrome extensions (learn how you can also test Chrome extensions in Selenium)
Back to top

Selenium vs. Puppeteer: Installation

Installing Selenium and Puppeteer is fairly easy, with the primary difference being the required libraries. As Selenium offers support for multiple languages, users need to follow language-specific instructions, while Puppeteer users only need to install it via npm.

Let’s look at each one:

Selenium

When installing Selenium, first install the language-specific library you’d like and then download the necessary browser driver for the required browser. If there are multiple browsers, download a driver for each browser. In this example, we will see how to install and configure Selenium for Python.

1. Install Selenium Libraries (Python 3.8)

# Using Pipenv
pipenv install selenium

# Using PIP
pip install selenium

 

 

installing Selenium libraries

Note: Pipenv is used to manage the Python virtual environment. The normal pip command can also be used to install Selenium.

2. Visit the Selenium website to download the Selenium browser drivers. You can download the official web drivers from there.

download Selenium browser drivers

After downloading, you can either add the driver to the system path or directly point to the driver from the automation script. This can be done by providing the “executable_path” option when defining the browsers.

That’s it!

Puppeteer

Puppeteer offers two installation options: puppeteer and puppeteer-core. 

  • The puppeteer library includes the recent version of Chromium and users can start using Puppeteer by simply downloading the Puppeteer library.
  • puppeteer-core is the lightweight version of Puppeteer that does not come with a bundled browser. The puppeteer-core library should be used when connecting to an existing browser or a remote browser.

puppeteer or puppeteer-core can be installed using the npm or yarn package managers.

npm i puppeteer

npm i puppeteer-core

 

 
npm package manager to install Puppeteer

That’s it!

Back to top

The Architecture of Selenium vs. Puppeteer 

Both tools offer solid automation capabilities, yet with totally different underlying architecture for facilitating their functionality.

Selenium Architecture

Selenium uses its WebDriver to provide a set of APIs that will facilitate communication with the web browser. Previously, Selenium 3 and earlier versions used the JSON Wire protocol to provide interactions between the client libraries and browser drivers. However, starting with Selenium 4, the JSON Wire protocol is replaced by the WebDriver W3C protocol. This eliminates the need for a middleman to encode and decode API requests and facilitates direct communication with web browsers using the standardized W3C Protocol.

Puppeteer Architecture

Meanwhile, Puppeteer uses the DevTools protocol, which is native to chromium, chrome, and blink-based browsers. Thus, it is able to communicate with the supported browsers directly.

Back to top

Selenium vs. Puppeteer: Feature Comparison

Feature

Selenium

Puppeteer

Programing Language

Java, Python, C#, JavaScript, Kotlin, Ruby

JavaScript

Supported Browsers

Chrome (Any Chromium-based browsers), Mozilla, Safari, Edge, IE, Opera

Chromium, Chrome, and Blink-based browsers. (Edge, Vivaldi, Brave) Experimental support for Firefox.

Supported Platforms

Windows, Linux, macOS

Windows, Linux, macOS

Community Support

Extensive and mature community

Comparatively limited but growing

Execution Speed

Relatively Slower

Fast

Configuration and Setup

Relatively complex for new users

Simple with a single installation command

Test Platform Support

Web and Mobile with Appium

Web only

No-Code Interface

Yes through Selenium IDE

No

Distributed Testing

Selenium GRID

No

Screenshot Support

Images (Selenium 3)/ Images and PDF (Selenium 4)

Images and PDF

Integrations

Extensive integration options and support for third-party tools and platforms

Limited integration options

 

Back to top

Benefits of Selenium vs Puppeteer

In this section, let's look at the unique benefits offered by each automation framework when dealing with automated testing.

Selenium Benefits

  • Extensive language, platform, and browser support.
  • Comprehensive testing framework with inbuilt tools for automation, recording, and distributed testing with WebDriver, IDE, and Grid, respectively.
  • Direct integrations with most CI/CD and other test platforms like Cucumber to extend the functionality.
  • Appium to extend test automation to mobile applications with cross-device support.
  • Less resource usage compared to other testing tools such as QTP, SilkTest, etc...

Puppeteer

  • Native access to chrome/chromium-based browsers with DevTools protocol, which leads to higher control of browsers and access to components such as the Chrome Performance Analysis tool.
  • Fewer dependency requirements as there is no separate management of browser drivers.
  • Faster performance compared to other tools like Selenium.
Back to top

Start Automated Web Testing With Selenium vs. Puppeteer

In the previous sections, we gained an understanding of these two test automation frameworks, their strengths, and how to install them in a local environment. Now let’s move on to see how to get started with test automation using these tools.

For each framework, we will be creating a simple test scenario to carry out a Google Search and capture a screenshot of the results page. All the following examples are based on a Windows environment with Python for Selenium and JavaScript for Puppeteer and with Chrome as the targeted browser.

Selenium

We will be using the Selenium 3 Python library with the Chrome Driver to create the test script. The script will call the Chrome browser, navigate itself to Google and carry out a search using the term “Selenium.” Then it will save the results page as a screenshot in png format.

Python Script:

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time
from datetime import datetime
 
# Create the driver
# Point to the downloaded chrome driver using executable_path
driver = webdriver.Chrome(executable_path=r"G:\\chromedriver.exe")
 
try:
    # Navigate to Google
    start_time = datetime.now()
    driver.get("https://www.google.com/")
 
    # Locate the Search Box
    search_box = driver.find_element_by_name("q")
 
    # Enter search term
    search_box.send_keys("Selenium")
 
    # Send enter command to execute the search
    search_box.send_keys(Keys.ENTER)
 
    # Wait to fully render the results
    time.sleep(2)
    # Take a screenshot as PNG
    driver.save_screenshot("google_search_selenium.png")
    end_time = datetime.now()
    execution_time = (end_time - start_time)
    # Print Script Execution Time
    print(f"Execution Time: {execution_time.total_seconds()}")
 
except Exception as e:
    print(f"Error Occurred: {e}")
finally:
    driver.close()

And here are the results: 

Selenium web automation test result

What it looks like on CLI:

CLI

Screenshot

Puppeteer

Here, we will be using the Puppeteer package with the inbuilt browser to carry out testing. Same as above, we will navigate to Google, perform a search for “Puppeteer,” and take a screenshot of the results page in png format.

JavaScript Script

const puppeteer = require('puppeteer');
 
try {
(async () => {
    // Create the Browser
    const browser = await puppeteer.launch({ headless: false })
    const page = await browser.newPage()
 
    // Set ViewPort
    await page.setViewport({ width: 960, height: 1080 });
 
    var start_time = new Date();
    // Navigate to Google
    await page.goto('https://www.google.com/')
 
    // Locate the Search Box and enter search term
    await page.type('[name="q"]', 'Puppeteer')
 
    // Send enter command to execute the search 
    await page.keyboard.press('Enter');
 
    // Wait to results to load
    await page.waitForNavigation()
    
    // Take the screenshot in png format
 
    await page.screenshot({path: 'google-search-puppeteer.png'});
    
    var end_time = new Date();
    var seconds = (end_time.getTime() - start_time.getTime()) / 1000;
    // Print Script Execution Time
    console.log("Execution Time: " + seconds)
    await browser.close()
})()
} catch (error) {
    console.log(error);
}

 

What the results look like:

Puppeteer browser test automation result

What the command line looks like:

CLI

As you can see both tools were able to successfully run the same test case. However, as we have targeted chrome, we can see that Puppeteer has the upper hand when it comes to execution time.

Back to top

When to Choose Selenium vs. Puppeteer?

Both Selenium and Puppeteer are very powerful tools, with useful capabilities for test automation. How can developers and testers know which one to choose?

If you are using the Google Chrome browser, Puppeteer is the preferred choice as it provides unparalleled access and performance for chrome-based browser testing with native integrations. In addition, Puppeteer is more of an automation tool than a testing tool, making it suitable for automation tasks like scraping and crawling. Furthermore, By using the DevTools Protocol, Puppeteer can access native tools such as the debugger, networking control, and performance analytics.

On the other hand, Selenium provides a greater range of browser coverage and a larger feature set compared to Puppeteer. As the WebDriver protocol offers cross-browser support, and with the standardization of this protocol as a W3C standard, you can interact with any browser directly. Thus Selenium helps to greatly expand the test scope without needing to rely on different tools for different platforms. 

Additionally, the Selenium IDE and Grid provide all the tools necessary to record test cases and scale test environments with simultaneous tests. Coupled with the freedom to use multiple programming languages, Selenium can be integrated into a wider range of development environments. All this leads to Selenium being an excellent choice to meet any automation need. Therefore, it's better to go for Selenium if you require a mature, feature-rich test framework with an extensive feature set and wide-ranging support. 

Back to top

Bottom Line

In this article, we covered two of the most popular test automation frameworks, each offering distinct benefits for various automation needs.

Selenium has an overall advantage over Puppeteer in terms of supported platforms, browsers, and available features. However, if you are targeting the chromium/chrome ecosystem, Puppeteer offers a better solution with its tighter integration and faster execution times. In the end, it all comes down to your testing and automation requirements.

So, take this article as the baseline to evaluate your requirements and choose the correct framework for your future test needs.

START TESTING NOW

 

Related Resources

Back to top