0
0
Cypresstesting~20 mins

Length assertions in Cypress - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Length Assertion Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
assertion
intermediate
2:00remaining
Verify element count with correct length assertion
You want to check that a list on the page has exactly 5 items using Cypress. Which assertion correctly verifies this?
Cypress
cy.get('.item-list > li').should('have.length', 5);
Acy.get('.item-list > li').should('have.length', 5);
Bcy.get('.item-list > li').should('have.length.greaterThan', 5);
Ccy.get('.item-list > li').should('length', 5);
Dcy.get('.item-list > li').should('have.lengthOf', 5);
Attempts:
2 left
💡 Hint
Use the correct Cypress assertion syntax for exact length checks.
Predict Output
intermediate
2:00remaining
Output of length assertion failure message
What will Cypress output if the following code runs but the page has only 3 items instead of 5?
Cypress
cy.get('.todo-item').should('have.length', 5);
ATest passes silently with no errors
BAssertionError: expected 3 to equal 5
CSyntaxError: unexpected token 'length'
DTypeError: cannot read property 'length' of undefined
Attempts:
2 left
💡 Hint
Think about what happens when the actual count does not match the expected count.
locator
advanced
2:00remaining
Best locator for length assertion on dynamic list
You want to assert the length of a dynamic list where items have a class that changes but all share a data attribute data-test='list-item'. Which locator is best for stable length assertions?
Acy.get('[data-test="list-item"]').should('have.length', 4);
Bcy.get('.dynamic-class').should('have.length', 4);
Ccy.get('li').should('have.length', 4);
Dcy.get('#list > div').should('have.length', 4);
Attempts:
2 left
💡 Hint
Use stable attributes that do not change dynamically.
🔧 Debug
advanced
2:00remaining
Debug failing length assertion in Cypress test
A test fails with this code:
cy.get('.menu-item').should('have.length', 3);
The error says actual length is 2. What is the most likely cause?
ACypress cannot find elements because the page is not loaded.
BThe assertion syntax is incorrect and causes a silent failure.
CThe page has only 2 elements with class 'menu-item' at test time.
DThe selector '.menu-item' is invalid and matches no elements.
Attempts:
2 left
💡 Hint
Check the actual number of elements on the page when the test runs.
framework
expert
2:00remaining
Using length assertion with chained commands in Cypress
Consider this Cypress code:
cy.get('.container').find('.card').should('have.length', 4);
What does this assertion verify?
AIt checks that there are 4 '.container' elements and 4 '.card' elements separately.
BIt checks that inside each '.container' element there are 4 '.card' elements.
CIt checks that there are 4 '.container' elements, each containing at least one '.card'.
DIt checks that the total number of '.card' elements inside all '.container' elements combined is 4.
Attempts:
2 left
💡 Hint
Remember how Cypress chains commands and aggregates matched elements.