Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Playwright Debugging Tools

Playwright Debugging Tools

This chapter introduces Playwright's three major debugging tools: UI Mode, Trace Viewer, and command-line Debug mode.


UI Mode in Detail

UI Mode is Playwright's built-in graphical test interface, offering a one-stop experience for exploring, running, and debugging tests.

Starting UI Mode



npx playwright test --ui

Browsing the Test List

Once started, the left sidebar displays all test files.

You can expand each file and describe block to run, view, watch, or debug each test individually.

Watch Mode

After clicking the Watch button, Playwright monitors file changes and automatically re-runs affected tests whenever test code or source code is updated.

Timeline View

After a test runs, a timeline view appears at the top, using different colors to mark navigations and actions.

You can hover to view a snapshot of each step, and double-click to select a time range and view its detailed log.

DOM Snapshot

Click a step in the timeline and the DOM snapshot at that moment appears on the right.

You can see the actual state of the page before and after each action, quickly locating issues.

Test Filtering

UI Mode supports multiple filtering methods:


Deep Debugging with Trace Viewer

Trace Viewer is Playwright's record-and-replay tool, letting you inspect every step of a test like debugging a recording.

Configuring Trace Collection

Example

// File path: playwright.config.ts

export

default

defineConfig

(

{

  use

:

{

   

// Collect Trace only on failed retries

    trace

:

'on-first-retry'

,

   

// Possible values:

   

// 'off' —— do not collect

   

// 'on' —— collect every time

   

// 'on-first-retry' —— collect only on the first retry (recommended)

   

// 'retain-on-failure' —— retain Trace only on failure

 

}

,

}

)

;

Opening the Trace



# Open locally

npx playwright show-trace path/to/trace.zip

You can also upload it to trace.playwright.dev to view it online.

Information Contained in a Trace

Content Description
DOM Snapshots Complete page DOM snapshots before and after each step
Network requests All HTTP requests and responses (URL, status code, headers, body)
Console Logs All messages output by the browser console
Screenshot A page screenshot for each step
Action Log Call parameters and execution time of all Playwright actions
Source Code The test source location corresponding to each action

Using Trace to Locate Failures

Find the failed step in the Trace Viewer timeline, examine the page's DOM state and network requests before and after that step, and identify the cause of failure.

Trace is the most powerful debugging tool. When a test fails on CI but you cannot reproduce it locally, downloading the Trace file generated by CI lets you analyze the failure offline.


--debug Mode and page.pause()

--debug Command-Line Mode



npx playwright test --debug

Function:

page.pause() Pauses Execution

Example

// Set a breakpoint in the code

test

(

'Test for Debugging'

,

async

(

{

page

}

)

=>

{

  await page.

goto

(

'https://www.runoob.com/'

)

;

 

// Pause execution and open the Inspector

  await page.

pause

(

)

;

 

// In the Inspector you can step through subsequent actions

  await page.

getByText

(

'RUNOOB Tutorial'

)

.

click

(

)

;

}

)

;

Use page.pause() After running the test with this flag, Playwright automatically opens the Inspector window so you can inspect the page state step by step.


Choosing Among the Three Debugging Methods

Method Applicable scenarios
UI Mode Daily development, browsing the test list, quickly checking test status
Trace Viewer Locating CI failures, analyzing every step of complex tests
--debug / page.pause() Developing new tests, deeply debugging specific steps