Cypress, How to get the count

Viewed 9687

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?

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.

Related