javascript - Cypress new command definition reference
问题描述
i created custom commands in Cypress, and then used them in my test.spec.js but when i try to go to the definition of my command (ctrl + rightClick) it shows (Any) and no reference. is there any solution for that ? because if someone else try to read my test script it will not be easy to know where the commad definition is ...
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
});
解决方案
Yes, there is a way.
You add one TS file in support/
.
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>
}
}
The command will be added into cy object, so when I type it will suggest the new custom command as well:
You can see there's info about the command when you hover over it:
Plus you will be takes to commands.d.ts
file when you click on it with Ctrl pressed.
推荐阅读
- unit-testing - 在 GO 语言中模拟
- javascript - firestore 使用带变量的更新将数据添加/附加到字段而不覆盖
- reactjs - 使用 ObjectId 发布到 mongoDb 多对一关系
- java - 我如何复制正在运行的应用程序的 RAM、保存它并稍后将其重新加载到 RAM 中?
- javascript - 如何创建一个函数返回一个在内部承诺解决后解决的承诺
- javascript - 将文本插入弹出字段(文本输入)
- java - JavaFX 输入字段不接受制表符
- javascript - 特定 json 的 typescript 接口
- ssl - Mosquitto MQTT 客户端证书异常
- opengl - CLion GLSL 支持插件。添加文件关联