How to check not visible **or** not exists #24968
-
Hey folks! Is there any way I can make an assertion that will pass both in the case an element is not in the DOM and in the case it's not visible? If I do Can I somehow make an assertion that accepts both cases? Like, I saw this was asked before, in #17197 , but the accepted answer there is
I don't really know what that means. 😞 Can anyone point me in the right direction please? |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 2 replies
-
A cy.get('selector').should($el => {
const doesNotExist = $el.length == 0;
const isNotVisible = !$el.is("visible");
const doesNotExistOrIsNotVisible = doesNotExist || isNotVisible;
expect(doesNotExistOrIsNotVisible, "does not exist or is not visible").to.be
.true;
}) |
Beta Was this translation helpful? Give feedback.
-
This seems like a pretty common problem: why is there not a simpler tool (than dropping into jQuery) available for solving it? One vote for there being such a tool. P.S. Also this approach doesn't work if the element is gone from the page completely, eg.
Before you even get to the guts of your assertion, the query fails (because the element isn't there):
Again, Cypress really needs a tool to solve this problem. |
Beta Was this translation helpful? Give feedback.
A
.should(callback
function allows you work with the passed in DOM element to add more assertions, custom assertions, etc. For your question, you can do the following to check if your DOM element either does not exist or is not visible. Here is a working example.