Playwright vs Selenium: Head to Head Comparison

playwright vs selenium

In this article, we will explore some of the critical aspects of test automation by comparing two prominent tools: Selenium and Playwright. 

Selenium, a widely adopted tool, has a good hold in the test automation community for years. 

On the other hand, Playwright is a newer, emerging tool that promises to bring a futuristic approach to test automation.

Our focus will be on understanding the technical differences between these tools, examining how they operate under the hood. Rather than simply determining which tool is better, we aim to provide insights into their respective architectures, capabilities, and unique features. 

This comparison will help you understand the strengths and potential use cases for each tool, aiding you in making an informed decision based on your specific testing needs.

Architecture: Playwright vs Selenium

We can see how the respective APIs interact with browsers and how communication happens.

PlaywrightSelenium
Websocket- connectionHTTP- connection
Bi-directionalone – direction
PersistentRequest -> response

Playwright:

Playwright Architecture

Selenium:

Selenium Architecture

Instantiation

PlaywrightSelenium
Playwright Core: With playwright core we can directly import browser modules (i.e chromium, firefox etc) and we can create browser contexts, pages and actions.In Selenium, the WebDriver API uses browser-specific drivers (e.g., ChromeDriver for Chrome, GeckoDriver for Firefox). The WebDriver interacts with the browser driver through the HTTP protocol, launching and managing a browser instance. This process requires setting up and configuring the correct driver for the desired browser.
Use browser contexts to provide isolated sessions within the same browser instance.Typically uses separate browser instances to achieve session isolation.
Faster and more resource efficient when creating new sessions.Slower and more resource intensive when launching new sessions.
Simple and streamlined API for managing contexts.More complex setup for managing multiple isolated sessions.
@playwright/test’: Second and easy approach is to use the ‘@playwright/test’ package. This package wraps the playwright core.
It provides a test runner and include ‘tests and assertions’ out of the box.
TestNG’ is a testing framework for Java that provides functionalities similar to those of @playwright/test. This also supports fixtures[annotations], parallelisation, assertions, test configurations. The thing is the setup and integration is little bit complex when compared to Playwright.

Playwright:

First approach:

Second approach:

Selenium:

Synchronization-Waits

Here we can see how the tools ensure that the browser and page elements are in the expected state before performing further actions or assertions

PlaywrightSelenium
Automatic waitsYes, Playwright automatically waits for elements to be ready before performing actionsNo, requires explicit wait statements to handle readiness
Assertions with auto waitingBuilt-in, Playwright assertions automatically wait for the conditions to be metRequires explicit waits or custom conditions to handle timing issues
Explicit waitingSupports explicit waits using methods like waitForSelector, waitForEventSupports explicit waits using WebDriverWait and ExpectedConditions
Timeout configurationConfigurable at multiple levels (e.g., per action, per test, globally)Configurable via setScriptTimeout, setPageLoadTimeout, and implicitlyWait

Playwright:

  • In Playwright, automatic waits are built into the framework to help manage the synchronization of actions with the state of the webpage
  • Playwright automatically waits for elements to be ready before performing actions like clicking, typing, or asserting
  • Assertions with auto-waiting are a powerful feature that helps ensure your tests are robust and reliable. These assertions automatically retry until the expected condition is met or a timeout is reached

Selenium:

Implicit Wait

  • Applies to all elements in the WebDriver instance. Once set, it will be used for the lifetime of the WebDriver object.
  • It’s used to instruct the WebDriver to wait for a certain period when trying to locate an element before throwing a NoSuchElementException.

    Explicit Wait:
  • Applies only to the specific element(s) and condition(s) for which it is set. It needs to be specified each time for each condition.  
  • It’s used to wait for a specific condition to occur before proceeding further in the code execution. Commonly used conditions include element visibility, element clickability, and presence of an element.

Supporting browsers: : Playwright vs Selenium

PlaywrightSelenium
Chromium, Firefox, WebkitChrome, Safari, Edge, Firefox, Internet Explorer, Opera
Supports mobile view portsSupports mobile view ports
Supports headed and headless modesSupports headed and headless mode
Not very much suitable for cross-browser testingHighly suitable for cross-browser testing

Playwright:

