如何在赛普拉斯中执行自定义命令?

时间:2019-03-21 13:40:43

标签: javascript typescript visual-studio-code cypress

我在cypress中使用自定义命令,它工作正常。我正在使用Visual Studio代码作为编辑器。 我一直在寻找如何让智能识别自定义命令的方法,并在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>
}}

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

1 个答案:

答案 0 :(得分:1)

要直接回答,不支持打开/查看自定义命令的直接声明(如果支持,则某些人可以更正)。我通常会按照自定义命令在单独的文件中进行分组。例如,

文件: 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文件中,我将导入命令文件,

import './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