Verifying that Element Should not Exist in Cypress

Verifying that Element Should not Exist in Cypress

Ferenc Almasi β€’ 2021 October 03 β€’ Read time 1 min read
  • twitter
  • facebook

Want to verify that an element should not exist in Cypress? You need to chain the should assertion off from cy.get command:

Copied to clipboard! Playground
// The element should not exist
cy.get('.element').should('not.exist');

// You can also check for invisibility:
cy.get('.element').should('not.be.visible');

// Using expect:
expect(Cypress.$('.element')).not.to.exist;

You can also verify visibility using not.be.visible, and you can use and expect statement too. In this case, however, you need to wrap the selector in Cypress.$ to create a jQuery element from it.

Note that when you want to verify if an element should exist, you only need to get the element. No need for a should assertion. If Cypress fails to find it, your test will fail too.

Copied to clipboard!
// When the element should exist, you only need a .get
cy.get('.element');

Want to learn Cypress from end to end? Check out my Cypress course on Educative where I cover everything:

Learn Cypress with Educative
How to verify that an element should not exist in Cypress
If you would like to see more webtips, follow @flowforfrank

Resources:

  • twitter
  • facebook
Did you find this page helpful?
πŸ“š More Webtips
Mentoring

Rocket Launch Your Career

Speed up your learning progress with our mentorship program. Join as a mentee to unlock the full potential of Webtips and get a personalized learning experience by experts to master the following frontend technologies:

Courses

Recommended

This site uses cookies We use cookies to understand visitors and create a better experience for you. By clicking on "Accept", you accept its use. To find out more, please see our privacy policy.