赛普拉斯看不到自定义 cy。命令



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

命令.js

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

支持/索引.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'.

索引.js和命令.js未从原始目录中移动。 赛普拉斯.json 文件为空。

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

他们的文档似乎在这里很好地概述了它。不过,与他们的示例唯一不同的是,您不会从generateToken返回任何内容,因此我认为您无法将其放入全局Chainable接口中。

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

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

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

这是一个工作示例:

cypress/support/command.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");

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

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代码中获得类型支持,也不想面对运行时错误cypress Property does not exist on type 'cy & EventEmitter'

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

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

命令.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);

tsconfig.json中添加路径

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

尝试调用Cypress.generateToken('XXXX');这行得通吗?

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

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

在支持/命令中做这样的事情.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;
}

一种针对不想在cypress.d.ts中声明任何命令类型的开发人员的方法,至少在实验时是这样。

使用此声明文件,可以添加和调用任何命令,而不会抱怨打字稿。

// cypress.d.ts 
import { mount } from "cypress/react"
declare global {
namespace Cypress {
interface Chainable<Subject = any> {
mount: typeof mount
[k: string]: (...args: any[]) => Chainable<Subject>
}
}
}

在下面尝试,看看它是否有效。

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

相关内容

最新更新