Introduction
Playwright is a modern open-source automation library from Microsoft used for end-to-end testing of web applications. It supports multiple browsers (Chromium, Firefox, and WebKit) and offers a powerful, fast, and reliable test automation experience.
Why Choose Playwright?
- Cross-browser testing: Easily test across Chrome, Safari, and Firefox.
- Auto-waiting: Handles timing issues by waiting for elements to be ready.
- Headless and headed mode: Great for CI/CD and debugging.
- Powerful selectors: Supports text, role, CSS, XPath, and more.
- Network mocking: Simulate different backend responses with ease.
Installation
Use the following command to install Playwright and its test runner:
npm init playwright@latest
This sets up Playwright with all required dependencies, browsers, and example tests.
Writing Your First Test
import { test, expect } from '@playwright/test';
test('homepage has title and links to docs',
async ({ page }) => {
await page.goto('https://playwright.dev/');
await expect(page).toHaveTitle(/Playwright/);
await page.click('text=Get started');
await expect(page).toHaveURL(/.*docs/);
});
This simple test visits the Playwright homepage, checks the title, clicks on “Get Started” and verifies the resulting URL.
Running Tests
Run tests using the command:
npx playwright test
It will execute all test files matching *.spec.ts
or *.test.ts
in the tests
directory.
Using UI Mode
Playwright provides a UI mode to debug tests interactively. Start it using:
npx playwright test --ui
This mode lets you run, pause, and inspect tests visually—perfect for debugging flaky tests.
Conclusion
Playwright makes end-to-end testing simple yet powerful. With features like auto-waiting, network mocking, and UI debugging, it’s quickly becoming a favorite for modern web app testing. If you're starting fresh or switching from Selenium, Playwright is worth exploring.