我在 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
下声明所有命令。
引用:
关于javascript - 如何在 Cypress 中执行自定义命令?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55281766/