我希望能够单击复选框,并测试元素不再在Cypress的DOM中。有人能建议你怎么做吗?

// This is the Test when the checkbox is clicked and the element is there

cy.get('[type="checkbox"]').click();
cy.get('.check-box-sub-text').contains('Some text in this div.')

我想做与上面的测试相反的事情。

所以当我再次点击它的div类复选框子文本不应该在DOM。


当前回答

如果没有元素,我们可以使用简单的行:

cy。get (' [type = "复选框“]”)。should(“not exist。”)

其他回答

您也可以使用下面的代码

expect(opportunitynametext.include("Addon")).to.be.false

or

should('be.not.be.visible')

or

should('have.attr','minlength','2')

柏树6。x +迁移

根据赛普拉斯关于存在的文件

非常流行的尝试,有点天真,将工作,直到它不起作用,然后你将不得不重新编写它……一次又一次……

// retry until loading spinner no longer exists
cy.get('#loading').should('not.exist')

这并不能真正解决大多数人想要的标题问题。

这适用于它被删除的情况。但如果你想让它永远不存在……它将重试,直到它消失。

但是,如果您想测试元素在我们的例子中是否不存在。

是的lol。这是你真正想要的,除非你只是想再头疼一天。

// Goes through all the like elements, and says this object doesn't exist ever
cy.get(`img[src]`)
  .then(($imageSection) => {
    $imageSection.map((x, i) => { expect($imageSection[x].getAttribute('src')).to.not.equal(`${Cypress.config().baseUrl}/assets/images/imageName.jpg`) });
})

在我的情况下,Cypress是如此之快,简单的。应该('not.be.visible')正在通过测试,之后,加载器出现和测试失败。

我成功地做到了:

cy.get('.loader__wrapper')
  .should('be.visible')
cy.get('.loader__wrapper', { timeout: 10000 })
  .should('not.be.visible')

当应用程序加载超过4秒时,最好将超时设置为10秒。

柏树中没有试接流

在java-selenium中,我们通常添加NoSuchElementException并执行case。如果UI没有显示某些基于角色的访问情况下的元素。

以下是对我有效的方法:

cy.get('[data-cy=parent]').should('not.have.descendants', 'img')

我检查一些<div data-cy="parent">里面没有图像。 对于原始问题,你可以在内部节点上设置data-cy="something, i.e. child"属性,并使用以下断言:

cy.get('[data-cy=parent]').should('not.have.descendants', '[data-cy=child]')