# Cypress 自定义 Commands

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

> **Note:**
> 在 IntelliJ IDEA 中对 Cypress 自定义命令的支持由 [Test Automation](https://plugins.jetbrains.com/plugin/20175-test-automation) 插件提供。 如果相关功能不可用，请确保插件 [已安装并启用](ui-test-automation.html#install_test_automation_plugin)。

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

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

## 自定义命令编码辅助

### Cypress 专用检查

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

但是，此机制具有一些特定性。 例如，任何 [action command](https://docs.cypress.io/guides/core-concepts/interacting-with-elements#Actionability)必须始终放在链的末尾并且只能使用一次。

不正确的使用:

在此示例中，这个链包含两个连在一起的 action 命令，这是不允许的。 执行此测试将导致错误。

```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')
})

```

在 JavaScript/TypeScript 中不执行命令链的验证，而在 Cypress 中仅在运行时执行，这可能会导致测试执行过程中的问题。 IntelliJ IDEA 则为链式命令提供检查，并高亮显示不正确的用法。

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

### 函数特定检查

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

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

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

有关更多信息，请参见 [代码检查](code-inspection.html)。

### 正在重命名

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

Procedure:

1. 高亮显示要重命名的命令。

![高亮命令](https://resources.jetbrains.com.cn/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)), `Ctrl+R, R` (Visual Studio), `⌘ R, R` (Visual Studio (macOS)), `Ctrl+R` (NetBeans), `Alt+Shift+R` (Eclipse), `⇧ F6` (Eclipse (macOS)) 并提供一个新名称用于该命令。

![重命名命令](https://resources.jetbrains.com.cn/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.cn/help/img/idea/2026.3/aqua_ts_cypress_command.png)

JavaScript:

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

> **Tip:**
> 最初，IntelliJ IDEA 会根据为项目选择的编程语言，将该命令高亮显示为错误或弱警告。 然而，一旦您创建了 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)), `Alt+Enter` (Visual Studio), `⌥ ⏎` (Visual Studio (macOS)), `Alt+Enter` (NetBeans), `Ctrl+1` (Eclipse), `⌘ 1` (Eclipse (macOS))，然后选择   创建缺失的 TypeScript 声明  。

TypeScript:

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

JavaScript:

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

![生成的 TypeScript 声明](https://resources.jetbrains.com.cn/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.cn/help/img/idea/2026.3/app-client.expui.gutter.implementingMethod.svg)。

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

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

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

## 使用自定义命令

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

Procedure:

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

自定义命令:

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

用法:

![自定义登录命令用法](https://resources.jetbrains.com.cn/help/img/idea/2026.3/commands.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+Alt+S` (Visual Studio), `⌘ Comma` (Visual Studio (macOS)), `Ctrl+Alt+S` (NetBeans), `Ctrl+Alt+S` (Eclipse), `⌘ Comma` (Eclipse (macOS)) 打开设置，然后选择  `语言与框架 | Cypress` 。

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

![指定新路径](https://resources.jetbrains.com.cn/help/img/idea/2026.3/aqua_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)

