我需要处理我的页面可能在加载阶段显示弹出对话框或可能不出现的情况。单击任意位置都会将其删除,我对测试此对话框不感兴趣,但它阻止了我需要访问的页面,因此必须将其删除
这是在对话框出现时获取对话框的代码
cy.get('.wps_popup')
.find('[data-wps-popup-close]')
.click()
Run Code Online (Sandbox Code Playgroud)
但我不能把它放在测试的顶部,因为这个元素可能不会出现。
如何处理条件元素 - 我是否需要intercept更改 DOM 并将该代码放入事件侦听器中?
我的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 挂钩期间,我们将跳过当前套件中的其余测试`
我基本上期望这里有一个软断言,即使该断言失败,它将继续运行其余的测试。
请指教。
我试图wait()在测试中避免使用该功能。
我知道,根据官方文档,Cypress 是异步工作的,我们不需要使用该wait()函数,特别是visit()命令处理该函数,因为它加载页面然后继续前进。
在我的测试用例中,我想处理导致问题的两件事:
打开左侧导航菜单上的下拉菜单。有 5 个菜单,我想要第二个
单击一个选项转到另一个页面
it("clicks on the 'Front End' and navigates to the correct page", () => {
visit(path, {
timeout: 120000,
pageLoadTimeout: 120000,
});
cy.get(selectors.CATEGORIES)
.eq(2)
// I use 'within', because I want to search **inside the
// selectors.CATEGORIES.eq(2) and not on the whole DOM**
.within(() => {
cy.get(dataCySelector("gridRow")).then(($optionsWrapper) => {
const parentEl = $optionsWrapper.parent();
const isMenuOpen = parentEl.css("display");
// if i dont add the wait(), it selects the …Run Code Online (Sandbox Code Playgroud) 我试图将断言传递给 if 条件,并在满足条件时执行一个逻辑,在条件失败时执行另一个逻辑。
由于测试因断言失败而失败,我无法达到预期的结果。
我尝试了以下...
if(cy.get("div").length \> 0) {
cy.log("print this")
} else {
cy.log("print this")
}
Run Code Online (Sandbox Code Playgroud)
或者
if(cy.get("div").should('have.length.greaterThan', 0) {
cy.log("print this")
} else {
cy.log("print this")
}
Run Code Online (Sandbox Code Playgroud)