Bird
0
0

You want to test a dynamic list where items can be added or removed. Which Cypress code correctly asserts that the list has at least 2 items but no more than 5 items?

hard📝 Application Q15 of 15
Cypress - Assertions
You want to test a dynamic list where items can be added or removed. Which Cypress code correctly asserts that the list has at least 2 items but no more than 5 items?
Acy.get('.list-item').should('have.length.gte', 2).and('have.length.lte', 5)
Bcy.get('.list-item').should('have.length', 2, 5)
Ccy.get('.list-item').should('have.length').within(2, 5)
Dcy.get('.list-item').should('have.lengthBetween', 2, 5)
Step-by-Step Solution
Solution:
  1. Step 1: Understand Cypress length assertions

    Cypress supports chaining assertions like should('have.length.gte', 2) for "greater than or equal" and should('have.length.lte', 5) for "less than or equal".
  2. Step 2: Evaluate each option

    cy.get('.list-item').should('have.length.gte', 2).and('have.length.lte', 5) correctly chains two assertions for minimum and maximum length. Options B, C, and D use invalid or non-existent syntax.
  3. Final Answer:

    cy.get('.list-item').should('have.length.gte', 2).and('have.length.lte', 5) -> Option A
  4. Quick Check:

    Use chained length.gte and length.lte for range checks [OK]
Quick Trick: Chain should('have.length.gte') and should('have.length.lte') for ranges [OK]
Common Mistakes:
  • Using invalid assertion methods
  • Trying to pass multiple lengths in one assertion
  • Assuming 'lengthBetween' exists

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Cypress Quizzes