-2

Is there a way to NOT abort a test run if an AssertionError appears?

My test case is dependent on CSS values and if I write a condition like the one below, cypress stops with an AssertionError:

let dialog = cy.get('#privacy_dialog')
if (dialog.should('have.css', 'display', 'block')) {
  // confirm the dialog if it is displayed
  cy.get('#dialog_btn').click()  
} else {
  // process with login, because css in this case is "display: none;"
  cy.get('#login_btn').click()
}

It also wouldn't work this way, because then I get another error TypeError $dialog.should is not a function:

cy.get('#privacy_dialog').then(($dialog) => {
  if ($dialog.should('have.css', 'display', 'block')) {
    cy.get('#dialog_btn').click()
  } else {
    cy.get('#login_btn').click()
  }
})
sunwarr10r
  • 4,420
  • 8
  • 54
  • 109
  • Cypress commands don't _return_ the value, they _yield_ it to the next thing in the chain. Did you read e.g. https://docs.cypress.io/guides/core-concepts/conditional-testing? – jonrsharpe Apr 08 '21 at 08:22
  • @jonrsharpe Thank you for the comment. I read it and that's why I tried the second approach, which also didnt work – sunwarr10r Apr 08 '21 at 08:23
  • Because you're still trying to use the result of a Cypress command as a condition - it's not going to return true or false. `.should('have.css', 'display')` would yield `'block'` for example, per https://docs.cypress.io/api/commands/should#Yields. – jonrsharpe Apr 08 '21 at 08:24

1 Answers1

3

One way is to use jquery to check the css property in the if condition:

cy.get('#privacy_dialog').then(($dialog) => {
    if ($dialog.css('display') == 'block') {
        cy.get('#dialog_btn').click()
    }
    else {
        cy.get('#login_btn').click()
    }
})
Alapan Das
  • 17,144
  • 3
  • 29
  • 52