stumped
stumped

Reputation: 3293

Get the length of a class list on cypress?

Would there be a way to test for how many classes are within an element? I see methods for being able to test if an element has a class and testing length. Is there a way to combine these two together?

Upvotes: 3

Views: 1507

Answers (2)

Yevhen Laichenkov
Yevhen Laichenkov

Reputation: 8672

Try something like this:

  cy.get('button')
    .invoke('attr', 'class')
    .then(classNames => classNames.split(' '))
    .should('have.length', 2);
  });

Upvotes: 5

Mr. J.
Mr. J.

Reputation: 3741

If you can provide a part of the HTML I can customise the code for you, but it should be something like this:

cy.get(<ELEMENT>)
  .find('class')
  .should('have.length', <NUMBER>)

This way you look for the , then look for the element 'class' within it. And verify that it present of times.

Upvotes: 0

Related Questions