Playwright vs Selenium: Key Differences

Matt PaulMatt Paul
10 min read

Introduction

When it comes to automated testing of websites, test automation frameworks are crucial. In terms of prominent test automation frameworks, Playwright and Selenium rank significantly.

Selenium has been enabling browser automation for a long time, while Playwright is a more recent Framework. Playwright is compatible with WebKit, Chrome, and Firefox; Selenium is great for cross-browser testing since it supports a large number of browsers.

If you're trying to decide between Playwright and Selenium for your website's test automation, this article should assist you at best.

Overview of Playwright

Playwright is a platform for comprehensively testing online applications. While it shares some similarities with Selenium and related testing tools, it is more user-friendly and meant to be quicker. Mainstream web browsers like as Chrome, Firefox, and WebKit can be tested using Playwright, which supports TypeScript, JavaScript, Python, .NET, and Java. By simulating Google Chrome on Android and Safari on iOS natively, Playwright enables mobile web testing as well.

Included with Playwright is a robust suite of tools, including Codegen, Playwright Inspector, and Traceviewer. You can create tests in any language using the codegen tool, as it records your activities and then lets you save them.

Page inspection, selector generation, test execution, click point visualization, execution log exploration, and more are all within the purview of the Playwright inspector tool. All the necessary data to examine the test failure can be captured using the trace viewer tool. You may find test source, action explorer, live DOM snapshots, test execution screencasts, and more in Playwright trace.

Playwright is rapidly becoming a popular choice among followers, although it is much younger than Selenium. Though it lacks Selenium's support for some languages and browsers, it makes up for it with functionalities and capacities that are more in line with the current web.

Overview of Selenium

Selenium was first presented in 2004 by Jason Huggins. Since then, hundreds of hours of code development and maintenance have been graciously provided by volunteer participants,

For the purpose of automating web browsers, Selenium provides a collection of useful technologies. If you want to make sure that your website works flawlessly on all devices and browsers, this is the framework to choose. Webdriver, the Selenium IDE, and the Selenium Grid are all parts of the larger Selenium framework.

Through the use of the Webdriver, you can access a web browser and its contents from within its code. The automation of functional and regression testing of web applications is its primary use case, and it can also be utilized with additional tools to provide a thorough testing solution.

To create reusable, manageable, and portable testing scripts in several languages, Selenium IDE users can save their activities with a web page and export the script. For elementary test automation activities and the creation of basic test cases, the lightweight and user-friendly Selenium IDE is an excellent choice.

Users may run tests on numerous computers concurrently with Selenium Grid. With this, users can run tests in parallel, test their web apps in different settings, and ultimately finish test suites faster.

The building blocks of a Selenium Grid are a hub and certain nodes. Clients submit test requests to the hub, which then assigns them to the relevant node. The testing and subsequent reporting of findings to the hub are the responsibility of each node, which is in charge of running one or more web browsers. The capacity to quickly and effectively scale test infrastructure to meet testing needs is one of the many perks of Selenium Grid.

Key Differences: Playwright Vs. Selenium

1. Prerequisites

The functionality of Selenium cannot be achieved without either the Client Language Bindings and browser drivers or a standalone Selenium server. Just make sure you have NodeJS set up for Playwright. After that, run Playwright using either npm or yarn to get things rolling. The necessary browsers will be downloaded by Playwright.

2. Languages Supported

While Selenium is compatible with Python, Java, JavaScript, C#, Ruby, and PHP, Playwright is compatible with JavaScript, TypeScript, Python, Java, and .NET. When considering whether a tool is right for you, the fact that Selenium backs various languages can be a deciding factor.

3. Test Runner

A robust playwright runner is included with Playwright. Outside test runners like Jest-playwright (playwright jest), AVA, Mocha, etc., are also an option. The Selenium IDE is where you may build your tests; the Command Line Runner and the SIDE runner are how you execute them.

4. Element Locators

The use of locators allows for the instantaneous search of element(s) on the website. XPath, CSS, Link Text, Partial Link Text, Name, Class, and Id are all types of locators that Selenium can use. In addition, "relative locators" were added to Selenium 4 to let users describe the position of an element on the page using expressions like "above that element" or "to the right of this other element."

Testing can get shaky at times due to Selenium's assumption that these locators are already loaded on the page. Conversely, Playwright's Locator API is used for element discovery and interaction. It is extremely easy to discover components on a page using some of the built-in locator methods, such as page.getByText(), page.getByLabel(), page.getByTitle(), etc.

It is also possible to use the locator.filter([options]) function in Playwright to filter locators based on text. The search will be case-insensitive and look for a specific string within the element or one of its descendant elements.

5. Waits

Selenium tests are not reliable because WebDriver does not monitor the DOM's active, current state. Various kinds of waits, such as implicit waits, are required to address this issue. Implicit waits cause WebDriver to poll the DOM for a specific amount of time in order to locate any element using explicit waits (which enable your code to stop running the program until the scenario you provide it settles).

A built-in auto-wait method is included with Playwright. In order to make sure that actions operate as intended, it runs a battery of actionability tests on the components. It will not carry out the action you've asked for until all the necessary checks have been passed.

A TimeoutError is thrown when the necessary checks are not completed within the specified timeout. Playwright is able to get rid of flakiness using this.

6. Running Tests in Parallel

Tests can be executed in parallel by both Playwright and Selenium. Playwright has it built in, whereas Selenium requires third-party technologies like TestNG to accomplish the same thing. Selenium Grid allows you to accomplish more sophisticated and efficient parallel testing.

Test scenarios with numerous tabs, sources, and users are possible using Playwright. It is also possible to execute many user-specific scenarios against your server simultaneously.

7. Visual Testing

Making contrasting screenshots graphically using await expect(page).toHaveScreenshot() is a part of the Playwright Test. You may utilize expect(value).toMatchSnapshot(snapshotName) in addition to screenshots to match text or other binary data with ease.

Playwright Test is able to automatically identify the type of material and use the most suitable comparison method. You may integrate third-party libraries and tools into Selenium, such as Java imageIO, aShot, Applitools Eyes, etc., in order to accomplish visual testing.

8. Test Recording and Screenshots

If a test fails when executing in headless mode in a continuous integration environment, taking a screenshot or recording the test might assist in troubleshooting it. These features are pre-installed in Playwright; however, in Selenium, they need either extra code or third-party extensions. Playwright gives us the option to capture the entire screen or a specific aspect.

9. Assertions

To make sure the web app is behaving as intended, you may use assertions. You may use either the built-in assert keyword or a third-party assertion library like JUnit or TestNG to use assertions in Selenium.

Playwright relies on the JEST framework, which is known for its test assertions. Matchers such as toEqual, toContain, toMatch, toMatchSnapshot, and many more are included in this collection. In addition, Playwright adds useful async matchers that will hold off until the precondition is satisfied.

10. Test Reporting

There are a variety of pre-installed reports in Playwright Test, and users may even create their own. The built-in reporters provide comprehensive information on failures, whereas successful runs are often reported with varying levels of verbosity.

Only by integrating with third-party extensions such as TestNG reporter, Allure reporter, etc., can Selenium provide accurate test reports.

11. API Automation

It is not possible to test APIs natively using Selenium. The appropriate libraries and classes for REST, etc., must be utilized.

Playwright comes with its own built-in API. Without installing any other libraries, you can connect to your application's REST API using Playwright.

12. Efficiency or Rate of Execution

To communicate with the web browser locally in Selenium versions prior to Selenium 4, the JSON Wire Protocol was utilized.

As the web browser utilizes the W3C protocol and the Selenium Client libraries use the JSON protocol, the entire operation was slowed down by API encoding and decoding. Selenium 4 marks the end of support for the JSON Wire Protocol, replacing it with the more reliable WebDriver W3C Protocol.

Compared to Selenium, Playwright's code processing is faster. A single web socket connection is used by Playwright to convey all requests. This implies that the code is immediately translated to JSON format and sent to the server using the WebSocket protocol as soon as the tests are requested. Until the test is over, this link will remain in place. This will increase performance while decreasing failure spots.

13. Community Support

When compared to Selenium, Playwright's community support and documentation are lacking due to its relatively young age. Nonetheless, a Playwright automation course is an excellent choice for newcomers. To get you started effectively, it offers extensive information and tools.

As a mature technology, Selenium provides user help through its community and numerous online self-support publications.

Playwright Vs Selenium: Which to choose?

Since each of Playwright and Selenium has its own set of benefits and drawbacks, choosing one over the other is rather situational. Which of these two testing frameworks is better is going to rely on the top objectives and demands of the project.

Reasons To Choose Selenium

Compatible with Most Web Browsers: Chrome, Firefox, Safari, IE, Edge, and Opera are just a few of the browsers that Selenium is compatible with. It works wonderfully for testing on a variety of browsers, including the older ones.

Current and Completed Projects: A big community, thorough documentation, and a plethora of connectors characterize Selenium, making it an established technology. It works well for projects that require a lot of support and durability.

Versatility in Language Use: Java, C#, Python, Ruby, and JavaScript are just a few of the languages that Selenium is compatible with. If your team is fluent in a certain language or wants to include tests into an existing project that uses a specific language, this might be a great asset.

Existing Selenium Infrastructure: It can be more sensible to stick with Selenium instead of looking for a new solution if your project currently has a Selenium test suite and infrastructure.

Reasons To Choose Playwright

Modern Web Application Testing: With support for all major current rendering engines, Playwright is perfect for testing your modern web apps. It works well with complicated situations that include many windows, tabs, and web workers.

Trace Viewer: Once the code has executed, you can check the recorded Playwright traces with the aid of Playwright Trace Viewer, a graphical user interface tool. You can use traces to help you figure out why your tests failed on CI. Traces on trace.playwright.dev can be viewed in your browser or locally.

Headless Browser Testing: If you're using a continuous integration or continuous delivery pipeline, you may want to look at Playwright's built-in assistance for headless browser testing. Though Selenium has headless mode support as well, Playwright's approach is cleaner.

Complex Interaction Handling: Automated waiting, network interception, and support for several pages/tabs are just a few of the additional capabilities offered by Playwright that simplify the process of writing tests for complicated user interactions.

Consistent API Across Browsers: Maintaining and testing across browsers is made easier using Playwright's standardized API.

Parallel Test Execution: If you have a big test suite, Playwright can run all of the tests in parallel across several browsers and contexts, saving you time and effort. However, if you're using Selenium, you must set up Selenium Grid to do the same.

Final Remarks

In the realm of test automation, two well-known open-source browser automation technologies have taken the lead. You have learned what separates Playwright from Selenium in this post. If you are seeking a more extensive community, language support, and browser compatibility, along with access to actual device automation, Selenium is a great pick. But if you're looking for a simple, fast, and comprehensive debugging tool, Playwright is your best bet.

While both Selenium and Playwright let QA specialists and developers build powerful automated tests for online apps, they do it in different ways. Both have their own uniqueness. Therefore, you must prioritize your requirements.

0
Subscribe to my newsletter

Read articles from Matt Paul directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Matt Paul
Matt Paul