调试测试
Playwright Inspector
Playwright Inspector 是一个 GUI 工具,可以帮助您调试 Playwright 测试。它允许您逐步执行测试、实时编辑定位器、选取定位器并查看可操作性日志。

在调试模式下运行
设置 PWDEBUG
环境变量以在调试模式下运行 Playwright 测试。这将 Playwright 配置为调试并打开 Inspector。当设置 PWDEBUG=1
时,会配置其他有用的默认值
- 浏览器以有头模式启动
- 默认超时设置为 0 (= 无超时)
配置源位置
要告诉 Playwright 在哪里查找您正在调试的源代码,请通过 PLAYWRIGHT_JAVA_SRC
环境变量传递源目录列表。列表中的路径在 macOS 和 Linux 上应以 : 分隔,在 Windows 上应以 ; 分隔。
- Bash
- PowerShell
- Batch
# Source directories in the list are separated by : on macos and linux and by ; on win.
PWDEBUG=1 PLAYWRIGHT_JAVA_SRC=<java source dirs> mvn test
# Source directories in the list are separated by : on macos and linux and by ; on win.
$env:PLAYWRIGHT_JAVA_SRC="<java source dirs>"
$env:PWDEBUG=1
mvn test
# Source directories in the list are separated by : on macos and linux and by ; on win.
set PLAYWRIGHT_JAVA_SRC=<java source dirs>
set PWDEBUG=1
mvn test
逐步执行测试
您可以使用 Inspector 顶部的工具栏播放、暂停或逐步执行测试的每个操作。您可以在测试代码中看到当前操作被突出显示,并在浏览器窗口中看到匹配的元素被突出显示。

从特定断点运行测试
为了加快调试过程,您可以在测试中添加 Page.pause() 方法。这样,您就不必逐步执行测试的每个操作,即可到达要调试的点。
page.pause();
添加 page.pause()
调用后,在调试模式下运行测试。单击 Inspector 中的“恢复”按钮将运行测试,并且仅在 page.pause()
处停止。

实时编辑定位器
在调试模式下运行时,您可以实时编辑定位器。“选取定位器”按钮旁边有一个字段,显示测试暂停时所处的定位器。您可以直接在“选取定位器”字段中编辑此定位器,并且匹配的元素将在浏览器窗口中突出显示。

选取定位器
调试时,您可能需要选择更具弹性的定位器。您可以通过单击“选取定位器”按钮并将鼠标悬停在浏览器窗口中的任何元素上来执行此操作。将鼠标悬停在元素上时,您将在下方看到突出显示定位此元素所需的代码。单击浏览器中的元素会将定位器添加到字段中,然后您可以在其中调整定位器或将其复制到代码中。

Playwright 将查看您的页面并找出最佳定位器,优先考虑 role、text 和 test id 定位器。如果 Playwright 找到与定位器匹配的多个元素,它将改进定位器,使其具有弹性并唯一标识目标元素,因此您不必担心由于定位器而导致测试失败。
可操作性日志
当 Playwright 在单击操作处暂停时,它已经执行了可在日志中找到的可操作性检查。这可以帮助您了解测试期间发生了什么以及 Playwright 执行或尝试执行的操作。日志会告诉您元素是否可见、启用和稳定,定位器是否解析为元素、滚动到视图等等。如果无法实现可操作性,它将显示操作为挂起。

追踪查看器
Playwright 追踪查看器是一个 GUI 工具,可让您浏览记录的 Playwright 测试追踪。您可以在左侧来回查看每个操作,并直观地查看操作期间发生的情况。在屏幕中间,您可以看到操作的 DOM 快照。在右侧,您可以看到操作详细信息,例如时间、参数、返回值和日志。您还可以浏览控制台消息、网络请求和源代码。
要了解有关如何记录追踪和使用追踪查看器的更多信息,请查看追踪查看器指南。
浏览器开发者工具
当在调试模式下使用 PWDEBUG=console
运行,playwright
对象在开发者工具控制台中可用。开发者工具可以帮助您:
- 检查 DOM 树并查找元素选择器
- 查看执行期间的控制台日志 (或了解如何通过 API 读取日志)
- 检查网络活动和其他开发者工具功能
这还将 Playwright 的默认超时设置为 0 (= 无超时)。

要使用浏览器开发者工具调试测试,请首先在测试中设置断点,以使用 Page.pause() 方法暂停执行。
page.pause();
在测试中设置断点后,您可以使用 PWDEBUG=console
运行测试。
- Bash
- PowerShell
- Batch
# Source directories in the list are separated by : on macos and linux and by ; on win.
PWDEBUG=console PLAYWRIGHT_JAVA_SRC=<java source dirs> mvn test
# Source directories in the list are separated by : on macos and linux and by ; on win.
$env:PLAYWRIGHT_JAVA_SRC="<java source dirs>"
$env:PWDEBUG=console
mvn test
# Source directories in the list are separated by : on macos and linux and by ; on win.
set PLAYWRIGHT_JAVA_SRC=<java source dirs>
set PWDEBUG=console
mvn test
Playwright 启动浏览器窗口后,您可以打开开发者工具。 playwright
对象将在控制台面板中可用。
playwright.$(selector)
查询 Playwright 选择器,使用实际的 Playwright 查询引擎,例如
playwright.$('.auth-form >> text=Log in');
<button>Log in</button>
playwright.$$(selector)
与 playwright.$
相同,但返回所有匹配的元素。
playwright.$$('li >> text=John')
[<li>, <li>, <li>, <li>]
playwright.inspect(selector)
在元素面板中显示元素。
playwright.inspect('text=Log in')
playwright.locator(selector)
创建定位器并查询匹配的元素,例如
playwright.locator('.auth-form', { hasText: 'Log in' });
Locator ()
- element: button
- elements: [button]
playwright.selector(element)
为给定元素生成选择器。例如,在元素面板中选择一个元素并传递 $0
playwright.selector($0)
"div[id="glow-ingress-block"] >> text=/.*Hello.*/"
详细 API 日志
Playwright 支持使用 DEBUG
环境变量进行详细日志记录。
- Bash
- PowerShell
- Batch
DEBUG=pw:api mvn test
$env:DEBUG="pw:api"
mvn test
set DEBUG=pw:api
mvn test
对于 WebKit:在执行期间启动 WebKit Inspector 将阻止 Playwright 脚本继续执行,并将重置预配置的用户代理和设备模拟。
有头模式
Playwright 默认以无头模式运行浏览器。要更改此行为,请使用 headless: false
作为启动选项。
您还可以使用 setSlowMo 选项来减慢执行速度(每个操作 N 毫秒),并在调试时进行跟踪。
// Chromium, Firefox, or WebKit
chromium.launch(new BrowserType.LaunchOptions()
.setHeadless(false)
.setSlowMo(100));