https://cypress.io logo
check if a button is enabled
# general-chat
c
how to check if a button is enabled in cypress?
t
Copy code
js
cy.get('button')
  .should('be.enabled')
Here is a list of chai assertions you can use with cypress (like 'be.enabled'): https://docs.cypress.io/guides/references/assertions#Chai-jQuery
g
You can always find examples like this by searching https://glebbahmutov.com/cypress-examples/
c
cy.get("button") .contains("Create ID") .then(($clickable) => { if ($clickable.prop("disabled")) return; });
won't this help?
a call to button named create id and to exit if button is not enabled
g
help to do what? and I don't think it does much 🙂 Please consult my examples from https://glebbahmutov.com/cypress-examples/ - checking if a button is enabled is built into Chai-jQuery, so you should be good there
f
the simple should('be.enabled') assertion should give you what you are looking for, if that is not the case, and I've bumped into this one more than once because of an unnecessary complex DOM or bad page refresh. I had to check for the opossite doing something like should('not.have.class', 'disabled') it is not the same, but depending on the case it may be useful
c
actually the problem is i don't want the test to fail.if the button is not enabled the test fails.how do i avoid it or exit the test case
any idea?
g
why do you check / use an assertion if you don't want it to fail?
c
the code is designed to click a button to perform some task but if the button is not enabled for some reason the task fails and hence the test
i just don't want that to happen
cypress tells it couldn't find the element and fails the test
can't i skip the test in such a scenario?
g
look up conditional testing https://cypress.tips/search BUT in general it is anti-pattern
24 Views