Playwright can run your tests in multiple browsers and configurations by setting up projects in the config.

Selenium:

Launching different browsers using the respective webDrivers.

Parallelization

PlaywrightSelenium
Automatic test sharding across workersManual configuration needed
Automatically balances load across workersRequires custom setup in Grid

Playwright:

-By default, Playwright runs test files in parallel
-In order to achieve even greater parallelization, you can further scale Playwright test execution by running tests on multiple machines simultaneously. We call this mode of operation “sharding”

playwright parallel

https://playwright.dev/docs/test-sharding#sharding-tests-between-multiple-machines

image4

Selenium:

In Selenium, test sharding is typically achieved through various strategies depending on the testing framework and setup you are using:

TestNG:

Selenium Grid:’

Types of testing supported

                Playwright                    Selenium
Web UI testingWeb UI testing
API testing In built not supported
Visual RegressionNA
Accessibility testingNA
Load Testing- K6 integrationLoad Testing- Jmeter integration
Component Testing [Experimental phase]Similar to JUnit, Mockito

Supporting languages

                    Playwright                  Selenium
Javascript/TypescriptJava
PythonC#
C#Python
JavaRuby, Kotlin

Performance: Selenium vs Playwright

        PlaywrightSelenium
startup timePlaywright is designed to be fast. It launches browsers in a headless mode by default, which reduces the startup time significantly.Selenium tends to have a longer startup time, especially when using Selenium Grid to distribute tests across multiple machine
parallel ExecutionPlaywright supports parallel test execution out-of-the-box with minimal configuration, enabling faster test runsSelenium supports parallel execution through frameworks like TestNG and JUnit, and through Selenium Grid. However, setting up and managing Selenium Grid can be complex and time-consuming
FlakinessLess flakyModerately Flaky

Dependencies

PlaywrightSelenium

Browser libraries
Built-in (playwright install handles browser installation)Separate downloads required (ChromeDriver, GeckoDriver, etc.)
        Testing FrameworkPlaywright/test (built-in testing framework) TestNG, PyTest, Appium
Shared/Cloud EnvironmentMicrosoft Playwright TestingSelenium Grid

Community support

                  Playwright                Selenium
Upcoming tool, moderately availableHighly available, used by many people

Beyond Selenium and Playwright: Exploring Better Alternatives

Selenium and Playwright, despite their open-source nature, present challenges in terms of scalability, maintenance, and cross-browser compatibility. Consider exploring other options that address these limitations and provide a more efficient testing experience.

TestGrid offers a modern approach to end-to-end testing with its AI-powered codeless platform. TestGrid provides a comprehensive suite of features to simplify and accelerate your testing process:

  • Test your web applications and mobile apps across a wide range of browsers, devices, and operating systems. Ensure your app delivers a consistent and flawless experience on all platforms.
  • TestGrid’s AI capabilities to automatically generate test cases from user stories, requirements, and other artifacts. This eliminates the need for manual script writing and saves you valuable time.
  • Identify visual regressions and ensure your app’s UI elements look and function as intended. TestGrid’s visual testing capabilities help you maintain a consistent user experience.
  • Seamlessly integrate TestGrid with your existing development and collaboration tools like Jira, Teams, and more. This streamlines your workflow and improves team communication
  • Generate comprehensive test reports with detailed insights on test execution, defect tracking, and overall test coverage.
  • No need for local installation or setup! Simply create and run automated tests directly on the cloud, eliminating infrastructure management overhead.

Conclusion

And finally both Playwright and Selenium offer compelling advantages. Playwright, with its modern architecture and native support for multiple browsers contexts, waiting, reporting, debugging etc represents a robust solution for engineers looking for speed and simplicity in managing concurrent tests across various environments. 

Selenium, on the other hand, stands as the time-tested giant with extensive browser support and a vast community, making it a versatile choice for complex, long-term projects.

Choosing between Playwright and Selenium ultimately depends on the specific project requirements, team’s programming expertise, and the long-term maintenance plan. For teams prioritizing a modern, efficient tool with the latest features in web automation, Playwright is the way forward. Conversely, if your project demands extensive browser compatibility and has an infrastructure that leverages the deep integrations and plugins that Selenium supports, then Selenium remains unmatched.