BreadcrumbHomeResourcesBlog Selenium Vs. Puppeteer: Which Is Better? December 8, 2021 Selenium vs. Puppeteer: Which Is Better?Open Source AutomationBy Shanika WickramasingheWhen 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.Table of Contents What's the Difference Between Selenium vs. Puppeteer?Selenium vs. Puppeteer: InstallationThe Architecture of Selenium vs. Puppeteer Selenium vs. Puppeteer: Feature ComparisonBenefits of Selenium vs PuppeteerStart Automated Web Testing With Selenium vs. PuppeteerWhen to Choose Selenium vs. Puppeteer?Bottom LineTable of Contents1 - What's the Difference Between Selenium vs. Puppeteer?2 - Selenium vs. Puppeteer: Installation3 - The Architecture of Selenium vs. Puppeteer 4 - Selenium vs. Puppeteer: Feature Comparison5 - Benefits of Selenium vs Puppeteer6 - Start Automated Web Testing With Selenium vs. Puppeteer7 - When to Choose Selenium vs. Puppeteer?8 - Bottom Line 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.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 inputsScraping websitesCrawling single-page applicationsRunning test on the latest Chromium versionsGaining direct access to browser tools, like performance analytics and debuggingCreating PDFs and screenshots of pages Testing Chrome extensions (learn how you can also test Chrome extensions in Selenium)Back to topSelenium vs. Puppeteer: InstallationInstalling 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:SeleniumWhen 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 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.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!PuppeteerPuppeteer 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 That’s it!Back to topThe Architecture of Selenium vs. Puppeteer Both tools offer solid automation capabilities, yet with totally different underlying architecture for facilitating their functionality.Selenium ArchitectureSelenium 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 ArchitectureMeanwhile, 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 topSelenium vs. Puppeteer: Feature ComparisonFeatureSeleniumPuppeteerPrograming LanguageJava, Python, C#, JavaScript, Kotlin, RubyJavaScriptSupported BrowsersChrome (Any Chromium-based browsers), Mozilla, Safari, Edge, IE, OperaChromium, Chrome, and Blink-based browsers. (Edge, Vivaldi, Brave) Experimental support for Firefox.Supported PlatformsWindows, Linux, macOSWindows, Linux, macOSCommunity SupportExtensive and mature communityComparatively limited but growingExecution SpeedRelatively SlowerFastConfiguration and SetupRelatively complex for new usersSimple with a single installation commandTest Platform SupportWeb and Mobile with AppiumWeb onlyNo-Code InterfaceYes through Selenium IDENoDistributed TestingSelenium GRIDNoScreenshot SupportImages (Selenium 3)/ Images and PDF (Selenium 4)Images and PDFIntegrationsExtensive integration options and support for third-party tools and platformsLimited integration options Back to topBenefits of Selenium vs PuppeteerIn this section, let's look at the unique benefits offered by each automation framework when dealing with automated testing.Selenium BenefitsExtensive 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...PuppeteerNative 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 topStart Automated Web Testing With Selenium vs. PuppeteerIn 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.SeleniumWe 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: What it looks like on CLI:ScreenshotPuppeteerHere, 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 Scriptconst 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:What the command line looks like: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 topWhen 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 topBottom LineIn 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 ResourcesDriving Headless Browser Testing with Selenium and PythonBack to top
Shanika Wickramasinghe Software Engineer Shanika Wickramasinghe is a software engineer by profession and a graduate in Information Technology. She specializes in both Software development and QA automation. Shanika considers writing the best medium to learn and share her knowledge. She is passionate about everything she does, loves to travel, and enjoys nature whenever she takes a break from her busy work schedule. You can connect with her on LinkedIn.