Search code examples
testingautomationcypress

Cypress: Get all elements that have some specific attribute equal to any value


I am trying to get all elements that just have a specific attribute, doesn't matter what the attribute value is, and count the number of elements found.

for example get all attribute that has "row-index" attribute:

<div row-index="0" style="height: 47px; transform: translateY(0px);">aaa</div>
<div row-index="1" style="height: 47px; transform: translateY(0px);">bbb</div>
<div row-index="3" style="height: 47px; transform: translateY(0px);">ccc</div>

I have tried this code, that doesn't work:

cy.get('div[@row-index]').should('have.length', 3);

Solution

  • You can also directly use row-index for this:

    cy.get('[row-index]').should('have.length', 3);
    

    Another way to assert length:

    cy.get('[row-index]').its('length').should('eq', 3);