What is playwright?
Playwright is a Node.js library developed by Microsoft that allows developers to automate browser actions for testing and scraping purposes. It supports multiple browsers, including Chromium, Firefox, and WebKit, and provides a high-level API to control headless or full browsers over the DevTools Protocol.
What are playwright's main functionalities?
Browser Automation
Automate browser actions such as opening a page, clicking elements, and navigating through websites.
const { chromium } = require('playwright');
(async () => {
const browser = await chromium.launch();
const page = await browser.newPage();
await page.goto('https://example.com');
// other actions...
await browser.close();
})();
Web Scraping
Extract data from web pages by selecting elements and retrieving their content.
const { firefox } = require('playwright');
(async () => {
const browser = await firefox.launch();
const page = await browser.newPage();
await page.goto('https://example.com');
const data = await page.textContent('.some-element');
console.log(data);
await browser.close();
})();
Automated Testing
Write automated tests for web applications, including assertions to validate the behavior of the application.
const { webkit } = require('playwright');
const { expect } = require('@playwright/test');
(async () => {
const browser = await webkit.launch();
const page = await browser.newPage();
await page.goto('https://example.com');
const title = await page.title();
expect(title).toBe('Example Domain');
await browser.close();
})();
Cross-Browser Testing
Test web applications across multiple browsers to ensure compatibility and consistent behavior.
const { chromium, firefox, webkit } = require('playwright');
(async () => {
for (const browserType of [chromium, firefox, webkit]) {
const browser = await browserType.launch();
const page = await browser.newPage();
await page.goto('https://example.com');
// Perform tests across different browsers
await browser.close();
}
})();
Mobile Emulation
Emulate mobile devices to test responsive designs and touch interactions.
const { chromium, devices } = require('playwright');
const iPhone11 = devices['iPhone 11 Pro'];
(async () => {
const browser = await chromium.launch();
const context = await browser.newContext({
...iPhone11
});
const page = await context.newPage();
await page.goto('https://example.com');
// Emulate mobile device actions and layout
await browser.close();
})();
Other packages similar to playwright
selenium-webdriver
Selenium WebDriver is one of the most well-known browser automation tools. It requires separate drivers for each browser and is generally slower than Playwright due to its reliance on the WebDriver protocol, which is more standardized but less performant.
puppeteer
Puppeteer is another Node.js library that provides a high-level API to control Chrome or Chromium over the DevTools Protocol. It is similar to Playwright but was developed by the Chrome DevTools team and initially focused on Chrome, whereas Playwright supports multiple browsers out of the box.
cypress
Cypress is a front-end testing tool built for the modern web. It is both a library for writing tests and a test runner that executes the tests. Cypress is known for its rich interactive test runner interface but does not support as many browsers as Playwright and is primarily focused on testing rather than general browser automation.
nightwatch
Nightwatch.js is an automated testing framework for web applications and websites, written in Node.js and using the W3C WebDriver API. It is designed to simplify the process of writing end-to-end tests. Nightwatch is less feature-rich compared to Playwright and is more focused on the testing aspect.
🎭 Playwright
Playwright is a Node library to automate Chromium, Firefox and WebKit with a single API. Playwright is built to enable cross-browser web automation that is ever-green, capable, reliable and fast.
| Linux | macOS | Windows |
---|
Chromium 84.0.4135.0 | :white_check_mark: | :white_check_mark: | :white_check_mark: |
WebKit 13.0.4 | :white_check_mark: | :white_check_mark: | :white_check_mark: |
Firefox 76.0b5 | :white_check_mark: | :white_check_mark: | :white_check_mark: |
Headless execution is supported for all the browsers on all platforms.
Usage
npm i playwright
This installs Playwright and browser binaries for Chromium, Firefox and WebKit. Once installed, you can require
Playwright in a Node.js script and automate web browser interactions.
Capabilities
Playwright is built to automate the broad and growing set of web browser capabilities used by Single Page Apps and Progressive Web Apps.
- Scenarios that span multiple page, domains and iframes
- Auto-wait for elements to be ready before executing actions (like click, fill)
- Intercept network activity for stubbing and mocking network requests
- Emulate mobile devices, geolocation, permissions
- Native input events for mouse and keyboard
- Upload and download files
Examples
Page screenshot
This code snippet navigates to whatsmyuseragent.org in Chromium, Firefox and WebKit, and saves 3 screenshots.
const playwright = require('playwright');
(async () => {
for (const browserType of ['chromium', 'firefox', 'webkit']) {
const browser = await playwright[browserType].launch();
const context = await browser.newContext();
const page = await context.newPage();
await page.goto('http://whatsmyuseragent.org/');
await page.screenshot({ path: `example-${browserType}.png` });
await browser.close();
}
})();
Mobile and geolocation
This snippet emulates Mobile Safari on a device at a given geolocation, navigates to maps.google.com, performs action and takes a screenshot.
const { webkit, devices } = require('playwright');
const iPhone11 = devices['iPhone 11 Pro'];
(async () => {
const browser = await webkit.launch();
const context = await browser.newContext({
...iPhone11,
geolocation: { longitude: 12.492507, latitude: 41.889938 },
permissions: ['geolocation']
});
const page = await context.newPage();
await page.goto('https://maps.google.com');
await page.click('text="Your location"');
await page.waitForRequest(/.*preview\/pwa/);
await page.screenshot({ path: 'colosseum-iphone.png' });
await browser.close();
})();
Evaluate in browser context
This code snippet navigates to example.com in Firefox, and executes a script in the page context.
const { firefox } = require('playwright');
(async () => {
const browser = await firefox.launch();
const context = await browser.newContext();
const page = await context.newPage();
await page.goto('https://www.example.com/');
const dimensions = await page.evaluate(() => {
return {
width: document.documentElement.clientWidth,
height: document.documentElement.clientHeight,
deviceScaleFactor: window.devicePixelRatio
}
})
console.log(dimensions);
await browser.close();
})();
Intercept network requests
This code snippet sets up request routing for a WebKit page to log all network requests.
const { webkit } = require('playwright');
(async () => {
const browser = await webkit.launch();
const context = await browser.newContext();
const page = await context.newPage();
page.route('**', route => {
console.log(route.request().url());
route.continue();
});
await page.goto('http://todomvc.com');
await browser.close();
})();
Resources