【问题标题】:Cypress doesn't see custom cy. commands赛普拉斯没有看到自定义 cy。命令
【发布时间】:2020-02-08 00:35:32
【问题描述】:

赛普拉斯无法导入自定义命令

commands.js

Cypress.Commands.add('generateToken', ({secret}) => {
    const totp = require('totp-generator');
    const token = totp(secret); 
    });

支持/index.js

import './commands'

test.spec.ts

/// <reference types="Cypress" />

context('Actions', () => {
    beforeEach(() => {})
  })
    it('Main test', () => {
        cy.generateToken('XXXX');
    })

test.spec.ts 中的 generateToken() 始终加下划线并出现错误:

Property 'generateToken' does not exist on type 'cy'.

index.js 和 commands.js 没有从原始目录中移动。 cypress.json 文件为空。

【问题讨论】:

    标签: typescript cypress


    【解决方案1】:

    由于您的规范是一个打字稿文件,您是否为 support/index.js 添加了新的类型定义?

    他们的文档似乎很好地概述了它here。不过,与他们的示例不同的是,您没有从 generateToken 返回任何内容,因此我认为您不能将其放入全局 Chainable 接口中。

    您可以尝试将其添加到 support/index.d.ts 文件中,看看它是否对您大喊大叫

    declare namespace Cypress {
      interface Chainable {
        generateToken({secret}: {secret: string}): void
      }
    }
    

    【讨论】:

    • 非常感谢。看起来它有效!无法访问工作回购检查,明天会做。我不得不稍微修改一下代码:declare namespace Cypress { interface Chainable { generateToken(secret): void } } 你能解释一下它为什么起作用吗?
    【解决方案2】:

    这是我如何让 TypeScript 查看我的自定义命令:

    commands.ts

    declare namespace Cypress {
      interface Chainable<Subject> {
        generateToken(secret: any): Cypres.Chainable<void>;
      }
    }
    
    function generateToken(secret: any): void {
      // Generate token
    }
    
    Cypress.Commands.add('generateToken', generateToken);
    

    test.spec.ts

    cy.generateToken(secret);
    

    【讨论】:

    • 我没有 commands.ts 文件。我只有commands.js。你在哪里创建的?
    • 我只是将其命名为 commands.ts 而不是 commands.js。您的索引文件仍然可以只使用import './commands';
    【解决方案3】:

    2021 年全面开工。

    对于support/index.ts 文件

    import './commands'
    

    对于support/commands.ts 文件

    Cypress.Commands.add('generateToken', generateToken);
    function generateToken({secret}): void {
        const totp = require('totp-generator');
        const token = totp(secret); 
    }
    
    // this is another example.
    Cypress.Commands.add('login', login);
    function login(username: string, password: string): void {
        // steps for login 
    }
    
    declare namespace Cypress {
        interface Chainable<Subject> {
           generateToken({secret}): void;
    
           /**
            * This will log user in
            * @param email string
            * @param password string
            */
           login(email: string, password: string): void;
        }
    }
    

    对于test.spec.ts 文件

    cy.login("somebody@domain.com", "something");
    cy.generateToken("Example");
    

    其他答案都不适合我。我想在 VS Code 中提供类型支持,并且不想面对 cypress Property does not exist on type 'cy &amp; EventEmitter' 运行时错误。

    我添加了另一个支持 JSDoc 的登录示例。

    【讨论】:

      【解决方案4】:

      由于 nshirley 提出的答案对我不起作用,我找到了this 评论。 它非常相似,但在顶部添加了declare global,以在全局范围内声明这些自定义命令。

      这是一个工作示例:

      cypress/support/commands.ts:

      declare global {
        namespace Cypress {
          interface Chainable<Subject> {
            /**
             * Provides a working example
             */
            generateToken(secret: any): Cypress.Chainable<Element>;
          }
        }
      }
      
      const generateToken = (secret: any) => {
        // Your Code
      };
      
      Cypress.Commands.add("generateToken", generateToken);
      

      在你的测试中你可以直接使用它:

      cy.generateToken("Example");
      

      如您所见,您甚至可以为您的方法提供一些文档,但这完全是可选的。

      【讨论】:

        【解决方案5】:

        尝试致电Cypress.generateToken('XXXX'); 可以吗?

        另外,在添加命令时尝试删除不必要的括号。试试这个:

        Cypress.Commands.add('generateToken', secret => {
            const totp = require('totp-generator');
            const token = totp(secret); 
        });
        

        【讨论】:

        • 不,同样的问题。 ```任何属性'generateToken'在'Cypress'.ts(2339)类型上不存在```
        • 我已经更新了答案,也许你在那里遇到了括号问题?如果这不起作用,我无能为力:/
        • 噗……不知道的人。在这里查看我们的 repo,我们一直在使用 Cypress 进行一些测试,并在那里有几个自定义命令。也许您会发现您和我们之间的一些差异,这将是关键..github.com/boxwise/dropapp/tree/master/cypress/support
        【解决方案6】:

        试试下面看看是否有效。

        Cypress.Commands.add('generateToken', { prevSubject: false }, ({secret}) => {

        // 你的代码。

        });

        【讨论】:

        • 没有帮助。即使我在commands.js中取消注释某些示例函数,它仍然不能用于测试。
        【解决方案7】:

        在 support/command.js 中做这样的事情

        Cypress.Commands.add('generateToken', (secret) => {
        
               const totp = require('totp-generator');
               const token = totp(secret);
          };
          cy.request(options)
        
        });
        

        然后在你的js代码中

            /// <reference types="Cypress" />
        
        context('Actions', () => {
            beforeEach(() => {})
          })
            it('Main test', () => {
                const token ='XXXX'
                cy.generateToken(token);
            })
        

        编辑 1

        你可以添加这个,并且可以根据你的测试添加更多的迭代等待

        describe('check the tokens', function() 
        {
        // on 30 seconds 
        it('first token ',()=> {
            cy.wait(30000).then(()=>{
                const token = getToken();   // You're waiting here for 30 sec before you get token
                console.log('first token: ' + token);
            })
        })
        // on 60 seconds 
        it('second token', ()=>{
            cy.wait(60000).then(()=>{
                const token = getToken();
                console.log('second token: '+ token);
            });
        })
        })
        
        function getToken () {
            const totp = require('totp-generator');
            const token = totp('ABCD');
            return token;
          }
        

        【讨论】:

        • 不,同样的问题。
        • @A.Makarevich - 在这种情况下,我会在 test.spec 中创建一个函数并调用它。查看我的编辑
        • 问题是,这个函数在测试开始时被调用,但是测试本身持续了30多秒,并且token过期了。每次我想使用它时,我都需要重新生成它。
        • @A.Makarevich - 是的,这很痛苦。我在编辑 1 上编辑了更多内容,可能会尝试那个,并且可以在需要时添加迭代等待
        • 不,这样的铁杆等待对我来说不是一个选择。
        【解决方案8】:

        在tsconfig.json中添加路径

        {
          "include": [
            "integration/*.ts",
            "support/*.ts"
          ]
        }
        

        【讨论】:

          猜你喜欢
          • 2020-03-17
          • 2019-06-05
          • 1970-01-01
          • 1970-01-01
          • 2021-11-11
          • 2021-03-01
          • 1970-01-01
          • 2020-04-14
          • 1970-01-01
          相关资源
          最近更新 更多