自定义命令重载可以像函数重载一样完成吗?文档中没有对此的答案。
例如:
Cypress.Commands.add('navigateAndWaitForApi',
(relativePath: string, apisPath: string[], timeout?: number) => {
let options = {};
if (timeout !== undefined) {
options = { timeout: TIMEOUT };
}
apisPath.forEach((api)=> {
cy.intercept(`/api/${api}`).as(api);
})
cy.visit(`/${relativePath}`);
cy.wait(apisPath.map(apiPath => `@${apiPath}`), options);
});
Cypress.Commands.add('navigateAndWaitForApi',
(relativePath: string, apiPath: string, timeout?: number) => {
cy.navigateAndWaitForApi(relativePath, [apiPath], timeout);
});
Run Code Online (Sandbox Code Playgroud)
看来并非如此。命令名称navigateAndWaitForApi是总签名。
在命令定义之后添加此内容
console.log(Cypress.Commands._commands)
Run Code Online (Sandbox Code Playgroud)
显示命令存储在一个对象中,由命令名称作为键控。
添加相同的命令两次,第二次将覆盖第一个。
可以在运行时对参数进行类型检查。
Cypress.Commands.add('navigateAndWaitForApi',
(relativePath: string, apiPaths: string|string[], timeout?: number) => {
if (typeof apiPaths === 'string') {
apiPaths = [apiPaths]
}
let options = {};
if (timeout !== undefined) {
options = { timeout: TIMEOUT };
}
apiPaths.forEach((api)=> {
cy.intercept(`/api/${api}`).as(api);
})
cy.visit(`/${relativePath}`);
// cy.wait(apiPaths.map(apiPath => `@${apiPath}`), options); // look dubious
apiPaths.forEach(apiPath => {
cy.wait(`@${apiPath}`), options)
})
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
604 次 |
| 最近记录: |