What if you could catch bugs by confirming what <em>shouldn't</em> be there, automatically?
Why Negative assertions (not) in Cypress? - Purpose & Use Cases
Imagine you are checking a webpage manually to confirm that a button is not visible or a message does not appear. You have to carefully look at the page every time, which is tiring and easy to miss.
Manually verifying that something is not present is slow and error-prone. You might overlook subtle changes or forget to check every time, leading to bugs slipping through.
Negative assertions like not in Cypress let you automatically check that something does not exist or is not visible. This saves time and ensures your tests catch unwanted elements reliably.
if (!button.isVisible()) { console.log('Pass') } else { console.log('Fail') }
cy.get('button').should('not.be.visible')
It enables automated, reliable checks that certain elements or states are absent, preventing bugs before users see them.
For example, ensuring an error message does not show after a successful form submission keeps the user experience clean and trustworthy.
Manual checks for absence are slow and risky.
Negative assertions automate and secure these checks.
They help catch bugs that appear as unwanted elements or messages.
Practice
cy.get('button').should('not.exist') check for?Solution
Step 1: Understand the command
This assertion checks that the selected element does not exist in the DOM at all.should('not.exist')Step 2: Apply to the element
The commandbuttoncy.get('button').should('not.exist')verifies that nobuttonelement is present in the page.Final Answer:
It checks that the button is not present in the page DOM. -> Option CQuick Check:
should('not.exist') = element absent [OK]
- Confusing 'not.exist' with 'not.be.visible'
- Thinking it checks if element is disabled
- Assuming it checks element visibility
#modal is hidden?Solution
Step 1: Identify correct negative visibility assertion
In Cypress, to check if an element is hidden but still in the DOM, useshould('not.be.visible').Step 2: Check each option's syntax
cy.get('#modal').should('not.exist') checks for absence, not hidden. cy.get('#modal').should('be.hidden') uses invalid assertionbe.hidden. cy.get('#modal').should('not.visible') has incorrect syntaxnot.visible.Final Answer:
cy.get('#modal').should('not.be.visible') -> Option AQuick Check:
Hidden element = should('not.be.visible') [OK]
- Using 'not.exist' to check hidden instead of absent
- Writing incorrect assertion names like 'be.hidden'
- Missing 'be' in visibility assertion
cy.get('.alert').should('not.exist')
cy.get('.alert').should('not.be.visible')What will happen if the element
.alert is present but hidden?Solution
Step 1: Analyze
This assertion fails because the element exists in the DOM, even if hidden.should('not.exist')when element is present but hiddenStep 2: Analyze
This assertion passes because the element is present but not visible.should('not.be.visible')when element is hiddenFinal Answer:
The first assertion fails, the second passes. -> Option AQuick Check:
Existence vs visibility differ: exist fails, visibility passes [OK]
- Assuming 'not.exist' passes if element is hidden
- Confusing visibility with existence
- Thinking both assertions behave the same
cy.get('.popup').should('not.exist')But the test fails even though the popup is hidden. What is the likely problem?
Solution
Step 1: Understand difference between 'not.exist' and hidden elements
'not.exist' fails if element is present in DOM, even if hidden.Step 2: Apply to the popup element
The popup is hidden but still in DOM, so assertion fails because element exists.Final Answer:
The popup element exists but is hidden, so 'not.exist' fails. -> Option BQuick Check:
'not.exist' means element absent, hidden still exists [OK]
- Using 'not.exist' to check hidden elements
- Assuming hidden means not in DOM
- Ignoring difference between visibility and existence
div with class .notification is neither present nor visible on the page. Which Cypress code correctly checks both conditions?Solution
Step 1: Understand that if element does not exist, it cannot be visible
If the element does not exist in the DOM, it is automatically not visible.Step 2: Check which assertion covers both absence and visibility
Usingshould('not.exist')alone is enough to confirm element is absent and thus not visible.Final Answer:
cy.get('.notification').should('not.exist') -> Option DQuick Check:
Not existing means not visible too [OK]
- Chaining 'not.exist' with 'not.be.visible' unnecessarily
- Running two separate assertions causing flaky tests
- Using only 'not.be.visible' when element might exist
Ready to prove it?
Pass the test and this pattern moves unknown
