Cypress: Test if element does not exist

隐身守侯 提交于 2019-12-08 22:25:42

问题


I want to be able to click on a check box and test that an element is no longer in the DOM in Cypress. Can someone suggest how you do it?

//This is the Test when the check box is clicked and the element is there
cy.get('[type="checkbox"]').click();
cy.get('.check-box-sub-text').contains('Some text in this div.')

I want to do the opposite of the test above. So when I click it again the div with the class should not be in the DOM.


回答1:


Well this seems to work, so it tells me I have some more to learn about .should()

cy.get('.check-box-sub-text').should('not.exist');



回答2:


cy.get(data-e2e="create-entity-field-relation-contact-name").should('not.exist')

might lead to some false results, as some error messages get hidden. It might be better to use

.should('not.visible') 

in that case.




回答3:


you can also search a for a text which is not supposed to exist:

cy.contains('test_invite_member@gmail.com').should('not.exist')

Here you have the result in Cypress: 0 matched elements




回答4:


Here's what worked for me:

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

I check that some <div data-cy="parent"> has no images inside. Regarding original question, you can set data-cy="something, i.e. child" attribute on inner nodes and use this assertion:

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



回答5:


You can also use below code

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

or

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

or

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


来源:https://stackoverflow.com/questions/48915773/cypress-test-if-element-does-not-exist

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!