赛普拉斯:如何知道在使用 If 条件和 xpath 时元素是否可见?
Cypress: How to know if element is visible or not in using If condition and xpath?
上述问题的答案非常适合 CSS 选择器。答案是
cy.get("body").then($body => {
if ($body.find("selector_for_your_button").length > 0) {
//evaluates as true if button exists at all
cy.get("selector_for_your_button']").then($header => {
if ($header.is(':visible')){
//you get here only if button EXISTS and is VISIBLE
} else {
//you get here only if button EXISTS but is INVISIBLE
}
});
} else {
//you get here if the button DOESN'T EXIST
assert.isOk('everything','everything is OK');
}
});
我想实现类似的东西,但对于 XPath。谁能帮帮我?
提前致谢
借助 XPath,您可以使用 count()
cy.xpath(`count(//*[@class="${selector}"])`)
.then(count => {
if (count > 0) {
cy.xpath(`//*[@class="${selector}"]`) // repeat without count
.click()
} else {
}
})
但我认为@jjhelguero 的警告仍然适用。条件测试没有任何重试,因此您正在失去 Cypress 的最大优势 - 减少测试中的脆弱性。
上述问题的答案非常适合 CSS 选择器。答案是
cy.get("body").then($body => {
if ($body.find("selector_for_your_button").length > 0) {
//evaluates as true if button exists at all
cy.get("selector_for_your_button']").then($header => {
if ($header.is(':visible')){
//you get here only if button EXISTS and is VISIBLE
} else {
//you get here only if button EXISTS but is INVISIBLE
}
});
} else {
//you get here if the button DOESN'T EXIST
assert.isOk('everything','everything is OK');
}
});
我想实现类似的东西,但对于 XPath。谁能帮帮我?
提前致谢
借助 XPath,您可以使用 count()
cy.xpath(`count(//*[@class="${selector}"])`)
.then(count => {
if (count > 0) {
cy.xpath(`//*[@class="${selector}"]`) // repeat without count
.click()
} else {
}
})
但我认为@jjhelguero 的警告仍然适用。条件测试没有任何重试,因此您正在失去 Cypress 的最大优势 - 减少测试中的脆弱性。