# Playwright

> For the complete documentation index, see [llms.txt](https://www.jetbrains.com/zh-cn/help/go/llms.txt).

> **Note:**
> 在 GoLand 中对 Playwright 的支持由 [Test Automation](https://plugins.jetbrains.com/plugin/20175-test-automation) 插件提供，本节中描述的大多数功能均依赖于该插件。 不过，基本功能如运行和调试 Playwright 测试在没有插件的情况下也能正常工作。

![](https://resources.jetbrains.com/help/img/idea/2026.3/javascript-impl.icons.playwright.svg) [Playwright](https://playwright.dev/) 是一个开源的测试自动化框架。 提供用于自动化 Web 浏览器交互的高级 API。

## 创建一个新的 Playwright 项目

Procedure:

1. 在主菜单中，进入  `文件 | 新建 | 项目` .

或者， 如果您在   欢迎  屏幕上， 点击   新项目  。

2. 在左侧列表中选择   Playwright  。

![创建一个新的 Playwright 项目](https://resources.jetbrains.com/help/img/idea/2026.3/go_playwright_new_project.png)

3. 请命名新项目并在必要时更改其位置。

4. 请指定   Node.js 运行时  。

5. 指定安装 Playwright 的命令。

6. 点击   创建  。

根据您所选择的选项创建了一个新项目。

## 初始化 Playwright 项目

一旦项目创建完成，您需要初始化它。 在此过程中，您将选择所需的语言，安装 Playwright 浏览器，并指定其他设置。

![项目初始化](https://resources.jetbrains.com/help/img/idea/2026.3/aqua_playwright_project_initialization.png)

开始项目初始化：

Procedure:

1. 在   运行工具窗口   `Alt+4` (Windows), `⌘ 4` (macOS), `⌘ 4` (IntelliJ IDEA Classic (macOS)), `⌘ 4` (macOS System Shortcuts), `Alt+4` (XWin), `Alt+4` (GNOME), `Alt+4` (KDE), `Alt+4` (Emacs), `Alt+4` (Sublime Text), `⌘ 4` (Sublime Text (macOS)), `Ctrl+Shift+U` (VSCode), `⌘ ⇧ U` (VSCode (macOS))中，按键盘上的上/下箭头按钮选择您要在项目中使用的语言。

2. 请输入您要放置端到端测试的文件夹名称。

3. 请指定是否要添加 [GitHub Actions 工作流](https://docs.github.com/en/actions/using-workflows)。

4. 指定是否要安装 Playwright 浏览器。

当您指定所有参数时，初始化开始。 系统将通知您，一旦流程完成：

![项目初始化成功](https://resources.jetbrains.com/help/img/idea/2026.3/aqua_playwright_init_success.png)

## 基于角色的定位器支持

在 Playwright 中，您可以通过其 [角色](https://playwright.dev/docs/locators#locate-by-role)定位元素。 这有助于准确定位页面上的特定元素，无论是按钮、复选框、标题、链接或其他元素。

GoLand 可以生成这些定位器并将其添加到您的代码中，以便您在测试中使用它们。

![基于角色的定位器](https://resources.jetbrains.com/help/img/idea/2026.3/aqua_locator_types.png)

## 添加元素到代码

Procedure:

1. 打开您要添加元素的文件。

2. 点击 ![Web Inspector 按钮](https://resources.jetbrains.com/help/img/idea/2026.3/aqua-plugin.icons.webInspector%4020x20.svg)窗口右侧栏以打开   Web 检查器  工具栏窗口。

3. 请在地址栏中指定页面的 URL。

![地址栏控制](https://resources.jetbrains.com/help/img/idea/2026.3/aqua_address_bar_controls.png)

4. 点击 ![页面中选择元素按钮](https://resources.jetbrains.com/help/img/idea/2026.3/aqua-plugin.icons.browser.expui.selectWeb.svg) 并选择您想要在 Web Inspector 中添加的元素。

![选择页面中的元素](https://resources.jetbrains.com/help/img/idea/2026.3/aqua_select_element_in_page.png)

5. 一个元素被选定后，请点击 ![添加元素到代码](https://resources.jetbrains.com/help/img/idea/2026.3/app.expui.general.add.svg)将该元素添加到代码中。

!["添加元素到代码"按钮](https://resources.jetbrains.com/help/img/idea/2026.3/aqua_add_element_to_code.png)

若要添加特定类型的选择器（  ID 、  Name 、  Tag with classes 等），请点击 ![通过添加元素到代码](https://resources.jetbrains.com/help/img/idea/2026.3/app-client.expui.general.chevronDownLarge.svg)并选择所需选项。

![通过添加元素来编写代码](https://resources.jetbrains.com/help/img/idea/2026.3/aqua_locator_types.png)

> **Tip:**
> 在此，您还可以根据其角色将元素添加到代码中。

如果您想在 CSS 和 XPath 定位器之间切换，请点击 ![](https://resources.jetbrains.com/help/img/idea/2026.3/aqua-plugin.icons.locators.expui.css.svg)并选择所需的定位器类型。

![选择定位器类型](https://resources.jetbrains.com/help/img/idea/2026.3/aqua_locator_type_switcher.png)

因此，一段代码被生成并添加到代码编辑器中。

## 运行测试

Procedure:

* 运行测试时，点击测试类或测试方法旁边的 ![“运行”按钮](https://resources.jetbrains.com/help/img/idea/2026.3/app-client.expui.gutter.run.svg)装订区域图标，然后从列表中选择   运行  选项。

或者，将文本光标放置在测试类以运行该类中的所有测试，或放置在测试方法，然后按 `Ctrl+Shift+F10` (Windows), `⌃ ⇧ R` (macOS), `⌃ ⇧ F10` (IntelliJ IDEA Classic (macOS)), `⌃ ⇧ R` (macOS System Shortcuts), `Ctrl+Shift+F10` (XWin), `Ctrl+Shift+F10` (GNOME), `Ctrl+Shift+F10` (KDE), `Ctrl+Shift+F10` (Emacs), `Ctrl+Shift+F10` (Sublime Text), `Ctrl+Shift+F10` (Sublime Text (macOS)), `Ctrl+Shift+F10` (VSCode), `Ctrl+Shift+F10` (VSCode (macOS))。

您可以使用 run/debug configurations 以更加可定制的方式运行测试。 欲了解更多信息，请参阅 [运行测试](performing-tests.html)。

### 在可见模式下运行测试

在有界模式下运行测试使您能够直观地展示 Playwright 如何与网站进行交互。

要在有头模式下运行测试：

Procedure:

1. 点击运行/调试配置切换器中的当前配置，然后点击 ![](https://resources.jetbrains.com/help/img/idea/2026.3/app-client.actions.more.svg)并选择   编辑  选项。

![编辑运行/调试配置](https://resources.jetbrains.com/help/img/idea/2026.3/aqua_edit_configurations.png)

2. 在   Playwright 选项  字段中，指定  `--headed` 命令：

![配置运行/调试配置以在 headed mode 运行测试](https://resources.jetbrains.com/help/img/idea/2026.3/aqua_pw_for_js_ts_headed_mode.png)

3. 应用更改并运行您的测试。

结果，一个浏览器被打开以重现测试中的步骤。

## 查看测试结果

测试运行结束后，结果将显示在   测试运行器  选项卡的   运行  工具窗口中。 在此选项卡中，您可以重新运行测试、导出和导入测试结果、查看运行每个测试所花费的时间，并执行其他操作。

![在运行工具窗口的测试运行器选项卡上显示测试结果](https://resources.jetbrains.com/help/img/idea/2026.3/aqua_playwright_review_tests.png)

欲了解更多信息，请参阅 [探索测试结果](viewing-and-exploring-test-results.html)。

## 调试测试

一般来说，调试过程包括以下步骤：

1.  为必须挂起执行的代码行设置断点。

2.  在调试模式下运行您的代码。

3.  请使用   调试   工具窗口分析代码并定位潜在问题。

4.  解决问题。

要在 IDE 中重现这些步骤，请执行以下操作：

Procedure:

1. 在装订区域，悬停在您要挂起执行的可执行代码行上，然后点击 ![](https://resources.jetbrains.com/help/img/idea/2026.3/app-client.expui.breakpoints.breakpoint.svg)。

![设置断点](https://resources.jetbrains.com/help/img/idea/2026.3/aqua_playwright_set_breakpoints.png)

2. 点击 ![](https://resources.jetbrains.com/help/img/idea/2026.3/app-client.expui.run.run.svg) 并选择   调试  。 这将启动调试器会话并运行您的代码。

3. 一旦程序触及断点，执行就会暂停，您可以在   调试  工具窗口中查看调试会话的结果。

![调试工具窗口概述](https://resources.jetbrains.com/help/img/idea/2026.3/aqua_debug_overview.png)

有关调试的详细信息，请参阅 [调试](debugging-code.html)。

## 生成 Playwright 报告

[Playwright reporters](https://playwright.dev/docs/test-reporters) 提供有关失败测试的详细信息，使您能够更彻底地分析测试执行期间发生的情况。

在运行/调试配置中选择一个报告器，以控制报告的格式设置：

Procedure:

1. 点击运行/调试配置切换器中的当前配置，然后点击 ![](https://resources.jetbrains.com/help/img/idea/2026.3/app-client.actions.more.svg)并选择   编辑  选项。

![编辑运行/调试配置](https://resources.jetbrains.com/help/img/idea/2026.3/aqua_edit_configurations.png)

2. 在   Playwright 选项  字段中，指定所需的 [报告者](https://playwright.dev/docs/test-reporters#built-in-reporters)。 例如：

```GENERIC
--reporter=line

```

3. 应用更改并运行您的测试。

因此，会生成一份详细报告。

![生成的 Playwright 报告](https://resources.jetbrains.com/help/img/idea/2026.3/aqua_pw_report.png)

## 修改运行/调试配置

如果您想修改测试的启动属性，请编辑运行/调试配置：

Procedure:

1. 点击运行/调试配置切换器中的当前配置，然后选择   编辑配置…  选项。

![选择运行/调试配置](https://resources.jetbrains.com/help/img/idea/2026.3/aqua_playwright_edit_run_debug_config.png)

2. 在   运行/调试配置  对话框中，选择您想要编辑的配置。

3. 配置所需的启动属性。

![配置启动属性](https://resources.jetbrains.com/help/img/idea/2026.3/调试配置](run-debug-configuration.html)。

## See also

### 外部链接

[Playwright](https://playwright.dev/) [Playwright reporters](https://playwright.dev/docs/test-reporters) [List of reporters](https://playwright.dev/docs/test-reporters#built-in-reporters)

