How can I verify which class name in the element?

Viewed 122

I have element. And I need verify which class in this element and do something after that. How can I do it?

cy.get('element')
  .should('have.class', 'Enabled')
  .log('Enabled')
  or
  .log('Disabled')
1 Answers

Using a .then() function to yield the element found by cy.get(), and then using the JQuery function hasClass to determine if the yielded element has the Enabled class should work.

cy.get('element').then(($el) => {
  cy.log($el.hasClass('Enabled') ? 'Enabled' : 'Disabled');
  if ($el.hasClass('Enabled')) {
    // some code
  } else {
    // some other code
  }
})
Related