Introduction to Playwright
Playwright is a browser automation framework for testing web applications across Chromium, Firefox and WebKit.
What is Playwright?
Playwright uses browser protocol connections to open pages, locate elements and validate application behavior. Its test runner provides fixtures, assertions, retries, parallel execution and reporting.
Installation and setup
npm init playwright@latest
npx playwright install
These commands create a Playwright project and download the required browsers.
First test
Create a test file named tests/home.spec.ts:
import {test, expect} from '@playwright/test';
test('homepage has the correct title', async ({page}) => {
await page.goto('https://example.test');
await expect(page).toHaveTitle(/Example/);
});
Run it:
npx playwright test tests/home.spec.ts
Playwright architecture
- The test runner executes test functions.
- Fixtures provide browser, page and context objects.
- Locators describe how to find elements.
- Assertions wait for expected conditions.
- Reports display passed, failed and skipped checks.
Real-world automation example
test('customer can open the checkout page', async ({page}) => {
await page.goto('/products');
await page.getByRole('button', {name: 'Buy now'}).click();
await expect(page).toHaveURL(/\/checkout$/);
await expect(page.getByRole('heading', {name: 'Checkout'})).toBeVisible();
});
Common mistakes
- Using fixed
waitForTimeout()calls instead of locators and assertions. - Testing against a production environment unintentionally.
- Reusing a single selector that depends on CSS implementation.
- Treating a successful page load as proof that a workflow works.
Summary
- Playwright combines browser control, test execution and assertions.
- Tests are written in JavaScript or TypeScript.
- Stable locators and web-first assertions improve reliability.
- Browser binaries must be installed in local and CI environments.