How to assert not clickable? #21150
-
I have an element covered by another element. How can I assert that it's not clickable? <div id="container" style="position:relative">
<button id="button-1" style="position:absolute">
Button underneath
</button>
<button id="button-2" style="position:relative">
Button that covers the other button
</button>
</div> // What I want to do
cy.get("#button-1").should("not.be.clickable");
// Here I expect a failure - works as expected!
// "cy.click() failed because this element: <button ...> is being covered by another element: ..."
cy.get("#button-1").click();
// I had hoped that #button-1 would "not.be.visible", but it is "visible"
cy.get("#button-1").should("not.be.visible"); Is there a way to assert that an element is not clickable, that it is being covered, or any other way to check that a |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 1 reply
-
According to this answer on stackoverflow, you'll want listen to the
|
Beta Was this translation helpful? Give feedback.
-
Minimal example of a Cypress command: Cypress.Commands.add("shouldNotBeActionable", { prevSubject: "element" }, (subject, done) => {
cy.once("fail", (err) => {
expect(err.message).to.include("`cy.click()` failed because this element");
expect(err.message).to.include("is being covered by another element");
done();
});
cy.wrap(subject).click(position, { timeout: 100 }).then(() =>
done(new Error("Expected element NOT to be clickable, but click() succeeded")));
});
declare global {
namespace Cypress {
interface Chainable {
shouldNotBeActionable(done: Mocha.Done): Chainable<Element>;
}
}
} Example usage: it("should not be clickable", (done) => {
cy.get("#button-1").shouldNotBeActionable(done);
});
it("should not be clickable on the right side", (done) => {
cy.get("#button-1").shouldNotBeActionable(done, {
position: "right",
});
}); Cypress command written in TypeScript: declare global {
namespace Cypress {
interface Chainable {
/**
* Assert that the subject is not clickable.
*/
shouldNotBeActionable(
/** The Mocha `done` callback. */
done: Mocha.Done,
/** Options passed to `.click()`. By default, the timeout is 100 ms. */
clickOptions?: Partial<Cypress.ClickOptions> & {
position?: Cypress.PositionType;
}
): Chainable<Element>;
}
}
}
Cypress.Commands.add(
"shouldNotBeActionable",
{ prevSubject: "element" },
(subject, done, { position, timeout = 100, ...clickOptions } = {}) => {
cy.once("fail", (err) => {
expect(err.message).to.include("`cy.click()` failed because this element");
expect(err.message).to.include("is being covered by another element");
done();
});
const chainable = position
? cy.wrap(subject).click(position, { timeout, ...clickOptions })
: cy.wrap(subject).click({ timeout, ...clickOptions });
chainable.then(() =>
done(new Error("Expected element NOT to be clickable, but click() succeeded")));
}
); |
Beta Was this translation helpful? Give feedback.
Minimal example of a Cypress command:
Example usage: