Test Automation Frameworks are vital in automated testing Websites. Playwright and Selenium are among the most popular test automation frameworks.
While Playwright is a newer Framework, Selenium is a legacy Library which has been supporting browser automation. Playwright supports Chromium, Firefox, and WebKit browsers, while Selenium offers a wide variety of browser support with Chrome, Firefox, IE, Edge, Opera, and Safari making it ideal for Cross Browser Testing.
This article covers Playwright vs Selenium in detail to help you choose the most appropriate framework for your website test automation.
Playwright vs Selenium Comparison
Below are the key differences between Playwright and 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 | Selenium is a library and can be used with Test runners like JUnit, TestNG, NUnit etc. |
Trace Viewer | Comes with Trace viewer to run and debug tests which is very convenient and Powerful tool | No Such features and depends completely on Test Runner to debug and execute tests |
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 but also support Headless Browsers |
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 and self-support documents. Strong community support from professionals across the world |
Real Devices Support | Supports real device clouds and remote servers | Supports real device clouds and remote servers |
What is Playwright?
Playwright by Microsoft is the newest addition to the Headless Browser Testing frameworks in popular use. Built by the same team that created Puppeteer (Headless Browser Testing Framework for Google Chrome), Playwright is an open-source NodeJS-based framework.
- However, it provides wider coverage for cross-browser testing by supporting Chrome, Firefox, and WebKit, while Puppeteer only supports Chrome and Chromium browsers.
- 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.
- It is highly useful for 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, allowing 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 testers have to write code for the wait explicitly for other frameworks, 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 parts 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 (AKA parallel testing) through Browser Context. This scales up testing and comes in handy when multiple web pages must 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 run the browsers in headful mode. By passing a flag, when the browser is launched, Playwright can be used to run browsers in 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
Also Read: Headless Browser Testing with NightwatchJS
What is Selenium?
Selenium is an open-source automation testing suite that is widely used for the automation testing of web applications. It automates browsers and interacts with UI elements to replicate user actions 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, 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 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.
How to run Selenium Tests
Selenium launches browsers in headed mode by default, you can also launch the browser in headless mode by passing the arguments to chromeoptions().
Example to launch browser in headed mode
WebDriver driver = new ChromeDriver(options); driver.get("https://www.bstackdemo.com/")
Example to launch browser in headless mode
ChromeOptions options = new ChromeOptions(); options.addArguments("--headless=new"); WebDriver driver = new ChromeDriver(options); driver.get("https://www.bstackdemo.com/");
First initialize an object of ChromeOptions() and then add the argument “–headless=new” which is then passed to ChromeDriver.
Key Differences in Playwright vs Selenium: Explained
Selenium and Playwright are both powerful web automation tools, but they differ in architecture, speed, browser support, and community adoption. While Selenium has been the industry standard for years, Playwright offers modern features and faster execution.
Choosing between them depends on your testing needs, such as language support, execution speed, and real device testing. Below are the key differences:
- Architecture: Selenium uses the WebDriver API and HTTP requests, while Playwright relies on a persistent WebSocket connection, making it faster and more efficient.
- Browser Support: Selenium supports a wider range of browsers, including Chrome, Firefox, Safari, Edge, and Opera, whereas Playwright supports Chromium, Firefox, and WebKit.
- Execution Speed: Playwright’s WebSocket-based communication reduces latency and speeds up test execution, whereas Selenium’s HTTP-based approach can be slower.
- Language Support: Selenium supports Java, Python, C#, Ruby, PHP, and JavaScript, while Playwright primarily supports JavaScript, TypeScript, Python, C#, and Java.
- Real Device Testing: Selenium integrates with real device clouds for mobile and desktop testing, whereas Playwright primarily offers native mobile emulation with limited real device support.
- Headless Testing: Playwright is designed with built-in headless browser support, whereas Selenium requires additional configuration for headless execution.
- Community and Ecosystem: Selenium has a larger, well-established community with extensive documentation, while Playwright, being newer, has a smaller but rapidly growing community.
- Test Runner and Frameworks: Selenium supports popular frameworks like JUnit, TestNG, and WebDriverIO, while Playwright is commonly used with Jest, Mocha, and Vitest.
- Integration with CI/CD: Both Selenium and Playwright integrate with CI/CD tools, but Playwright offers more seamless integration with modern DevOps workflows.
- Ease of Setup: Selenium requires setting up browser drivers and language bindings, whereas Playwright has a simpler setup with built-in browser binaries.
Playwright vs Selenium: Which to choose?
Both Playwright and Selenium have advantages and limitations, which means choosing between them is subjective to the scenario they will be used for. It depends on the project requirements and the priorities to choose one among these two testing frameworks.
When to Use Selenium
- Wide Browser Compatibility: Selenium supports various browsers like Chrome, Firefox, Safari, Internet Explorer, Edge, and Opera. It is ideal for testing across multiple browsers, including older versions.
- Established and Mature Projects: Selenium is a well-established tool with a large community, extensive documentation, and numerous integrations. It is suitable for projects that need stability and extensive support.
- Language Flexibility: Selenium supports multiple programming languages such as Java, C#, Python, Ruby, and JavaScript. It is useful if your team has expertise in a specific language or needs to integrate tests into an existing project using a specific language.
- Existing Selenium Infrastructure: If your project already has a Selenium test suite and infrastructure, it may be more practical to continue using Selenium rather than switching to a new tool.
When to Use Playwright
- Testing Modern Web Applications: Playwright is designed to test modern web apps, supporting all modern rendering engines (Chromium, WebKit, and Firefox). It handles complex scenarios involving multiple tabs, frames, and web workers efficiently.
- Trace Viewer: Playwright Trace Viewer is a GUI tool that helps you explore recorded Playwright traces after the script has run. Traces are a great way for debugging your tests when they fail on CI. You can open traces locally or in your browser on trace.playwright.dev.
- Headless Browser Testing: Playwright has built-in support for headless browser testing, which is often faster and more efficient for CI/CD pipelines. While Selenium also supports headless mode, Playwright’s implementation is more streamlined.
- Handling Complex Interactions: Playwright offers advanced features such as automatic waiting, network interception, and support for multiple pages/tabs, making it easier to write tests for complex user interactions.
- Consistent API Across Browsers: Playwright’s API is consistent across all supported browsers, which simplifies cross-browser testing and maintenance.
- Parallel Test Execution: Playwright supports running tests concurrently across multiple browsers and contexts, making it faster and more efficient for large test suites unless like Selenium where we need to set up Selenium Grid to run the tests in parallel.
Why Run Selenium and Playwright Tests on Real Devices
Here’s why you should run Selenium or Playwright Tests on Real Devices using BrowserStack Automate:
- Diverse Environment Testing: It enables the execution of Selenium or Playwright tests across a broad selection of browsers and operating systems, eliminating the necessity for maintaining local testing infrastructure. This ensures consistent application performance across various platforms.
- Concurrent Test Execution: By allowing simultaneous execution of multiple Selenium or Playwright test suites, BrowserStack Automate significantly cuts down on total testing time, facilitating quicker iterative feedback and accelerated deployment cycles.
- CI/CD Integration: The platform seamlessly integrates with major continuous integration and delivery systems, including Jenkins, Travis CI, CircleCI, and GitHub Actions, automating the testing process within the development pipeline.
- Diagnostic Tools for better debugging: BrowserStack provides comprehensive diagnostic capabilities, including detailed logs, screenshots, and video recordings of test sessions, aiding in the swift identification and resolution of issues.
- Testing on Real Devices: Beyond simulated environments, BrowserStack also supports testing on real devices and browsers on the cloud, offering more precise and real-world test outcomes.
- Customizable Test Execution: Users can tailor test executions to meet specific needs through BrowserStack’s user interface or APIs, enabling adaptable and controlled test runs.
Useful Resources for Selenium
Methods, Classes, and Commands
- Selenium Commands every Developer or Tester must know
- Selenium WebElement Commands
- Desired Capabilities in Selenium Webdriver
- Assert and Verify Methods in Selenium
- Understanding System setProperty in Selenium
- Select Class in Selenium : How to select a value in dropdown list?
- SendKeys in Selenium WebDriver
- getAttribute() method in Selenium: What, Why, and How to use
- How does Selenium isDisplayed() method work?
- findElement vs findElements in Selenium
- Types of Listeners in Selenium (with Code Examples)
- How to set Proxy in Firefox using Selenium WebDriver?
Configuration
- How to set up Selenium on Visual Studio
- How to configure Selenium in Eclipse
- Maven Dependency Management with Selenium
- How to Build and Execute Selenium Projects
XPath
- How to use XPath in Selenium?
- How to find element by XPath in Selenium with Example
- Top Chrome Extensions to find Xpath in Selenium
Locators and Selectors
- Locators in Selenium: A Detailed Guide
- CSS Selector in Selenium: Locate Elements with Examples
- How to Create Object Repository in Selenium
Waits in Selenium
- Wait Commands in Selenium C and C#
- Selenium Wait Commands: Implicit, Explicit, and Fluent Wait
- Understanding Selenium Timeouts
- Understanding ExpectedConditions in Selenium
- Understanding Role of Thread.sleep() in Selenium
Frameworks in Selenium
- Data Driven Framework in Selenium
- Implementing a Keyword Driven Framework for Selenium: A Practical Guide
- Hybrid Framework in Selenium
Miscellaneous
- How to create Selenium test cases
- How to set Proxy in Selenium?
- Difference between Selenium Standalone server and Selenium server
- Exception Handling in Selenium WebDriver
- How to use JavascriptExecutor in Selenium
- How to run your first Selenium test script
- Parallel Testing with Selenium
Best Practices, Tips and Tricks
- Top 5 Challenges Faced During Automation Selenium Testing
- 5 Selenium tricks to make your life easier
- 6 Things to avoid when writing Selenium Test Scripts
- Best Practices for Selenium Test Automation
- Why you should pay attention to flaky Selenium tests
- How to start with Selenium Debugging
- How to make your Selenium test cases run faster
- How to upgrade from Selenium 3 to Selenium 4
- Why you should move your testing to a Selenium Cloud?
Design Patterns in Selenium: Page Object Model and Page Factory
- Design Patterns in Selenium
- Page Object Model and Page Factory in Selenium
- Page Object Model and Page Factory in Selenium C#
- Page Object Model in Selenium and JavaScript
- Page Object Model and Page Factory in Selenium Python
Action Class
- How to handle Action class in Selenium
- How to perform Mouse Hover Action in Selenium
- Understanding Click Command in Selenium
- How to perform Double Click in Selenium?
- How to Drag and Drop in Selenium?
- How to Scroll Down or Up using Selenium Webdriver
- How To verify Tooltip Using Selenium
TestNG and Selenium
- Database Testing using Selenium and TestNG
- How to use DataProvider in Selenium and TestNG?
- All about TestNG Listeners in Selenium
- How to run parallel test cases in TestNG
- How to use TestNG Reporter Log in Selenium: Tutorial
- Prioritizing tests in TestNG with Selenium
JUnit and Selenium
- Understanding JUnit assertions for Selenium Testing with Examples
- How to run JUnit Parameterized Test in Selenium
- How to write JUnit test cases
- JUnit Testing Tutorial: JUnit in Java
- How to create JUnit Test Suite? (with Examples)
Use Cases
- Handling Login Popups in Selenium WebDriver and Java
- How to Launch Browser in Selenium
- How to handle Alerts and Popups in Selenium?
- How to get Selenium to wait for a page to load
- How to Find Element by Text in Selenium: Tutorial
- How to Read/Write Excel Data using Apache POI Selenium
- How to handle Captcha in Selenium
- How to handle multiple windows in Selenium?
- How to handle Multiple Tabs in Selenium
- How to find broken links in Selenium
- How to handle Cookies in Selenium WebDriver
- How to handle iFrame in Selenium
- How to handle Web Tables in Selenium
- How To Validate Text in PDF Files Using Selenium Automation
- Get Current URL in Selenium using Python: Tutorial
Types of Testing with Selenium
- Different Testing Levels supported by Selenium
- How to perform UI Testing with Selenium
- Regression Testing with Selenium: Tutorial
- UI Automation using Python and Selenium: Tutorial
- How to Run Visual Tests with Selenium: Tutorial
- How to perform ETL Automation using Selenium
- Cross Browser Testing in Selenium : Tutorial
Useful Resources for Playwright
- Playwright Automation Framework
- Playwright Java Tutorial
- Playwright Python tutorial
- Playwright Debugging
- End to End Testing using Playwright
- Visual Regression Testing Using Playwright
- Mastering End-to-End Testing with Playwright and Docker
- Page Object Model in Playwright
- Scroll to Element in Playwright
- Understanding Playwright Assertions
- Cross Browser Testing using Playwright
- Playwright Selectors
- Playwright and Cucumber Automation
Tool Comparisons:
- Playwright vs Puppeteer
- Playwright vs Cypress
- Playwright vs Selenium
- Cypress vs Selenium vs Playwright vs Puppeteer
Try Automation Testing on Cloud
Conclusion
No matter which test automation framework is chosen, cross browser testing is mandatory. It is the only way to guarantee that the website delivers seamless and consistent UX, irrespective of the device and browser used to access them.
- However, Emulators and simulators simply do not offer the real user conditions for testing software.
- Consider testing websites and apps on a real device cloud, preferably one that offers the latest devices, browsers, and OS versions.
- This applies to both manual testing and automation testing.
BrowserStack’s real device cloud provides 3500+ real browsers and devices for an instant, on-demand testing. - It also provides a cloud Selenium grid for automated testing, which can be accelerated by 10X with parallel testing.
- The cloud also provides integrations with popular CI/CD tools such as Jira, Jenkins, TeamCity, Travis CI, and more.
- Additionally, in-built debugging tools let testers identify and resolve bugs immediately.