Bird
0
0

Identify the error in this Cypress test code:

medium📝 Debug Q14 of 15
Cypress - Assertions
Identify the error in this Cypress test code:
cy.get('.alert').should('be.visible').should('have.text', 'Error occurred')

Given the element <div class='alert' style='display:none'>Error occurred</div> is hidden.
AThe element selector is incorrect
BThe element is hidden, so 'be.visible' assertion fails
C'have.text' assertion syntax is wrong
DChaining assertions is not allowed in Cypress
Step-by-Step Solution
Solution:
  1. Step 1: Check element visibility

    The element has style='display:none', so it is hidden and not visible.
  2. Step 2: Understand assertion failure

    The should('be.visible') assertion will fail because the element is hidden, causing the test to fail before checking text.
  3. Final Answer:

    The element is hidden, so 'be.visible' assertion fails -> Option B
  4. Quick Check:

    Hidden element fails 'be.visible' [OK]
Quick Trick: Hidden elements fail 'be.visible' assertion [OK]
Common Mistakes:
  • Assuming 'have.text' runs even if visibility fails
  • Thinking chaining assertions is invalid
  • Ignoring element's CSS display property

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Cypress Quizzes