Edd*_*ddy 2 assertion cypress cypress-conditional-testing
我的before()挂钩中有以下代码行。
before(() ==> {
cy.get(this.testPopup).then(($el) => {
if ($el.length) {
cy.get(this.testPopupCloseButton).click();
cy.get(this.testPopup).should("not.exist");
}
});
});
Run Code Online (Sandbox Code Playgroud)
会有一个弹出窗口,有时会显示,有时不会。我尝试编写一个逻辑来关闭该弹出窗口,并检查仅当弹出窗口存在时它是否在关闭时消失。
但我收到以下错误。
30000 毫秒后超时重试:期望找到元素:#test-popup-element,但从未找到。
由于此错误发生在 before all 挂钩期间,我们将跳过当前套件中的其余测试`
我基本上期望这里有一个软断言,即使该断言失败,它将继续运行其余的测试。
请指教。
小智 5
您无法解决问题,cy.get('body').then(($body) =>因为代码将在弹出窗口出现之前完成。该代码中没有重试,它总是立即决定弹出窗口是否存在。
相反,请使用元素轮询,如 Fody 所示:
如何使用 Cypress 检查可能不存在的元素
const ifElementExists = (selector, attempt = 0) => {
if (attempt === 100) return null // no appearance, return null
if (Cypress.$(selector).length === 0) {
cy.wait(100, {log:false}) // wait in small chunks
getDialog(selector, ++attempt) // try again
}
return cy.get(selector, {log:false}) // done, exit with the element
}
// Wait for 10 seconds for the popup
ifElementExists(this.testPopup).then($el => {
if ($el?.length) {
cy.get(this.testPopupCloseButton).click()
cy.get(this.testPopup).should('not.exist')
}
})
Run Code Online (Sandbox Code Playgroud)
这是有效的,因为它将等待弹出窗口长达 10 秒。如果您需要更长的等待时间,
请增加 。if (attempt === 100)您必须决定需要等待的最长时间并attempt相应地设置限制。
如果那时它从未出现,if ($el?.length)则会出现false,并且不会单击关闭按钮。
否则,弹出窗口最终会出现,代码可以继续并单击关闭按钮。
| 归档时间: |
|
| 查看次数: |
90 次 |
| 最近记录: |