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')
:
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:
// 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:
Rocket Launch Your Career
Speed up your learning progress with our mentorship program. Join as a mentee to unlock the full potential of Webtips and get a personalized learning experience by experts to master the following frontend technologies: