Bird
Raised Fist0
Cypresstesting~20 mins

Negative assertions (not) in Cypress - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Negative Assertions Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
What is the test result of this Cypress code?
Consider this Cypress test code snippet:
cy.get('#submit-button').should('not.be.disabled')

What will be the test result if the button is enabled?
Cypress
cy.get('#submit-button').should('not.be.disabled')
AThe test is skipped because 'not' is not a valid assertion.
BThe test fails because the button is enabled.
CThe test throws a syntax error due to incorrect assertion.
DThe test passes because the button is enabled (not disabled).
Attempts:
2 left
💡 Hint
Think about what 'not.be.disabled' means for an enabled button.
❓ assertion
intermediate
2:00remaining
Which assertion correctly verifies an element is NOT visible?
You want to check that an element with class '.popup' is not visible on the page. Which Cypress assertion is correct?
Acy.get('.popup').should('not.exist')
Bcy.get('.popup').should('be.hidden')
Ccy.get('.popup').should('not.be.visible')
Dcy.get('.popup').should('be.invisible')
Attempts:
2 left
💡 Hint
Focus on the correct Cypress assertion syntax for visibility.
🔧 Debug
advanced
2:00remaining
Why does this negative assertion fail unexpectedly?
Given the code:
cy.get('#login').should('not.have.class', 'active')

The test fails even though the element does not have the 'active' class. What is the likely cause?
Cypress
cy.get('#login').should('not.have.class', 'active')
AThe assertion is correct; the test fails due to a timing issue before the class is removed.
BThe element is detached from the DOM when the assertion runs, causing failure.
CThe element has multiple classes including 'active', so the assertion fails.
DThe 'not' keyword is not supported with 'have.class' in Cypress.
Attempts:
2 left
💡 Hint
Consider if the element's class changes after the assertion runs.
❓ locator
advanced
2:00remaining
Which locator is best for a negative assertion on a disabled button?
You want to assert that a button with text 'Submit' is NOT disabled. Which locator and assertion combination is best?
Acy.contains('button', 'Submit').should('not.be.disabled')
Bcy.get('button:disabled').contains('Submit').should('not.exist')
Ccy.get('button').contains('Submit').should('not.have.attr', 'disabled')
Dcy.get('button[disabled=false]').contains('Submit')
Attempts:
2 left
💡 Hint
Think about combining text locator with a negative assertion on disabled state.
❓ framework
expert
2:00remaining
How does Cypress handle negative assertions internally?
Which statement best describes how Cypress processes negative assertions like should('not.exist') or should('not.be.visible')?
ACypress converts negative assertions into positive ones by inverting the condition before checking once.
BCypress retries the assertion until the element disappears or becomes invisible within the default timeout.
CCypress immediately fails the test if the element exists or is visible at the first check.
DCypress disables retries for negative assertions to avoid false positives.
Attempts:
2 left
💡 Hint
Think about Cypress's retry-ability feature and how it applies to negative assertions.

Practice

(1/5)
1. What does the Cypress command cy.get('button').should('not.exist') check for?
easy
A. It checks that the button is disabled.
B. It checks that the button is visible on the page.
C. It checks that the button is not present in the page DOM.
D. It checks that the button is clickable.

Solution

  1. Step 1: Understand the command should('not.exist')

    This assertion checks that the selected element does not exist in the DOM at all.
  2. Step 2: Apply to the element button

    The command cy.get('button').should('not.exist') verifies that no button element is present in the page.
  3. Final Answer:

    It checks that the button is not present in the page DOM. -> Option C
  4. Quick Check:

    should('not.exist') = element absent [OK]
Hint: Use 'not.exist' to check element absence in DOM [OK]
Common Mistakes:
  • Confusing 'not.exist' with 'not.be.visible'
  • Thinking it checks if element is disabled
  • Assuming it checks element visibility
