Debugging Guide
Relevant source files
The following files were used as context for generating this wiki page:
- CONTRIBUTING.md
- README.md
- tests/playwright-test/watch.spec.ts
- tests/playwright-test/reporter-html.spec.ts
- tests/playwright-test/reporter-markdown.spec.ts
- tests/playwright-test/only-changed.spec.ts
- tests/playwright-test/ui-mode-test-source.spec.ts
- packages/web/src/shared/prompts.ts
- tests/playwright-test/reporter-blob.spec.ts
- FILING_ISSUES.md
- tests/playwright-test/test-server.spec.ts
Playwright provides a comprehensive suite of tools to assist in debugging tests, catering to different phases of the development and testing lifecycle. These tools range from interactive, real-time debugging sessions to powerful post-mortem analysis of test runs. The primary goal is to help developers quickly identify and resolve issues, from flaky tests to application bugs.
This guide covers the main debugging tools and workflows, including interactive tools like the Playwright Inspector and UI Mode, automated aids like Watch Mode, and analytical tools such as the Trace Viewer and various reporters. For official documentation, refer to the Playwright Debugging Tools and the guide on Running and Debugging Tests.
Sources: README.md, CONTRIBUTING.md, FILING_ISSUES.md
Core Debugging Tools
Playwright's core debugging tools are designed for interactive use, allowing developers to step through test execution, inspect the state of the web page, and generate test code.
Sources: README.md
Playwright Inspector
The Inspector is a GUI tool that allows you to step through Playwright API calls, see their results, and explore selectors. It's a powerful way to debug your tests, as you can see exactly what Playwright is doing at each step. Key features include:
- Inspecting the page and generating selectors.
- Stepping through test execution.
- Viewing click points and execution logs.
Sources: README.md
Trace Viewer
The Trace Viewer provides a way to explore recorded traces of a test run after it has completed. This is invaluable for diagnosing failures, especially in CI environments. It captures a wealth of information, including:
- A screencast of the test execution.
- Live DOM snapshots for each action.
- An action explorer to navigate through test steps.
- The test source code.
- Network requests and console logs.
Traces can be configured to be captured on failure and are often embedded directly in the HTML Reporter.
Sources: README.md, tests/playwright-test/reporter-html.spec.ts:413-433
UI Mode
UI Mode provides an interactive experience for running and debugging tests. It allows you to see the test source code alongside the test tree. When a test is selected or running, the corresponding line in the source code is highlighted. If there are errors, such as syntax errors or duplicate test titles, they are displayed directly in the source view, pointing to the exact location of the issue.
This diagram shows how UI Mode displays test source and errors.
Codegen
Codegen helps kickstart the test creation process by recording user actions and generating the corresponding Playwright script in any of the supported languages. This is useful for quickly creating a baseline test that can then be refined.
Sources: README.md
Watch Mode
Watch mode is designed to improve the developer feedback loop by automatically re-running tests when files are changed. It watches the test files and their dependencies for modifications.
Sources: tests/playwright-test/watch.spec.ts
Features
- Automatic Re-runs: When a test file or one of its imported modules is saved, watch mode automatically triggers a test run for the affected files.
- Interactive CLI: Provides a set of keyboard shortcuts to control the test execution without restarting the process.
- Dependency Tracking: Intelligently tracks the dependency graph for both CJS and ESM modules to determine which tests to re-run.
This diagram illustrates the workflow of using watch mode.
Keyboard Shortcuts
The interactive watch mode provides several commands to manage the test run.
| Key | Action |
|---|---|
Enter | Rerun all tests. |
f | Rerun only the tests that failed in the last run. |
p | Filter tests by a filename pattern (regex). |
t | Filter tests by a test title pattern (regex). |
c | Choose projects to run. |
r | Rerun tests for the files that were changed. |
h | Show the help menu. |
q | Quit watch mode. |
Sources: tests/playwright-test/watch.spec.ts:188-198, tests/playwright-test/watch.spec.ts:212-226, tests/playwright-test/watch.spec.ts:228-240, tests/playwright-test/watch.spec.ts:257-271, tests/playwright-test/watch.spec.ts:183-186
Limitations
- Watch mode is not compatible with the
--only-changedor--uiflags.
Sources: tests/playwright-test/only-changed.spec.ts:153-157, tests/playwright-test/watch.spec.ts
Reporting and Analysis
Post-mortem debugging is a critical part of the testing process, and Playwright's reporters are designed to provide rich, actionable information after a test run has completed.
HTML Reporter
The HTML reporter generates a self-contained website that provides a comprehensive overview of the test run. It is the primary tool for analyzing test failures.
Key features include:
- Test Results: A filterable list of all tests, showing their status (passed, failed, flaky, skipped).
- Detailed View: Clicking on a test reveals detailed information, including steps, hooks, errors, and attachments.
- Attachments: Screenshots, videos, and traces are embedded or linked directly in the report.
- Image Diffs: For visual regression tests, it displays expected, actual, and diff images.
- Error Highlighting: Code snippets for failed assertions are shown with syntax highlighting, making it easy to spot the problem.
- Search and Filter: Tests can be filtered by status, tags, or text search.
This sequence diagram shows the process from running tests to viewing the HTML report.
Markdown Reporter
The Markdown reporter provides a concise summary of the test run in a Markdown file. It's useful for quick overviews in environments like pull request comments. It lists failed and flaky tests prominently.
Example output:
**2 failed**
:x: dir1/a.test.js:6 › failing 1
:x: dir2/b.test.js:6 › failing 2
<details>
<summary><b>2 flaky</b></summary>
:warning: c.test.js:6 › flaky 2 <br/>
:warning: dir1/a.test.js:9 › flaky 1 <br/>
</details>
**3 passed, 3 skipped**
Sources: tests/playwright-test/reporter-markdown.spec.ts:40-52
Blob Reporter and Merging
For large test suites that are run in parallel across multiple machines (sharding), the blob reporter is used. Each shard produces a .zip file containing the test results. These blobs can then be aggregated into a single report using the playwright merge-reports command. This is essential for getting a unified view of a distributed test run.
This diagram shows the blob report and merge workflow.
Advanced Debugging Techniques
Running Only Changed Tests
The --only-changed flag provides an efficient way to run only the tests that have been affected by local file changes. It uses git to find files that are untracked or have been modified since a specific commit (e.g., HEAD~1). It also analyzes the dependency graph to include tests that import changed files.
This feature is useful for developers who want to validate their changes before committing, without running the entire test suite. It is not compatible with watch mode or UI mode.
Sources: tests/playwright-test/only-changed.spec.ts:68-80, tests/playwright-test/only-changed.spec.ts:123-143, tests/playwright-test/only-changed.spec.ts:153-162
AI-Assisted Debugging
The HTML reporter includes a "Copy prompt" feature that generates a detailed prompt for use with Large Language Models (LLMs) to help diagnose and fix test failures.
The generated prompt includes:
- Instructions to the AI.
- Test information (file, title).
stdoutandstderrfrom the test run.- Detailed error messages and stack traces.
- The relevant test source code.
- The
git diffof local changes, if available.
This flow diagram shows how the AI prompt is generated.
Verbose Logging
For deep-level debugging, Playwright uses the debug library for logging. You can enable specific logs by setting the DEBUG environment variable. For example, to see browser-related logs:
DEBUG=pw:browser npx playwright test
This is particularly useful when debugging issues with custom browser builds or complex browser interactions.
Sources: CONTRIBUTING.md