Home > Net >  cypress - assert something and then assert it again
cypress - assert something and then assert it again

Time:06-01

I'm trying to assert something on element 1 but i dont want cypress to display an error if the condition fails. instead, I want cypress to check something else and only if this second assertion fails I want cypress to throw a message.

//first assertion    
cy.get(`[data-cy="xxx"] > [data-cy="yyy"] > .x > .y`).should('have.css', 'color', 'rgb(227, 11, 11)');
//second assertion
cy.get(`element 2`).should('have.css', 'color', 'rgb(227, 11, 11)');

CodePudding user response:

You can do a conditional check for this, something like this:

cy.get('[data-cy="xxx"] > [data-cy="yyy"] > .x > .y')
  .invoke('css', 'color')
  .then((cssColor) => {
    if (cssColor == 'rgb(227, 11, 11)') {
      cy.log('Expected color is present')
    } else {
      //this will throw error if failed
      cy.get('element 2').should('have.css', 'color', 'rgb(227, 11, 11)')
    }
  })

CodePudding user response:

You can assert either one or the other element has the color value (since the condition is the same in both cases) by using a comma between the selectors

cy.get('[data-cy="xxx"] > [data-cy="yyy"] > .x > .y, element2')
  .should('have.css', 'color', 'rgb(227, 11, 11)')
  • Related