When trying to find the best web-based applications for software testing, how do you select the best one? Although perhaps you’re considering hiring an external team for QA services, you’d still need to decide on a particular app to do these tests on. So which should you choose? Playwright and Selenium are both test web applications. Playwright is a newer, open-source tool developed by Microsoft, while Selenium is an open-source tool that has been in the industry for a long time. How do their features compare, and which one should you choose? To understand this, let’s first briefly look at both tools. Let’s go straight into Playwright vs. Selenium to get a closer look.
Selenium
Jason Huggins originally introduced Selenium in 2004. The project has since then received close to 30K commits on GitHub by volunteer contributors who’ve generously donated thousands of hours in code development and maintenance.
Selenium is a suite of tools that are used to automate web browsers. It is the most popular framework used to test websites and ensure seamless and consistent user experiences across different browser and device combinations. Selenium comprises multiple components, such as Webdriver, Selenium IDE, and Selenium Grid.
The Webdriver allows you to programmatically control a web browser and interact with a web page. It is commonly used for automating functional, and regression testing of web applications and can be integrated with a variety of other tools to create a comprehensive testing solution.
With Selenium IDE, users can record their interactions with a web page and export the recorded script as a reusable, maintainable, and portable testing script in various programming languages. Selenium IDE is an easy-to-use, lightweight tool and is well-suited for creating simple test cases and performing basic test automation tasks.
Selenium Grid enables users to run tests on multiple machines simultaneously. This allows users to test their web applications in various environments and configurations and run tests in parallel, which can greatly reduce the time it takes to complete a test suite. Selenium Grid is composed of a hub and one or more nodes. The hub receives test requests from clients and distributes them to the appropriate node for execution. Each node runs one or more web browsers and is responsible for executing the tests and returning the results to the hub. Selenium Grid’s advantages are numerous, but one of the key benefits is the ability to easily and rapidly scale test infrastructure to match testing requirements.
Selenium recently released Selenium Version 4 with some major upgrades.
Playwright is a tool for end-to-end testing of web applications. It is similar to other testing tools like Selenium, but it is designed to be faster and easier to use. Playwright allows you to write tests in TypeScript, JavaScript, Python, .NET, and Java and run them against popular web browsers like Chromium, Firefox, and WebKit. Playwright also supports mobile web testing by native mobile emulation of Google Chrome for Android and Mobile Safari.
Playwright comes with a powerful set of tools like Codegen, Playwright Inspector, and Traceviewer. Codegen tool generates tests by recording your actions and lets you save them in any language.
The Playwright inspector tool can be used to inspect pages, generate selectors, step through the test execution, see click points, explore execution logs, etc. The trace viewer tool can capture all the information to investigate the test failure. Playwright trace contains test execution screencast, live DOM snapshots, action explorer, test source, etc.
Despite being far more recent than Selenium, Playwright is gaining momentum and an extensive fan base. It supports fewer browsers and languages than Selenium, but it also has more recent features and capabilities that align with the modern web.
Now that we understand the basics of both tools let’s look at the differences.
Playwright vs. Selenium
Prerequisites
Selenium requires a standalone selenium server or Client Language Bindings and browser drivers to get it to work.
For Playwright, only NodeJS needs to be installed. You can then get started by installing Playwright using npm or yarn. Playwright will download the browsers needed.
Languages Supported
Python, Java, JavaScript, C#, Ruby, and Php are supported by Selenium, whereas Playwright supports JavaScript, TypeScript, Java, Python, and .NET.
Selenium supports more languages, which can make a difference when choosing the tool that best fits you.
Test Runner
Playwright comes with its own powerful playwright runner. You can also use third-party test runners such as Jest-playwright (playwright jest), AVA, Mocha, etc.
With Selenium, you can use Selenium IDE to create tests and then use Selenium Command Line Runner or Selenium SIDE runner to run these tests.
Element Locators
Locators represent a way to find element(s) on the page at any moment.
Selenium supports locators like Id, Name, Class, Link Text, Partial link text, Xpath, and CSS. Additionally, in Selenium 4, “relative locators” was introduced to allow users to specify where on the page an element can be found using things like “above that element” or “to the right of this other element.” Selenium assumes these locators are already loaded on the page, which can sometimes lead to flaky tests.
Playwright, on the other hand, uses its Locator API to find and interact with elements. Some of the built-in locator’s page.getByText(), page.getByLabel(), page.getByTitle(), etc., make it really easy to find elements on a page. Playwright also supports filtering locators by text with the locator.filter([options]) method. It will search for a particular string somewhere inside the element, possibly in a descendant element, case-insensitively.
Waits
In Selenium, WebDriver does not track the active, real-time state of the DOM, leading to flaky tests. To tackle this, we have to use different types of waits, like implicit waits (by implicitly waiting, WebDriver polls the DOM for a certain duration when trying to find any element.) and explicit waits (allows your code to halt program execution, until the condition you pass it resolves.)
Playwright comes with a built-in auto-wait strategy. It performs a range of actionability checks on the elements before making actions to ensure these actions behave as expected. It auto-waits for all the relevant checks to pass and only then performs the requested action. If the required checks do not pass within the given timeout, the action fails with the TimeoutError. This helps Playwright eliminate flakiness.
Parallel Test Execution
Playwright and Selenium both support parallel test execution. With Playwright, it comes out of the box, but with Selenium, one can achieve that by using third-party solutions such as TestNG. To achieve more advanced and scalable parallel testing, you can use Selenium Grid.
With Playwright, you can have test scenarios that span multiple tabs, multiple origins, and multiple users. You can also create scenarios with different contexts for different users and run them against your server, all in one test.
Visual Testing
Playwright Test includes producing and visually comparing screenshots using await expect(page).toHaveScreenshot(). Apart from screenshots, you can use expect(value).toMatchSnapshot(snapshotName) to compare text or arbitrary binary data. Playwright Test auto-detects the content type and uses the appropriate comparison algorithm.
To achieve visual testing in Selenium, we need to incorporate external tools or libraries like Java imageIO, aShot, Applitools Eyes, etc.
Test Recording and Screenshots
Screenshots and recordings help debug test failures, especially when running in headless mode in a CI environment. Playwright is shipped with these capabilities out of the box, but Selenium requires third-party extensions or additional coding to achieve this. With Playwright, we also have the flexibility to capture the full screen or just one element.
Assertions
Assertions are used to verify that the state of the web application in question is as expected. To use assertions in Selenium, you can use the built-in assert keyword in your code or a third-party assertion library such as JUnit or TestNG.
Whereas Playwright uses JEST, expect library for test assertions. This library provides a lot of matchers like toEqual, toContain, toMatch, toMatchSnapshot , and many more. Playwright also extends it with convenient async matchers that will wait until the expected condition is met.
Test Reporting
Playwright Test comes with a few built-in reporters for different needs, and it also has the ability to provide custom reporters. All built-in reporters show detailed information about failures and mostly differ in verbosity for successful runs.
With Selenium, proper test reporting can be achieved only using third-party extensions like TestNG reporter, Allure reporter, etc.
API Automation
Selenium does not have in-built API testing support. We need to use libraries and respective classes for REST etc.
Playwright has in-built API support. Playwright can be used to get access to the REST API of your application, thereby eliminating the need to install external libraries.
Performance or Execution Speed
For Selenium versions earlier than Selenium 4, JSON Wire Protocol was used for communicating with the web browser from the local end. As the Selenium Client libraries use the JSON protocol, and the web browser uses the W3C protocol, API encoding and decoding were involved in the entire process, making it a slow process. Starting with Selenium 4, the JSON Wire Protocol will no longer be supported, and only WebDriver W3C Protocol will be used, making the tests more stable.
Playwright’s script execution is quicker than Selenium’s. Playwright communicates all requests through a single web socket connection. What it means is once the tests are invoked, the code is automatically converted into JSON format and will be transferred to the server using the web socket protocol. This connection stays in place until the test execution is completed. Thereby reducing the points of failure and increasing performance.
Community Support
Due to being relatively new, Playwright has limited community support and documentation compared to Selenium. However, for beginners, we highly recommend taking a Playwright automation course. It provides comprehensive guidance and resources to help you get started effectively.
Selenium is a well-aged tool offering support for its users via the Selenium community and many self-support documents published on the internet.
Playwright vs. Selenium: Which One To Choose?
Playwright is brand-new, in the making, and supports both API and UI test automation without requiring a change in framework. Meanwhile, Selenium is universally known and comes with the upside of a smaller learning curve. That being said, innovative businesses are already picking Playwright over Selenium.
Frequently Asked Questions
Is Playwright better than Selenium?
Playwright and Selenium are tools for web applications’ testing and automation. Both are open source with Playwright being developed by Microsoft, while Selenium was originally an internal tool at Thoughtworks and has been part of the industry for a longer time.
Is Selenium better than Playwright for scraping?
Selenium and Playwright can be effective tools for web scraping. Selenium provides greater browser compatibility and flexibility. However, for those looking for simplicity and a more intuitive option, there’s Playwright. Depending on the project’s requirements and details.
Does Playwright work with Selenium?
No, despite having similar functionalities, Playwright is a separate tool from Selenium. Instead of combining both tools, Selenium users should migrate to Playwright. Those looking for an easy transition and enhanced features should try Playwright.
Afsal Backer is a test automation engineer with experience in building UI and API test automation frameworks, implementing CI pipelines for QA, and test execution in AWS. He has certifications from Test Automation University. Afsal often shares on his LinkedIn and blog.