Cypress - get element without assertion

Viewed 1928

How do I get an element in Cypress without it asserting that it is present?

cy.get('.something')

Sometimes my element might not be there and I don't want it to fail the test.

Is there a different command I should be using?

2 Answers

You can use cy.$$('selector') to synchronously query for an element (jquery).

If you want this to happen after a cypress command, you'll need a .then:

cy.visit('/')
cy.get('element-one').then(() => {
  const $el2 = cy.$$('element-two')
  if ($el2.length) {
    // do this
  } else {
    // do that
  }
})
Related