Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cypress: Test if element does not exist

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.

like image 632
Maccurt Avatar asked Feb 21 '18 21:02

Maccurt


2 Answers

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'); 
like image 88
Maccurt Avatar answered Sep 30 '22 11:09

Maccurt


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

cy.contains('[email protected]').should('not.exist') 

Here you have the result in Cypress: 0 matched elements

enter image description here

documentation: https://docs.cypress.io/guides/references/assertions.html#Existence

like image 45
Alan Avatar answered Sep 30 '22 13:09

Alan