cypress - 在 Cypress 中重载自定义命令
问题描述
自定义命令重载可以像函数重载一样完成吗?文档中没有对此的答案。
例如:
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);
});
解决方案
事实并非如此。命令名称navigateAndWaitForApi
是总签名。
在命令定义之后添加
console.log(Cypress.Commands._commands)
显示命令存储在一个对象中,由命令名称键入。
两次添加相同的命令,第二次覆盖第一个。
可以在运行时对参数进行类型检查。
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)
})
});
推荐阅读
- google-cloud-platform - 在 stackdriver 中找不到 Cloud DLP 扫描结果
- aframe - Aframe 示例网络摄像头,但显示错误
- c# - 当我关闭一个 AppWindow 时,为什么我不能打开一个新的 AppWindow
- arrays - 从排序数组构造 BTree
- java - 带有连接的 JdbcTemplate 查询的可维护映射方法
- bash - 使用 bash 和 sed 如何从主机命令中提取 ip 并将其保存在 /etc/hosts 文件中
- javascript - 在打开和关闭语句内匹配({ 和 }-ish)
- spring-boot - 如何配置 Spring Boot 应用程序以接受未知的 SSL 客户端证书?
- php - url重定向时从curl下载pdf文件
- python-3.x - 不存储在数据库postgresql中的scrapy项目数据