# Cypress 自定义 Commands

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

[自定义命令](https://docs.cypress.io/api/cypress-api/custom-commands)提供了一种在 Cypress 测试套件中重用某些方法或函数的方式。 例如，您可以编写一个命令来登录用户，并在项目中适用时重复使用它。

GoLand 支持自定义命令，这意味着所有代码辅助功能，如重命名、导航、代码补全和检查等，都可以立即使用。

## 自定义命令编码辅助

### Cypress 专用检查

Cypress 使用一种 [链机制](https://docs.cypress.io/guides/core-concepts/introduction-to-cypress#Chains-of-Commands)，使您能够编写一系列命令，以便信息从一个命令传递到下一个命令，直到链结束或发生错误。

在  `click()`  等操作命令之后，传递给下一个命令的元素可能会过时。 GoLand 会对不安全的命令链发出警告。 开始新的链，以重新查找该元素。

不正确的使用:

此链会将被点击的按钮传递给  `type()` ，而不是查找输入字段。

```JAVASCRIPT
it('Click the search button and type Cypress', () => {
    cy.get("button[data-test='search-button']")
        .click()
        .type('Cypress')
 })

```

正确用法:

点击按钮后，开始新的链以查找输入字段：

```JAVASCRIPT
it('Click the search button and type Cypress', () => {
    cy.get("button[data-test='search-button']").click()
    cy.get("input[data-test$='inner']").type('Cypress')
})

```

当命令收到不兼容的元素或值时，命令链检查也会发出警告。

![检查不正确的链接命令](https://resources.jetbrains.com/help/img/idea/2026.3/aqua_incorrect_command_chain.png)

### 函数特定检查

GoLand 执行特定功能的代码检查，以检测和更正项目中的异常代码。

例如，如果您为该命令提供了不正确的参数数量或类型，错误代码会被高亮显示。 您可以将鼠标悬停在高亮的代码上查看错误描述。

![Aqua ts function specific inspections](https://resources.jetbrains.com/help/img/idea/2026.3/aqua_ts_function_specific_inspections.png)

欲了解更多信息，请参阅 [代码检查](code-inspection.html)。

### 重命名

如果您需要重命名命令，可以使用  Rename  重构。 为此：

Procedure:

1. 突出显示您想要重命名的命令。

![高亮命令](https://resources.jetbrains.com/help/img/idea/2026.3/aqua_highlighted_command.png)

2. 请按 `Shift+F6` (Windows), `⇧ F6` (macOS), `⇧ F6` (IntelliJ IDEA Classic (macOS)), `⌘ ⌥ R` (macOS System Shortcuts), `Shift+F6` (XWin), `Shift+F6` (GNOME), `Shift+F6` (KDE), `Shift+F6` (Emacs), `Shift+F6` (Sublime Text), `⇧ F6` (Sublime Text (macOS)), `F2` (VSCode), `F2` (VSCode (macOS)) 并提供一个新名称用于该命令。

![重命名命令](https://resources.jetbrains.com/help/img/idea/2026.3/aqua_rename_refactoring.png)

3. （可选）配置其他搜索选项并定义范围。

4. 点击   重构  。

欲了解更多信息，请参阅 [重命名重构](rename-refactorings.html)。

## 创建自定义命令

要创建自定义命令，您必须先编写命令，然后为其创建一个 TypeScript 声明。 为此：

Procedure:

1. 在代码编辑器中编写您的命令。

> **Note:**
> 建议在  `cypress/support/commands.ts`  或  `cypress/support/commands.js`  文件中指定命令，因为在评估任何测试文件之前，此文件会通过 [supportFile](https://docs.cypress.io/guides/core-concepts/writing-and-organizing-tests#Support-file) 中提供的 import 语句加载。

TypeScript:

![TypeScript 编写的自定义命令](https://resources.jetbrains.com/help/img/idea/2026.3/aqua_ts_cypress_command.png)

JavaScript:

![TypeScript 编写的自定义命令](https://resources.jetbrains.com/help/img/idea/2026.3/aqua_js_cypress_command.png)

> **Tip:**
> 最初，GoLand 会将该命令突出显示为错误或弱警告，具体取决于为项目选择的编程语言。 然而，一旦您创建了 TypeScript 声明，问题就会得到解决。

2. 将鼠标悬停在高亮代码上，然后点击   创建缺失的 TypeScript 声明  。

或者，将文本光标放在高亮显示的代码上，按下 `Alt+Enter` (Windows), `⌥ ⏎` (macOS), `⌥ ⏎` (IntelliJ IDEA Classic (macOS)), `⌥ ⏎` (macOS System Shortcuts), `Alt+Enter` (XWin), `Alt+Enter` (GNOME), `Alt+Enter` (KDE), `Alt+Enter` (Emacs), `Alt+Enter` (Sublime Text), `⌥ ⏎` (Sublime Text (macOS)), `Ctrl+.` (VSCode), `⌘ .` (VSCode (macOS))，然后选择   创建缺失的 TypeScript 声明  。

TypeScript:

![创建 TypeScript 声明](https://resources.jetbrains.com/help/img/idea/2026.3/aqua_creating_ts_declaration.png)

JavaScript:

![创建 JavaScript 声明](https://resources.jetbrains.com/help/img/idea/2026.3/index.d.ts`  文件中。

![生成的 TypeScript 声明](https://resources.jetbrains.com/help/img/idea/2026.3/aqua_generated_ts_declaration.png)

> **Tip:**
> 如果需要，您可以在设置中 [更改位置](#change_location_of_command_file) 的  `index.d.ts`  文件。

## 导航到 TypeScript 声明

您可能希望在命令实现与相应的 TypeScript 声明之间进行导航：

Procedure:

* 要导航到 TypeScript 声明，请点击命令实现附近边栏中的 ![](https://resources.jetbrains.com/help/img/idea/2026.3/app-client.expui.gutter.implementingMethod.svg)。

![导航到 TypeScript 声明](https://resources.jetbrains.com/help/img/idea/2026.3/aqua_go_to_ts_declaration.png)

* 要导航回命令实现，请点击 TypeScript 声明附近装订线中的 ![](https://resources.jetbrains.com/help/img/idea/2026.3/app-client.expui.gutter.implementedMethod.svg)。

![导航到命令实现](https://resources.jetbrains.com/help/img/idea/2026.3/aqua_go_to_command_declaration.png)

## 使用自定义命令

一旦您 [创建](#creating_custom_commands)了自定义命令，就可以在代码中使用它。 为此：

Procedure:

* 在您的测试中，键入  `cy.` 后跟自定义命令的名称。

自定义命令:

![自定义登录命令](https://resources.jetbrains.com/help/img/idea/2026.3/aqua_custom_login_command.png)

用法:

![自定义登录命令用法](https://resources.jetbrains.com/help/img/idea/2026.3/index.d.ts` 。 如果您想更改位置：

Procedure:

1. 按下 `Ctrl+Alt+S` (Windows), `⌘ Comma` (macOS), `⌘ Comma` (IntelliJ IDEA Classic (macOS)), `⌘ Comma` (macOS System Shortcuts), `Ctrl+Alt+S` (XWin), `Ctrl+Alt+S` (GNOME), `Ctrl+Alt+S` (KDE), `Ctrl+Alt+S` (Emacs), `Ctrl+Alt+S` (Sublime Text), `⌘ Comma` (Sublime Text (macOS)), `Ctrl+Comma` (VSCode), `⌘ Comma` (VSCode (macOS)) 打开设置，然后选择  `语言与框架 | Cypress` 。

2. 在   用于存储自定义命令的 TS 声明的 TypeScript 文件  字段中，指定一个新路径。

![指定新路径](https://resources.jetbrains.com/help/img/idea/2026.3/go_custom_commands_settings.png)

3. 应用更改并关闭对话框。

## See also

### 外部链接

[Cypress Custom Commands](https://docs.cypress.io/api/cypress-api/custom-commands) [Cypress chaining mechanism](https://docs.cypress.io/guides/core-concepts/introduction-to-cypress#Chains-of-Commands) [Cypress action commands](https://docs.cypress.io/guides/core-concepts/interacting-with-elements#Actionability)

