javascript - 如何在 Cypress 中执行自定义命令?

标签 javascript typescript visual-studio-code cypress

我在 cypress 中使用自定义命令,它工作正常。我正在使用 visual studio code 作为编辑器。 我一直在寻找如何让智能感知识别自定义命令,并在 https://github.com/cypress-io/cypress-example-todomvc#cypress-intellisense 中找到了它

我添加了 cypress/index.d.ts 文件:

/// <reference types="cypress" />
declare namespace Cypress {
interface Chainable<Subject> {
  /**
   * Do something
   * @example
   * cy.doSomething()
   */
  doSomething(): Chainable<any>
}}

现在当点击 spec 文件中的 doSomething 时,它会打开 index.d.ts 中的声明,有没有办法让 vscode 打开 support/commands.js 下的实际命令实现?

最佳答案

直接回答,不支持打开/查看自定义命令的直接声明(可能,如果支持,有人可以更正)。我通常会在单独的文件中遵循自定义命令的分组。例如,

文件: cypress/support/sample_command.ts

/// <reference types="Cypress" />

import * as PageElements from "../resources/selectors.json";
import * as Pages from "../resources/urls.json";

let xml: XMLDocument
let data: HTMLCollection

Cypress.Commands.add(
  "getWorkflowXML",
  (wfPath: string): Cypress.Chainable<HTMLCollection> => {
    var url = Cypress.env("url") + wfPath;

    return cy.request({
      log: true,
      url: url,
      auth: {
        user: Cypress.env("userName"),
        pass: Cypress.env("password")
      }
    }).then(response => {
      expect(response)
        .property("status")
        .to.equal(200);
      xml = Cypress.$.parseXML(response.body);
      data = xml.getElementsByTagName("java");
      return data;
    });
  }
);

declare global {
  namespace Cypress {
    interface Chainable {
      /**
       * Get Workflow XML through XHR call
       * @memberof Cypress.Chainable
       * @param wfPath
       */
      getWorkflowXML: (wfPath: string) => Cypress.Chainable<HTMLCollection>;
    }
  }
}

然后,在 cypress/support/index.js 文件中,我将导入命令文件,

导入 './sample_command'

这种方式给了我更好的可追溯性,而不是直接在 index.d.ts 下声明所有命令。

引用:

  1. https://docs.cypress.io/api/cypress-api/custom-commands.html#Syntax
  2. https://github.com/cypress-io/add-cypress-custom-command-in-typescript

关于javascript - 如何在 Cypress 中执行自定义命令?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55281766/

相关文章:

javascript - Backpax - 初始化后设置图像数据属性(data-img)

javascript - Ti.App.fireEvent 不起作用;错误未捕获类型错误 : Cannot read property 'App' of undefined

javascript - 比较属性源中的整数

typescript 接口(interface) : optional but not undefined

javascript - Angular 6和ASP Core中的动态权限

debugging - 使用 Visual Studio Code 连接到 QEMU 中运行的 OP-TEE 的 gdb-server

javascript - 如何给vscode添加全局变量?

javascript - 如何在vanilla js中选择一个在id中具有特定文本的选择类型对象?

node.js - 如何使用 Typescript 构建 Angular2 + NodeJs + Express 应用程序

android-studio - 可以使用 flutter 在 VS Code 和 Android Studio 中启动但不能连接到模拟器