Search code examples
cypress

Is there a way to check if an element has a class in cypress.io?


I don't want to test that it has the class like ...should('have.class', "some-class") I just want know if it does, and if doesn't then perform some action that gives it that class.

Basically, I want to see if the element has Mui-checked and if it doesn't them programmatically check it.


Solution

  • You can use the hasClass() jquery method for this:

    cy.get('selector').then(($ele) => {
      if ($ele.hasClass('foo')) {
        //Do something when you have the class
      } else {
        //Do something when you don't have the class
      }
    })