如何在 Cypress 中获取某个元素而不声明它存在?
cy.get('.something')
有时我的元素可能不存在,但我不希望它无法通过测试。
我应该使用不同的命令吗?
您可以使用cy.$$('selector')同步查询元素(jquery)。
如果您希望在 cypress 命令之后发生这种情况,您将需要.then:
cy.visit('/')
cy.get('element-one').then(() => {
const $el2 = cy.$$('element-two')
if ($el2.length) {
// do this
} else {
// do that
}
})
Run Code Online (Sandbox Code Playgroud)