Skip to main content

测试调试

🌐 Test Debugging

使用 CLI 以交互方式调试 Playwright 测试失败。

🌐 Use the CLI to debug Playwright test failures interactively.

正在连接到已暂停的测试

🌐 Connecting to a paused test

使用调试标志开始测试:

🌐 Start a test with the debug flag:

npx playwright test --debug=cli

测试在开始时暂停并输出会话名称。连接到它:

🌐 The test pauses at the start and outputs a session name. Connect to it:

playwright-cli attach <session-name>

然后探索页面状态:

🌐 Then explore the page state:

playwright-cli snapshot                 # see current state
playwright-cli console error # check for errors
playwright-cli eval "() => document.title"
playwright-cli screenshot --filename=debug-state.png

并控制执行:

🌐 And control execution:

playwright-cli resume                   # continue
playwright-cli step-over # step to next action
playwright-cli pause-at test.ts:42 # set a breakpoint

工作流程:调查不稳定的测试

🌐 Workflow: investigating a flaky test

# 1. Run the failing test
npx playwright test tests/checkout.spec.ts --debug=cli

# 2. Connect
playwright-cli attach playwright-test-1

# 3. Record a trace
playwright-cli tracing-start

# 4. Step through
playwright-cli resume
playwright-cli snapshot
playwright-cli console
playwright-cli network

# 5. Investigate at the failing step
playwright-cli screenshot --filename=before-failure.png
playwright-cli eval "() => document.querySelector('.spinner')?.style.display"

# 6. Save trace
playwright-cli tracing-stop