Overview
Playwright provides comprehensive debugging tools to help you understand test failures and optimize test execution. From interactive inspectors to detailed trace viewers, you have multiple options for debugging at different stages.Playwright Inspector
The Playwright Inspector is a GUI tool that lets you step through tests, live edit locators, and inspect actionability logs.Debug Mode
Run tests in debug mode to open the Inspector:When
--debug flag is used, tests run in headed mode and default timeout is set to 0 (no timeout).Inspector Features
1
Step through tests
Use the step controls to execute your test line by line:
- Step Over: Execute current line and move to next
- Step Into: Enter function calls
- Step Out: Complete current function
- Continue: Run until next breakpoint
2
Pick locators
Click the “Pick Locator” button, then click elements in the browser to generate locators automatically.
3
View actionability logs
See why actions succeed or fail with detailed logs showing:
- Element visibility
- Element position
- Element state
4
Edit locators live
Modify locators in the inspector and see them highlighted in the browser in real-time.
VS Code Debugger
Using the VS Code Extension
The Playwright VS Code extension provides the best debugging experience:1
Install the extension
Install the official Playwright Test for VSCode extension.
2
Set breakpoints
Click next to line numbers to set breakpoints (red dots appear).
3
Run in debug mode
Right-click the line number next to a test and select “Debug Test”.
4
Use debug controls
Use VS Code’s debug toolbar to step through code, inspect variables, and evaluate expressions.
Debug Configuration
Create a.vscode/launch.json for custom debug configurations:
Trace Viewer
Enable Trace Recording
Configure trace recording inplaywright.config.ts:
View Traces
Open trace files after test execution:Trace Viewer Features
The trace viewer provides comprehensive debugging information:- Action timeline: See every action taken during the test
- Screenshots: Visual snapshots at each step
- DOM snapshots: Explore the DOM state at any point
- Network activity: Inspect all network requests and responses
- Console logs: View console output
- Source code: See which line of code triggered each action
- Call stack: Understand the execution flow
Console Output
Page Console Logs
Capture browser console messages:Debug with console.log
Verbose Logging
Enable detailed Playwright logs:Pause Execution
Using page.pause()
Pause test execution at any point:- Inspect the current page state
- Try out locators
- Step through remaining test code
- Resume execution
Conditional Breakpoints
Headed Mode
Run tests with visible browser for visual debugging:playwright.config.ts:
Screenshots and Videos
Debug with Screenshots
Automatic Failure Screenshots
Debugging Best Practices
- Start with traces: The trace viewer provides the most comprehensive debugging information
- Use VS Code extension: For local development, the VS Code extension offers the best DX
- Add strategic page.pause(): Insert pauses before complex operations
- Enable verbose logging: Use DEBUG environment variable for detailed logs
- Slow down execution: Use
slowMooption to watch actions in real-time - Capture screenshots: Take screenshots at key points in your test
- Monitor network: Check network tab in trace viewer for API issues
Common Debugging Scenarios
Element Not Found
Timeout Issues
Flaky Tests
Debugging in CI
Upload Artifacts
Configure CI to upload debugging artifacts:View CI Traces
- Download the
playwright-reportartifact from your CI run - Extract the archive
- Run:
npx playwright show-report playwright-report/
Troubleshooting
Inspector not opening
Verify you’re using the--debug flag:
VS Code extension not working
Reload the VS Code window and ensure tests are detected:- Open Command Palette (Cmd+Shift+P)
- Run “Playwright: Reload”
Traces not being recorded
Verify trace configuration inplaywright.config.ts:
