← Back to library
Cypressassertionsbeginnerv10+
Count elements on the page
Assert the exact number of matched elements with have.length, or read the count dynamically.
1it('product grid shows the correct number of cards', () => {
2 cy.visit('/products');
3
4 // have.length asserts the exact count; Cypress retries until it passes
5 cy.get('[data-testid="product-card"]').should('have.length', 12);
6
7 // Use .its('length') when the count drives further conditional logic
8 cy.get('[data-testid="product-card"]')
9 .its('length')
10 .should('be.gte', 1);
11});#count#have.length#length#elements