Skip to content

How to check not visible **or** not exists #24968

Discussion options

You must be logged in to vote

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.

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;
})

Replies: 2 comments 2 replies

Comment options

You must be logged in to vote
2 replies
@lgenzelis
Comment options

@angel-paella
Comment options

Answer selected by lgenzelis
Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
4 participants