Bird
0
0

You want to verify that a list contains exactly 5 visible items with class task. Which Cypress code correctly asserts this?

hard📝 Application Q8 of 15
Cypress - Assertions
You want to verify that a list contains exactly 5 visible items with class task. Which Cypress code correctly asserts this?
Acy.get('.task:visible').should('have.length', 5)
Bcy.get('.task').filter(':visible').should('have.length', 5)
Ccy.get('.task').should('have.length', 5).and('be.visible')
Dcy.get('.task').should('contain.length', 5).filter(':visible')
Step-by-Step Solution
Solution:
  1. Step 1: Understand how to filter visible elements

    Using filter(':visible') filters elements to only visible ones.
  2. Step 2: Check assertion for exact count

    Then should('have.length', 5) asserts exactly 5 visible elements.
  3. Step 3: Evaluate other options

    A uses potentially unreliable selector syntax with :visible; C checks total length before visibility assuming no hidden elements; D uses invalid assertion method.
  4. Final Answer:

    cy.get('.task').filter(':visible').should('have.length', 5) -> Option B
  5. Quick Check:

    Filter visible then assert length = B [OK]
Quick Trick: Filter visible elements before asserting count [OK]
Common Mistakes:
  • Using invalid selectors with :visible
  • Asserting length before filtering visibility
  • Using wrong assertion methods

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Cypress Quizzes