Playwright vs Selenium: A Comparison
This post helps QAs decide when to use which framework for better testing by providing a comparison of two popular testing frameworks: Playwright and Selenium.
Join the DZone community and get the full member experience.
Join For FreeWhat Is Playwright?
Playwright by Microsoft is the newest addition to the Headless Browser Testing frameworks in popular use. Built by the same team which created Puppeteer (Headless Browser Testing Framework for Google Chrome), Playwright, too, is an open-source NodeJS based framework.
However, it provides wider coverage for cross-browser testing by supporting Chrome, Firefox, and WebKit, while Puppeteer supports Chrome and Chromium browsers only. Playwright is compatible with Windows, Linux, and macOS, and can be integrated with major CI/CD servers such as Jenkins, CircleCI, Azure Pipeline, TravisCI, etc., in addition to the testing frameworks like Jest, Jasmine, Mocha. Besides JavaScript.
- Playwright also supports multiple programming languages such as Python, Java, and .NET C#, giving more options to QAs writing test scripts.
- Playwright is highly useful for performing cross-browser testing on complex applications, due to its wide coverage, accuracy, and high speed. It offers end-to-end testing through its high-level API that allows the tester to control headless browsers.
When the tester runs a Playwright test script, the UI is readied at the backend before the test interacts with web elements. While for other frameworks, testers have to write code for the wait explicitly, Playwright ensures auto wait, making it easier to write concise test scripts. It also provides flexible testing through its capabilities, which cover a wide range of complex scenarios for comprehensive testing.
The auto wait feature in Playwright performs all relevant checks for an element, and the requested action is performed only when the checks are duly passed. This ensures that the elements perform as expected and the test results are more accurate.
Some actionability checks performed by Playwright include Attached, Visible, Stable, Receive Events, and Enabled.
Playwright also supports the execution of simultaneous tests (also known as parallel testing) through Browser Context. This scales up testing and comes in handy when multiple web pages have to be tested simultaneously. Here one browser instance is used to create multiple, concurrent, and isolated browser contexts, which can be closed when not needed. Each of these browser contexts could host multiple web pages simultaneously. Thus, scaling up when the volume is high and reducing it when not required, ensures optimal usage of resources.
How To Run Playwright Tests
While Playwright launches browsers in the headless mode by default, it can also be used to run the browsers in headful mode. By passing a flag, when the browser is launched, Playwright can be used to run browsers in the headful mode for tests.
The following code can be used to launch a headful browser:
const { chromium } = require('playwright'); //to launch the headful browser for firefox and webkit, replace chromium by firefox and webkit const browser = await chromium.launch({ headless: false });
For Linux systems, xvfb
is essential for launching headful browsers. Since xvfb
is pre-installed in Docker Image and Github Action, running xvfb
before the Node.js command allows the browsers to run in the headful mode.
xvfb-run node index.js
What Is Selenium?
Selenium is an open-source automation testing suite that is widely used for automation testing of web applications. It automates browsers and interacts with UI elements to replicate user actions in order to test whether a web application is functioning as expected.
Through its single interface, the Selenium framework allows the tester to write test scripts in different languages such as Java, Ruby, Perl, C#, NodeJS, Python, and PHP to name a few, offering flexibility.
Selenium supports a wide range of browsers and their different versions to enable cross-browser testing of web applications. It is the most popular framework used to test websites and ensure seamless and consistent user experiences across different browser and device combinations. That is why Selenium is one of the most trusted automated testing suites in the software industry.
Playwright vs Selenium
Criteria |
Playwright |
Selenium |
---|---|---|
Language | Supports multiple languages such as JavaScript, Java, Python, and .NET C# | Supports multiple languages such as Java, Python, C#, Ruby, Perl, PHP, and JavaScript |
Ease of Installation | Easy to Install | Easy to Install |
Test Runner Frameworks Supported | Mocha, Jest, Jasmine | Mocha, Jest, Jasmine, Protractor, and WebDriverIO |
Prerequisites | NodeJS should be installed | Java, Eclipse IDE, SeleniumStandalone Server, Client Language Bindings, and Browser Drivers should be installed |
Operating Systems Supported | Windows, Linux, and Mac OS | Windows, Linux, Solaris, and Mac OS |
Open Source | Open Source and Free | Open Source and Free |
Architecture | Headless Browser with event-driven architecture | Layered Architecture based on JSON Wire Protocol |
Browsers Supported | Chromium, Firefox, and WebKit | Chrome, Firefox, IE, Edge, Opera, Safari, and more |
Support | Since Playwright is fairly new, the support from the community is limited as compared to Selenium | Provides commercial support for its users via its sponsors in Selenium Ecosystem along with self-support documents. Strong community support from professionals across the world |
Real Devices Support | Does not support real devices but supports emulators | Supports real device clouds and remote servers |
Which One Is Preferred: Playwright or Selenium?
Both Playwright and Selenium have their own advantages and limitations, which means choosing between them is subjective to the scenario for which they will be used.
Although Playwright offers fast testing in complex web applications with headless architecture and just requires Node.js as a prerequisite, it is fairly new and lacks support on various levels such as community, browsers, real devices, language options, and integrations. Selenium has all of this to offer.
However, each of them supports CI/CD for a software project with due accuracy. Playwright has an upper hand in complex web applications but has limited coverage. On the contrary, Selenium offers wide coverage, scalability, and flexibility, along with strong community support.
Published at DZone with permission of Garima Tiwari. See the original article here.
Opinions expressed by DZone contributors are their own.
Comments