Recall & Review
beginner
What does a length assertion check in Cypress?
It checks how many elements match a selector or how many items are in a list or array on the page.
Click to reveal answer
beginner
How do you write a length assertion in Cypress to check if a list has exactly 3 items?
Use
cy.get('selector').should('have.length', 3) to check the list has 3 elements.Click to reveal answer
intermediate
Why is it better to use length assertions instead of checking text content for counting elements?
Length assertions directly check the number of elements, making tests clearer and less fragile than relying on text content.
Click to reveal answer
beginner
What happens if the length assertion fails in Cypress?
The test fails and Cypress shows an error message explaining the expected and actual number of elements found.
Click to reveal answer
intermediate
Can length assertions be combined with other assertions in Cypress?
Yes, you can chain length assertions with others like
should('be.visible') to check both count and visibility.Click to reveal answer
Which Cypress command checks if a list has exactly 5 items?
✗ Incorrect
The correct syntax to check the number of elements is
should('have.length', number).What does
cy.get('.items').should('have.length.greaterThan', 2) check?✗ Incorrect
The assertion checks that the number of matched elements is greater than 2.
If a length assertion fails, what will Cypress do?
✗ Incorrect
Cypress retries assertions until they pass or timeout, then fails the test if the condition is not met.
Which is a valid way to assert a list has at least 1 element in Cypress?
✗ Incorrect
Cypress supports
have.length.greaterThan to check length is more than a value.Why might you prefer
should('have.length', n) over counting elements manually?✗ Incorrect
Using
should('have.length', n) is simple and directly checks element count without extra code.Explain how to use length assertions in Cypress and why they are useful.
Think about counting items on a webpage.
You got /4 concepts.
Describe what happens when a length assertion fails during a Cypress test run.
Consider Cypress's retry behavior and reporting.
You got /4 concepts.