Playwright—a Node.js library developed by Microsoft— is a tool for automating web and end-to-end testing.
Playwright has many useful features that simplify automated web testing, such as:
Cross-browser testing: It supports testing for Chromium, Firefox, and Webkit.
Cross platform: It has support for all operating systems, i.e., Windows, Linux, and macOS.
Cross-language: It has support for multiple languages like Java, .NET C#, Python, TypeScript, and JavaScript.
Auto-waiting: It auto-waits for some checks to pass before performing the test.
Playwright is better than Selenium because of the following features:
Test generation and saving: It can produce tests based on recorded actions and save them in the required language.
Faster than Selenium: Tests run faster on Playwright than Selenium because it does all the tests over a web socket connection rather than on HTTP requests.
Investigating tests: It has a trace viewer that can record all information related to tests. We can also find the reason for failing tests through it.
Complete isolation of tests: It provides isolation of tests by providing a new browser mock setup for each test.
Playwright, Selenium, and other web automation tools have advantages and disadvantages. If speed is more important for us in the automation process, then we should go for Playwright. If we need multiple browsers and language support in automation, then Selenium is better than Playwright. Moreover, we should choose Playwright for automation if we need video recording and detailed reporting in our tests. Overall, the choice depends on our requirements and needs.
Let's see an example of how this library works in Node.js. We have some JavaScript code here that will open up the educative.io/answers page and look for the first <h1>
heading from that page.
const { chromium } = require('playwright'); // Import the Playwright library for browser automation (async () => { // Launch a Chromium browser (can be any of chromium, firefox, webkit) const browser = await chromium.launch({ headless: true }); // Launch Chromium in headless mode (no visible window) // Create a new browser page const page = await browser.newPage(); // Navigate to Educative await page.goto('https://www.educative.io/answers/introduction-to-asynchronous-calls-in-javascript'); // Example: Get the text of the first <h1> tag const first = await page.$eval('h1', heading => heading.textContent); // Find the first <h1> tag and get its text content console.log(first); // Print the text of the first <h1> tag // Close the browser await browser.close(); })();
Lines 1–11: The playwright
library is imported, providing tools for browser automation. An asynchronous function is defined. Inside, it launches a headless
Line 14: We wait for the page to fully load before proceeding. Then, we find the first <h1>
(paragraph) element on the results page and extract its text content.
Lines 15–18: The extracted text content from the first <h1>
tag is printed to the console. The browser is closed, releasing resources.
Note: This is a simple example, but we can see how powerful extracting elements from websites, and then matching them to some tests could be.
Playwright is a powerful and versatile web automation tool offering cross-browser support, speed improvements, and valuable debugging features. While both Playwright and Selenium have their strengths, Playwright shines when testing speed and rich reporting are priorities. If you're looking for a modern, developer-friendly way to automate your web tests, Playwright is definitely worth serious consideration.
Free Resources