question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Assertion should('not.be.visible') doesn't work as expected

See original GitHub issue

Current behavior

Assertion cy.get('[data-cy=an_invisible_button').should('not.be.visible'); fails with Timed out retrying: Expected to find element: [data-cy=an_invisible_button], but never found it.

Desired behavior

The assertion should be pass, because the invisible button is really not visible.

Test code to reproduce

cy.get(‘[data-cy=an_invisible_button’).should(‘not.be.visible’);

Versions

Bug was visible in Cypress version 6.0.0 Last known working Cypress Verion: 5.1.0 (Only tested because I updated to 6.0.0 from that version)

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:5
  • Comments:10 (3 by maintainers)

github_iconTop GitHub Comments

26reactions
jennifer-shehanecommented, Apr 26, 2021

Both assertions still exist in Cypress’s current version. You need to be specific about what you are asserting about an element.

  • Use .should('not.be.visible') for elements that exist in the DOM but would not be visible to a fully sighted person.
  • Use .should('not.exist') for elements that do not exist in the DOM, regardless of their visibility.
7reactions
jennifer-shehanecommented, Nov 27, 2020

Yes, this is expected behavior as of 6.0. Please see our Migration Guide for a detailed explanation. https://on.cypress.io/migration-guide

You’ll want to change your assert to .should(‘not.exist’) if you want to ensure it’s not in the DOM.

We understand this may be cumbersome to change but we think it’s better than having tests potentially falsely pass. We found a misspelled selector query in our own tests when updating even where it wasn’t testing what we thought.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Cypress should not.exist or not.be.visible - Stack Overflow
Hey, the custom command sounds like a good solution. Much easier than the Chai assertion, for sure. I do know - in this...
Read more >
should | Cypress Documentation
Create an assertion. Assertions are automatically retried until they pass or time out. An alias of .and() Note: .should() assumes you are already....
Read more >
Cypress Tip: Don't Overuse the Visibility Assertion - iheartjs
First, it's unnecessary because of two implicit behaviors of Cypress: actionability assertions and command retry-ability. Cypress will not ...
Read more >
Verifying that Element Should not Exist in Cypress - Webtips
Want to verify that an element should not exist in Cypress? You need to chain the should assertion off from cy.get command:.
Read more >
Assertions | Cypress examples (v9.5.0) - Gleb Bahmutov
The callback will be retried, but the assertions will not be shown as nicely in ... Load. The command below fails because the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found