Bird
0
0

Why does the assertion cy.get('.list').should('have.length', 0) sometimes pass even if the elements are not visible?

hard📝 Conceptual Q10 of 15
Cypress - Assertions
Why does the assertion cy.get('.list').should('have.length', 0) sometimes pass even if the elements are not visible?
ABecause the assertion checks for text length, not element count.
BBecause Cypress only counts visible elements by default.
CBecause the selector '.list' is invalid and returns zero elements.
DBecause <code>cy.get()</code> selects elements regardless of visibility, so length counts all matching elements.
Step-by-Step Solution
Solution:
  1. Step 1: Understand how cy.get() works

    It selects all elements matching the selector, visible or not.
  2. Step 2: Understand length assertion behavior

    The length counts all matched elements, so if none exist, length is 0 regardless of visibility.
  3. Final Answer:

    Because cy.get() selects elements regardless of visibility, so length counts all matching elements. -> Option D
  4. Quick Check:

    cy.get() counts all matched elements [OK]
Quick Trick: cy.get counts all elements, visible or hidden [OK]
Common Mistakes:
  • Assuming cy.get only selects visible elements
  • Confusing element count with text length
  • Thinking invalid selectors cause length 0 always

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Cypress Quizzes