Skip to main content
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.

Prerequisites

Make sure you have installed Playwright before proceeding.

Create your first test

1

Create a test file

Create a new file called example.spec.ts in your tests/ directory:
This test navigates to the Playwright homepage and verifies the page title contains “Playwright”.
2

Run the test

Execute your test using the Playwright Test runner:
By default, tests run in headless mode across all configured browsers.
3

View results

After the test completes, view the HTML report:
The report shows test results, execution time, and any failures.

Running tests in different modes

Default mode - browsers run without a UI:

Real-world examples

Here are practical examples from the Playwright repository demonstrating common testing patterns.

Example 1: Taking screenshots

Navigate to a page and capture a screenshot:

Example 2: Interacting with forms

Fill out a form and submit it:

Example 3: Mobile emulation

Test mobile viewports with device emulation:

Example 4: Evaluating JavaScript

Execute code in the browser context:

Example 5: Network interception

Intercept and log network requests:

Running specific tests

Understanding test anatomy

Every Playwright test follows this structure:
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.

Common CLI commands

Run all tests

Run with UI

Generate code

Show report

Debugging tests

1

Add a breakpoint

Use await page.pause() to pause execution:
2

Run in debug mode

This opens the Playwright Inspector with:
  • Ability to step through test actions
  • Pick locator tool
  • Console for running commands
  • Source code view
The Playwright Inspector allows you to step through your test, explore locators, and execute commands in real-time.

Next steps

Writing tests

Learn best practices for writing tests

Configuration

Configure test runner options

Locators

Master element selection strategies

Assertions

Understand web-first assertions

Tips for getting started

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.
When writing tests, use --headed mode to see what’s happening. Switch to headless mode for CI/CD.
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.