// spec/cypress/support/on-rails.ts
Cypress.Commands.add('app', function (name, command_options) {
return cy
.appCommands({ name: name, options: command_options })
.then((body) => {
return body[0];
});
});
Run Code Online (Sandbox Code Playgroud)
spec/cypress/tsconfig.json
{
"compilerOptions": {
"strict": true,
"target": "es5",
"lib": ["es6", "dom"],
"module": "es6",
"moduleResolution": "node",
"sourceMap": false,
"types": ["cypress"],
"downlevelIteration": true,
"allowSyntheticDefaultImports": true,
"esModuleInterop": true
},
"include": ["**/*.ts"]
}
Run Code Online (Sandbox Code Playgroud)
结果是
Property 'app' does not exist on type 'cy & EventEmitter'.
Run Code Online (Sandbox Code Playgroud)
相当老了,但我最近遇到了这个问题。
该文档展示了一种处理命令的方法。这是他们文档的链接:https://docs.cypress.io/guides/tooling/typescript-support#Adding-child-or-dual-commands
赛普拉斯/支持/index.ts
// load type definitions that come with Cypress module
/// <reference types="cypress" />
declare global {
namespace Cypress {
interface Chainable {
/**
* Custom command to type a few random words into input elements
* @param count=3
* @example cy.get('input').typeRandomWords()
*/
typeRandomWords(
count?: number,
options?: Partial<TypeOptions>
): Chainable<Element>
}
}
}
Run Code Online (Sandbox Code Playgroud)
赛普拉斯/支持/index.ts
Cypress.Commands.add('typeRandomWords', { prevSubject: 'element' }, (
subject /* :JQuery<HTMLElement> */,
count = 3,
options?
) => {
return cy.wrap(subject).type(generateRandomWords(count), options)
})
Run Code Online (Sandbox Code Playgroud)