'Conditional testing in cypress

I have a button that may or may not be disabled via a switch next to it. I know it's state, when the test gets to it, but I feel like I should be checking it anyway and switching it if it isn't in the state I want.

How can I write a conditional for this in a cypress test?

if (testSubject.getButton().is("disabled) {
  // do stuff
} else {
  // do other stuff
}


Solution 1:[1]

The general pattern for that is to use jQuery in your if statement,

cy.get('my-button')
  .then($button => {
    if ($button.is(':enabled')) {
      cy.wrap($button).click()
    }
  })

Be aware that using conditional testing like this can mask a regression in the app that allows a bug to slip through. It's ok if you have other tests covering it.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Fody