2. Which of the following is the correct Cypress syntax to assert that an element with id #modal is hidden?
easy
A. cy.get('#modal').should('not.be.visible')
B. cy.get('#modal').should('not.visible')
C. cy.get('#modal').should('be.hidden')
D. cy.get('#modal').should('not.exist')

Solution

  1. Step 1: Identify correct negative visibility assertion

    In Cypress, to check if an element is hidden but still in the DOM, use should('not.be.visible').
  2. 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 assertion be.hidden. cy.get('#modal').should('not.visible') has incorrect syntax not.visible.
  3. Final Answer:

    cy.get('#modal').should('not.be.visible') -> Option A
  4. Quick Check:

    Hidden element = should('not.be.visible') [OK]
Hint: Use 'not.be.visible' to check hidden elements [OK]
Common Mistakes:
  • Using 'not.exist' to check hidden instead of absent
  • Writing incorrect assertion names like 'be.hidden'
  • Missing 'be' in visibility assertion
3. Consider this Cypress code snippet:
cy.get('.alert').should('not.exist')
cy.get('.alert').should('not.be.visible')

What will happen if the element .alert is present but hidden?
medium
A. The first assertion fails, the second passes.
B. The first assertion passes, the second fails.
C. Both assertions pass.
D. Both assertions fail.

Solution

  1. Step 1: Analyze should('not.exist') when element is present but hidden

    This assertion fails because the element exists in the DOM, even if hidden.
  2. Step 2: Analyze should('not.be.visible') when element is hidden

    This assertion passes because the element is present but not visible.
  3. Final Answer:

    The first assertion fails, the second passes. -> Option A
  4. Quick Check:

    Existence vs visibility differ: exist fails, visibility passes [OK]
Hint: Existence and visibility are different checks [OK]
Common Mistakes:
  • Assuming 'not.exist' passes if element is hidden
  • Confusing visibility with existence
  • Thinking both assertions behave the same
4. You wrote this Cypress test:
cy.get('.popup').should('not.exist')

But the test fails even though the popup is hidden. What is the likely problem?
medium
A. The selector '.popup' is incorrect.
B. The popup element exists but is hidden, so 'not.exist' fails.
C. The test should use 'should('not.be.visible')' instead.
D. Cypress cannot check hidden elements.

Solution

  1. Step 1: Understand difference between 'not.exist' and hidden elements

    'not.exist' fails if element is present in DOM, even if hidden.
  2. Step 2: Apply to the popup element

    The popup is hidden but still in DOM, so assertion fails because element exists.
  3. Final Answer:

    The popup element exists but is hidden, so 'not.exist' fails. -> Option B
  4. Quick Check:

    'not.exist' means element absent, hidden still exists [OK]
Hint: Hidden elements still exist; use 'not.be.visible' for hidden check [OK]
Common Mistakes:
  • Using 'not.exist' to check hidden elements
  • Assuming hidden means not in DOM
  • Ignoring difference between visibility and existence
5. You want to verify that a div with class .notification is neither present nor visible on the page. Which Cypress code correctly checks both conditions?
hard
A. cy.get('.notification').should('not.exist').and('not.be.visible')
B. cy.get('.notification').should('not.exist'); cy.get('.notification').should('not.be.visible')
C. cy.get('.notification').should('not.be.visible')
D. cy.get('.notification').should('not.exist')

Solution

  1. 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.
  2. Step 2: Check which assertion covers both absence and visibility

    Using should('not.exist') alone is enough to confirm element is absent and thus not visible.
  3. Final Answer:

    cy.get('.notification').should('not.exist') -> Option D
  4. Quick Check:

    Not existing means not visible too [OK]
Hint: Check 'not.exist' alone to confirm absence and invisibility [OK]
Common Mistakes:
  • Chaining 'not.exist' with 'not.be.visible' unnecessarily
  • Running two separate assertions causing flaky tests
  • Using only 'not.be.visible' when element might exist
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard