按照赛普拉斯指南,我设法通过Typescript将赛普拉斯自定义命令注册到我的IDE中。我唯一遇到的麻烦是命令的默认值。
基本上:
index.d.ts
片段示例:
// commands.js
Cypress.Commands.add("rsChoose", {
prevSubject: true
}, (subject, text="", num=1, all=false) => {
// Open the select
cy.wrap(subject)
.click();
// Display all options
if (all === true) {
cy.wrap(subject)
.find(rsShowMoreButton)
.click();
}
// Alias the option list
cy.wrap(subject)
.find(rsOptions)
.as("options");
// Filter our options with our text
if (text.length > 0) {
cy.get("@options")
.contains(text)
.as("options");
}
// Choose the first matching option
cy.get("@options")
.eq(num - 1)
.click();
return cy.wrap(subject)
});
// index.d.ts
declare namespace Cypress {
interface JQuery<HTMLElement> {
/**
* Allows you to select an option from a FieldBox React select based on a given text
* If no text is given, will simply pick the first option
* Most select only display a limited amount of options. We can display them using the "all" param
* @prevSubject subject
* @param {String} [text=""] - The text our option must contain
* @param {Number} [num=1] - The option number we want to get (starts at 1)
* @param {Boolean} [all=false] - Forces the select to display all the available options
* @returns {Cypress} - The original subject (Cypress-wrapped select wrapper)
*/
rsChoose(text, num, all): JQuery<HTMLElement>
}
}
IDE中的错误:
我不确定要更改什么来解决此问题:(
答案 0 :(得分:2)
您需要告诉Typescript一些参数是可选的:
// index.d.ts
declare namespace Cypress {
interface JQuery<HTMLElement> {
rsChoose(text?, num?, all?): JQuery<HTMLElement>
}
}
如果您使用Typescript,我也建议您将类型添加到参数中;)