Playwright is an open-source end-to-end testing and browser automation framework developed by Microsoft. It allows you to automate actions in web browsers like Chromium (Chrome, Edge), WebKit (Safari), and Firefox with a single API.
It’s commonly used for:
- UI Testing – verifying that web applications work as expected.
- Web Scraping – extracting data from websites.
- Automation – filling forms, clicking buttons, downloading/uploading files, etc.
Playwright is similar to Selenium, but faster and more modern, supporting features like:
- Auto-waiting for elements
- Handling multiple browser contexts
- Cross-browser support
- Headless & headed mode
Example: Using Playwright with JavaScript/Node.js
First, install Playwright:
npm init -y
npm install playwright
Script: Open a webpage and take a screenshot
const { chromium } = require('playwright');
(async () => {
// Launch browser
const browser = await chromium.launch({ headless: true });
const context = await browser.newContext();
const page = await context.newPage();
// Navigate to website
await page.goto('https://example.com');
// Take screenshot
await page.screenshot({ path: 'example.png' });
// Close browser
await browser.close();
})();
✅ This will open https://example.com
in a headless Chromium browser and save a screenshot as example.png
.
Example: Playwright Test (with built-in test runner)
Playwright also provides a test runner (@playwright/test
) for writing structured tests.
Install:
npm install -D @playwright/test
Test file example.spec.js
:
import { test, expect } from '@playwright/test';
test('homepage has Playwright in title', async ({ page }) => {
await page.goto('https://playwright.dev/');
await expect(page).toHaveTitle(/Playwright/);
});
Run test:
npx playwright test