Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cypress, How to get the count

Tags:

cypress

I am trying to find the count of object in the webpage using Cypress and then validate that total number of records is correct. The data keeps changing so I cant hardcode it.

Is there a way to get the number?

like image 622
Ajay Avatar asked Oct 18 '25 14:10

Ajay


1 Answers

To get count of elements, you can use below code.

let countOfElements = 0;
cy.get(".element-selector").then($elements => {
    countOfElements = $elements.length;
  });

If you want to do verify the count of elements, you can do it like below.

// retry until we find 5 matching <.element-selector>
cy.get('.element-selector').should('have.length', 5)

You can check more about it in Cypress Docs.

like image 116
coder_shanks Avatar answered Oct 22 '25 07:10

coder_shanks