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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With