我希望能够单击复选框,并测试元素不再在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。


当前回答

使用.should('not.exist')来断言元素在DOM中不存在。


不要用not。可见的断言。它会错误地通过< 6.0,但现在正确地失败:

// for element that was removed from the DOM
// assertions below pass in < 6.0, but properly fail in 6.0+
.should('not.be.visible')
.should('not.contain', 'Text')

迁移文档:迁移到cypress -6-0

其他回答

您还可以同时使用get和contains来区分HTML元素。

<button type='button'>Text 1</button>
<button type='button'>Text 2</button>

假设你有两个不同文本的按钮,你想检查第一个按钮是否不存在,然后你可以使用;

cy.get('button').contains('Text 1').should('not.exist')

柏树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`) });
})

以下是对我有效的方法:

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]')
cy.get('[data-e2e="create-entity-field-relation-contact-name"]').should('not.exist');

可能会导致一些错误的结果,因为一些错误消息被隐藏了。也许用起来更好

.should('not.visible');

那样的话。

我会用:

cy.get (.check-box-sub-text)登陆(“not.be.visible”);

这比

cy.get (.check-box-sub-text)登陆(“not.exist”);

(元素可以出现在DOM中,但不可见display: none或不透明度:0)