Prerequisites
Make sure you have installed Playwright before proceeding.
Create your first test
1
Create a test file
Create a new file called This test navigates to the Playwright homepage and verifies the page title contains “Playwright”.
example.spec.ts in your tests/ directory: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
- Headless mode
- Headed mode
- UI mode
- Debug mode
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: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
- 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
Use Codegen to generate tests
Use Codegen to generate tests
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
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
Use VS Code extension
Install the Playwright Test extension for VS Code to run tests directly from the editor with debugging support.
