Cypress 新命令定义参考
Cypress new command definition reference
我在 Cypress 中创建了自定义命令,然后在我的 test.spec.js 中使用了它们,但是当我尝试转到命令的定义 (ctrl + rightClick) 时,它显示 (Any) 并且没有参考。有什么解决办法吗?因为如果其他人试图阅读我的测试脚本,就不容易知道命令定义在哪里...
test.spec.js
describe('test', () => {
before('Login before', () => {
cy.visit('/');
// my custom command cy.login()
cy.login();
});
});
commands.js
// definition of the command
Cypress.Commands.add('login', () => {
// body of the command
});
是的,有办法。
您在 support/
中添加了一个 TS 文件。
support/commands.d.ts:
declare namespace Cypress {
interface Chainable<Subject> {
/**
* Logs in a user
*
* @param {string} name User to log in
* @example
* cy.login('admin');
*
*/
login(name: string): Chainable<any>
}
}
该命令将添加到 cy 对象中,因此当我键入它时,它也会提示新的自定义命令:
当你将鼠标悬停在它上面时,你可以看到关于命令的信息:
此外,当您在按住 Ctrl 键的情况下单击它时,您将进入 commands.d.ts
文件。
我在 Cypress 中创建了自定义命令,然后在我的 test.spec.js 中使用了它们,但是当我尝试转到命令的定义 (ctrl + rightClick) 时,它显示 (Any) 并且没有参考。有什么解决办法吗?因为如果其他人试图阅读我的测试脚本,就不容易知道命令定义在哪里...
test.spec.js
describe('test', () => {
before('Login before', () => {
cy.visit('/');
// my custom command cy.login()
cy.login();
});
});
commands.js
// definition of the command
Cypress.Commands.add('login', () => {
// body of the command
});
是的,有办法。
您在 support/
中添加了一个 TS 文件。
support/commands.d.ts:
declare namespace Cypress {
interface Chainable<Subject> {
/**
* Logs in a user
*
* @param {string} name User to log in
* @example
* cy.login('admin');
*
*/
login(name: string): Chainable<any>
}
}
该命令将添加到 cy 对象中,因此当我键入它时,它也会提示新的自定义命令:
当你将鼠标悬停在它上面时,你可以看到关于命令的信息:
此外,当您在按住 Ctrl 键的情况下单击它时,您将进入 commands.d.ts
文件。