3 Different Ways to Count Elements in Cypress
There are three different ways to count the number of elements in Cypress. To get the exact length of elements, we can use .should('have.length')
:
Copied to clipboard!
cy.get('li').should('have.length', 4)
In case you don't know the exact length of the elements upfront, we can also check the minimum, and the maximum length of elements using the following assertions:
Copied to clipboard! Playground
// Checking for minimum lengths:
cy.get('li').should('have.length.at.least', 3)
// Checking for maximum lengths:
cy.get('li').should('have.length.of.at.most', 2)
Want to learn Cypress from end to end? Check out my Cypress course on Educative where I cover everything:
Resources:
π More Webtips
Master the Art of Frontend
- Access 100+ interactive lessons
- Unlimited access to hundreds of tutorials
- Prepare for technical interviews