Create and run your first Playwright test in minutes
This guide walks you through creating and running your first Playwright test. By the end, you’ll understand the basics of writing tests and interacting with web pages.
import { test, expect } from '@playwright/test';test('should add single todo', async ({ page }) => { await page.goto('https://demo.playwright.dev/todomvc'); // Type into the input field await page.getByRole('textbox', { name: 'What needs to be done?' }) .fill('Buy groceries'); // Verify the text appears await expect(page.getByRole('textbox', { name: 'What needs to be done?' })) .toHaveValue('Buy groceries'); // Submit by pressing Enter await page.keyboard.press('Enter'); // Verify the todo appears in the list await expect(page.getByText('Buy groceries')).toBeVisible(); await expect(page.getByText('1 item left')).toBeVisible();});
Auto-waiting: Playwright automatically waits for elements to be actionable before performing actions. This eliminates the need for manual timeouts and makes tests more reliable.
Run npx playwright codegen example.com to record browser interactions and generate test code automatically. This is a great way to learn Playwright’s API.
Start with headed mode
When writing tests, use --headed mode to see what’s happening. Switch to headless mode for CI/CD.
Use VS Code extension
Install the Playwright Test extension for VS Code to run tests directly from the editor with debugging support.
Common pitfall: Don’t forget await before Playwright actions. All Playwright methods are asynchronous and return Promises.