如何解决如何检查元素是否存在于 Cypress 的 DOM 中?
我想知道如何检查 Cypress 中网页的 DOM 中是否存在某个元素。
Selenium 中的这段代码在 Cypress 中的等价物是什么:
Boolean Display = driver.findElement(By.xpath("locator")).isDisplayed();
解决方法
要使用 xpath 定位器进行查询,请安装 cypress-xpath 扩展。
使用 npm 安装npm install -D cypress-xpath
使用 Yarn 安装yarn add cypress-xpath --dev
在测试中
cy.xpath(locator) // driver.findElement(By.xpath("locator"))
还要添加可见性检查,
cy.xpath(locator) // driver.findElement(By.xpath("locator"))
.should('be.visible') // isDisplayed()
或
cy.xpath(locator) // driver.findElement(By.xpath("locator"))
.should('not.be.hidden') // isDisplayed()
,
1.检查元素是否存在于DOM中:
cy.get(selector).should('exist')
2.检查元素在DOM中不存在:
cy.get(selector).should('not.exist')
3.检查元素是否可见:
cy.get(selector).should('be.visible')
4.检查元素不可见:
cy.get(selector).should('not.be.visible')
5.使用 JQuery:
cy.get('body').then(($body) => {
if ($body.find(selector).length > 0) {
//element exists do something
}
})
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。