Selenium is widely recognized for its essential role in web testing. However, it is also known for challenges associated with unpredictable selectors, which can lead to inconsistent and unreliable tests.
The name “Selenium” is a playful jab at a competitor named “Mercury,” hinting at the idea that just as selenium can counteract mercury poisoning, Selenium the tool could outdo Mercury the tool. This clever naming has more than lived up to its promise, making Selenium a mainstay in web automation. It’s become so ingrained that it’s used by a wide array of people, from QA professionals and developers to students and even retirees dabbling in tech during their free time.
For those stepping into the world of test automation, it’s highly likely that Selenium will be their first port of call. That’s because there’s a wealth of guides and resources available, making it seem like the only viable choice. Newcomers might be led to believe that other tools are too complex or intimidating, which isn’t necessarily the case. Today, the field of automation tools has evolved, and new tools like Playwright are proof to this assertion. Playwright enters the scene as a modern alternative, offering powerful features while remaining accessible to users of all levels. Let’s take a closer look.
Evaluating Web Testing Tools: Playwright Meets Selenium
Playwright is a Microsoft product developed by a team that previously created another framework for automated tests, called “Puppeteer” (looks like they moved from dolls on strings to the real drama). It provides practically everything, and I would say even much more, than what the average user gets from Selenium. But firstly, let’s try to compare them factually and even to some degree objectively.
Note: From here on, I will refer to Selenium Webdriver just as “Selenium”, because I am mainly comparing the Webdriver part with Playwright, focusing on test framework capabilities. Towards the end, I’ll talk about features that make testing easier, like maintenance and debugging, which Selenium gets from its related tools. Now let’s compare Playwright vs Selenium.
1.Supported Browsers
Previously, one advantage that Selenium had over Playwright was its support for Internet Explorer. However, if you are not specifically targeting the South Korean market, this might not be a significant factor. Additionally, it’s important to consider that Microsoft officially phased out Internet Explorer on June 15, 2022, for Windows 10, favouring Edge instead. So we can’t count that as a flaw for a Playwright, because it supports 3 browser engines: Chromium (which covers Chrome, Opera, and the above-mentioned Microsoft Edge), Firefox, and Webkit (Safari). I would say they are on equal footing in terms of browsers.
2.Programming Languages
Regarding programming languages, both support Java, Python, C#, and JavaScript. The differences are that Playwright also works with TypeScript (which by some could be considered a superset of JavaScript, then a whole separate language), while Selenium works with Ruby.
Java and Python are the most popular options for Selenium users.
On the other hand, TypeScript/JavaScript is seen as the preferred choice for Playwright. This preference is due to Playwright’s main API being originally written in Node.js. While one’s language preferences could be a deciding factor, both frameworks offer a fairly similar set of supported languages. Looking at the pure quantity of options, Selenium gets a point.
3.Browser actions
Both frameworks offer support for the basic actions needed in daily work, but differences emerge in how they handle more complex tasks.
In Selenium, when built-in methods don’t work, there is a high chance you would have to create a custom function or resort to the use of Actions class. With this, you must describe a sequence of precise instructions to get a correct result of, for instance, drag-n-drop or scrolling actions.
Playwright, on the other hand, offers a more extensive library of methods. These methods work reliably right from the start. This is due to Playwright’s strategy of interacting with elements by firing related events. This strategy avoids trying to simulate human actions through the driver. As a result, Playwright provides faster and more consistent outcomes during test runs. It also makes the code easier to read and maintain.
Speaking about configurations, Playwright has an ace up its sleeve. It offers the functionality of contexts for each window or tab, which could include cookies, session data, and other settings. This gives more control and precision over test execution. Yeah, some cases, like working with alerts, could be less intuitive, due to that event promise approach, but I would say Playwright is better in this regard, based on the vastness and reliability of options.
4.Waits
I will not discuss the correct approach of using waits in automated scripts. However, despite fixed and implicit waits being not advisable, the practice of defining wait time as a separate variable in Selenium scripts, in Java or Python, still irritates me to this day. I haven’t even touched on the need for waits for element states like visible or interactable.
Playwright, developed in an era of dynamic, complex, and constantly evolving websites, addresses this issue head-on. It leverages built-in auto-waiting to handle the heavy lifting. For instance, Playwright’s click function automatically ensures several conditions are met without the need for extra instructions. For example, a click function, without additional instructions, will ensure that the:
- locator resolves to an exactly one element;
- element is Visible;
- element is Stable, as in not animating or completing animation;
- element Receives Events, as in not obscured by other elements;
- element is Enabled.
Playwright is fast, reliable, and patient when it comes to waiting. It will try to check a condition 20 times before throwing an error, without any modifications. The timeout amount can be specified for the whole suite, a specific project, or a test case, using settings in the config file. This timeout can still be overwritten in particular action cases.
5.Selector strategies
Playwright can pierce shadow DOM without xPath, and that’s all, I don’t know what else you need to hear. That was the sole reason for some people to switch to this fairly new product.
Selenium and Playwright both list 8 locator strategies in their documentation. However, Playwright merges CSS and xPath locators into one method. This approach sets it apart.
For edge cases, you can specify at the beginning of the string whether it’s “css=” or “xpath=”. The philosophy behind Playwright’s locators focuses on visible text and identifiers, rather than on the structure of elements, in the context of rapidly changing Single Page Applications (SPAs). Looks like both are evenly matched, but again, one pierces shadow DOM.
6.Speed
When people discuss Playwright’s advantages, speed is often the first trait mentioned. This advantage incorporates earlier discussions on browser actions and waits, as well as context functionality that speeds up browser window launches. In controlled environments, Playwright achieves a speed increase of about 20% over Selenium. While some might see these claims as subjective, there are studies that support this observation.
Playwright is designed for high isolation of test cases, aiming to run everything in parallel. It even suggests splitting tests if the execution time approaches the 30-second mark. Interestingly, Playwright’s predecessor, Puppeteer, might outperform it in some scenarios. However, comparing the two is like contrasting a sports car with a luxury vehicle, each with its unique set of enhancements for quality of life.
Native Features and Installation
Installing Selenium from scratch on a new machine can be a lengthy process. This is because it requires installing the driver and also adding test runners, reporters, configurations, and other essential parts of the framework. It could take up to an hour to set everything up, unless you’re using a Selenium suite solution that’s already built. Even using a package manager doesn’t completely address this issue. This situation is where the comparison between Playwright and Selenium (Webdriver) starts to seem a bit uneven.
If you’re using Node.js, the installation process simplifies to just one npm install command. This command get all necessary components for running tests. Concerned about a Missing Browser? There’s no need to worry; it will be automatically downloaded and installed. Looking for a Test Runner? There’s one built-in, or you can choose from other popular alternatives. This includes a highly efficient VS Code extension.
Need a Test Reporter? You can select the format you prefer. Interested in adding traces, screenshots, and videos of your test runs for better debugging? Playwright includes an integrated visual debugger known as UI mode to assist you. Wondering how to handle 2FA and constant login steps? Playwright makes setting up reusable authentication states straightforward. And if you’re considering recording tests or even conducting API tests, Playwright supports those too.
When installing Selenium feels like buying a smartphone without earphones, power adapter, or USB cord (welcome to 2024, I guess), Playwright feels like getting it delivered straight to your couch with all the accessories, but also with family/pet pictures intact and apps already installed.
Some may argue that the extensive list of features still doesn’t cover all possible problems and that the Selenium enormous community sure has all the answers on StackOverflow. But firstly, the most common questions for Playwright are covered in documentation or active GitHub threads, and secondly, the Playwright community is growing by the day.
In Conclusion
After reading all of these opinions, you could be under the impression that this comparison is just to show that Selenium is worse. Perhaps, the game was rigged from the start. But to be frank, every particular situation or task can have several suitable solutions, depending on the needs and resources. And in future, we could cover other products, which would be more similar.
The main purpose of discussing all this is to highlight that beyond the plethora of Selenium tutorials you find after searching for “QA Automation,” there are other options available. There are more frameworks, tools, and opportunities out there.