3 Different Ways to Count Elements in Cypress

3 Different Ways to Count Elements in Cypress

Ferenc Almasi β€’ 2022 August 19 β€’ Read time 1 min read
  • twitter
  • facebook

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)
Checking for exact match

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)
Checking the minimum and maximum length of elements

Want to learn Cypress from end to end? Check out my Cypress course on Educative where I cover everything:

Learn Cypress with Educative
3 Different Ways to Count Elements in Cypress
If you would like to see more webtips, follow @flowforfrank

Resources:

  • twitter
  • facebook
Did you find this page helpful?
πŸ“š More Webtips
Mentoring

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:

Courses

Recommended

This site uses cookies We use cookies to understand visitors and create a better experience for you. By clicking on "Accept", you accept its use. To find out more, please see our privacy policy.