Playwright vs WebDriver: The Future of Browser Automation

How Playwright compares to WebDriver and why you should consider it for future web automation projects

Adam Tapper
Slalom Build
5 min readApr 1, 2021

--

In the quest to write scalable, reliable, and fast test automation, new tools are routinely introduced into the market. In 2020, Playwright was released by the Microsoft team and many people are asking — is it ready to be used or should our test automation remain on the “tried and trusted” WebDriver automation tools? In order to answer that question, we have to understand what Playwright is and how it differs from WebDriver.

What is Playwright?

Playwright is a web test automation library that tests against the underlying engine for the most popular browsers: Chromium for Chrome and Edge, Webkit for Safari, and Gecko for Firefox. Test scripts can be written in JavaScript, Python, C# and Go. Playwright leverages the DevTools protocol to write powerful, stable automated tests.

At this point, you may be asking — wait…what are these DevTools he speaks of and why are they important?

WebDriver vs. DevTools

As web automation has become more common over the last decade, WebDriver has been the underlying protocol leveraged by tools such as Selenium, Protractor, and WebdriverIO. Using this protocol, commands are sent from the test script to a driver that is specific to the browser under test. That driver serves as a middleman that translates the commands from the test script and sends them to the browser to be executed. Thus, the test script and browser are not directly aware of each other which limits the features that can be used and introduces timing issues as the test script does not know the current state of the browser as it is sending its commands. Overall, WebDriver provided a step forward in web automation, but it caused frustration due to timing issues producing false failures, a limited feature set due to not interacting directly with the browser and relying on the maintenance of a large amount of web drivers for each browser.

Enter the DevTools protocol which can be used to directly control and inspect the browser. Most modern browsers have adopted the Chrome DevTools Protocol which allows test scripts to communicate directly with the browser using websockets thus eliminating many of the issues of WebDriver and introducing a whole host of new features. This protocol was first used by Puppeteer, an open source web automation tool built by Google. However, Puppeteer did not offer support for Safari or Firefox. Microsoft hired developers from the Puppeteer team to build Playwright as an advanced version of that tool that provided more features and broader browser support.

Top Features of Playwright

The most powerful argument for using Playwright is the new features that it introduces. Because Playwright can actually see into and control the browser rather than relying on a middle translation layer, it allows for simulation of a more insightful and relevant user scenarios. We will explore a few of those key features below.

Auto-Waits

Rather than having to control the waiting through code in test scripts, Playwright does various actionability checks on elements before performing certain actions. This makes the tests more reliable and easier to maintain as demonstrated below.

WebDriverIO test that must wait for links to be clickable
Playwright test that automatically waits for links to be clickable

Network Control

Test scripts can vary conditions for the application under test by simulating file uploads and downloads, handling various methods of authentication, intercepting network requests and mocking out request responses.

Intercepting a network request to send an unauthorized response and capture how the web page responds

Browser Contexts

Browser contexts allow the simulation of incognito sessions as well as multi-page scenarios. This allows for testing of persistent sessions between tabs as well as ensuring that the website can function in incognito mode.

Opening Google in an incognito session to ensure the browser does not remember the login cookie

Permissions

Permissions such as notifications and geolocation can be enabled and simulated; user settings such as the color scheme being changed to dark mode or print.

Setting the user’s location to Chicago before opening Google Maps

Limitations of Playwright

While Playwright is a powerful tool with many new features, its novelty does bring about some limitations.

Patched Browsers

Because DevTools are not publicly exposed in Webkit and Gecko, the Playwright team had to submit patches to the engines that were reviewed and approved by the Webkit and Gecko teams for exposing the DevTools. This may change in the future to be a permanent change to the engines eliminating the need for patches.

Limited Browser Version Support

Due to the needed patches, as well as the fact the scripts run against the underlying engine, there is limited support for older browsers and no support for IE11. However, specifying a type of browser can be done provided you have the browser downloaded locally.

Launching a Specific Browser Version

Mobile Device Testing

Playwright supports mobile testing via device emulation rather than actual mobile devices. This may change in the future but for now, custom mobile devices can be defined by specifying their userAgent and viewport or you can use the predefined mobile devices in the deviceDescriptors file.

Mobile Device Emulation

Playwright vs. WebDriver

The question that we originally set out to answer was whether Playwright was ready to be used or if we should continue to use WebDriver tools despite their limitations. The definitive answer is…it depends.

The only case where accepting the limitations of WebDriver makes sense is when the focus of testing is specifically on whether the website renders properly in various versions of the same browser. However, when the testing is focused on how users will interact with a given website, Playwright is much more powerful than WebDriver given the features discussed above. Overall, Playwright is an exciting new tool and the big step towards moving web automation off of the WebDriver protocol.

--

--