If an element is not actionable on the page (in this case, covered by another element) and you try to click it, Cypress will show an error like this:
CypressError: Timed out retrying: cy.click() failed because this element:
<span>...</span>
is being covered by another element:
Great! But is there any way to assert that this is the case, aka that the element cannot be clicked?
This doesn't work:
should.not.exist
- the element does existshould.be.disabled
- the element is not disabledshould.not.be.visible
- the element is visible (just covered by another, transparent element)- using
cy.on('uncaught:exception', ...)
, since this is not an exception
See the Cypress tests at click_spec.coffee.
Simplest would be to mimic this test, even though docs recommend only using
cy.on('fail')
for debugging.This is similar to a unit test using
expect().to.throw()
to check that an exception occurs as expected so I feel the pattern is justified here.To be thorough, I would include a call to
click({force: true})
.As a custom command
I'm not sure how to make the chai extension you asked for, but the logic could be wrapped in a custom command
/cypress/support/index.js
/cypress/integration/myTest.spec.js
Note
I was expecting
done()
to time out when the premise of the test (i.e. that the button is covered) is false.This does not happen (reason unknown), but by chaining
.then()
off the 2nd click allowsdone()
to be called with an error message. Thethen()
callback will only be called if the click succeeds, otherwise thecy.once('fail')
callback handles click failure (as per Cypress' own test).