【问题标题】:Cypress new command definition referenceCypress 新命令定义参考
【发布时间】:2020-12-30 03:45:03
【问题描述】:

我在 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 
    });

【问题讨论】:

    标签: javascript reference command cypress definition


    【解决方案1】:

    是的,有办法。

    你在support/中添加一个TS文件。

    支持/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.Commands.add('login', () => { // 命令体 });
    猜你喜欢
    • 1970-01-01
    • 2022-07-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-16
    • 2019-08-12
    • 2021-01-29
    • 1970-01-01
    相关资源
    最近更新 更多