赛普拉斯新命令定义参考

问题描述

我在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对象中,因此当我键入该命令时,也会建议新的自定义命令:

enter image description here

将命令悬停在命令上时,您会看到有关该命令的信息:

enter image description here

此外,如果您在按住Ctrl键的同时单击commands.d.ts文件,则会将其转到该文件。

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...