score:0

Accepted answer

you almost have it, but need to use .then() or a .should() after the cypress commands, which do not give you anything useful when you assigning to a variable (the first code block)

cy.get('myelement').should('have.css', 'color')
  .should(color => {
    const white = 'rgb(255, 255, 255)'
    const black = 'rgb(0,0,0)'
    const chartreuse = 'rgb(127, 255, 0)'
    expect(color).to.be.oneof([white, black, chartreuse])
  }

or

const white = 'rgb(255, 255, 255)'
const black = 'rgb(0,0,0)'
const chartreuse = 'rgb(127, 255, 0)'

cy.get('myelement')
  .should('have.css', 'color')
  .and('be.oneof', [white, black, chartreuse])

the second code block is assigning color value correctly, but the timing is wrong - the if()...else runs before the value is assigned. also use a .then() to correct the timing

let color
cy.get('myelement').then(($element) => {
  color = $element.css('color')
})

// other commands...

cy.then(() => {
  expect(color).to.be.oneof([white, black])
})

or assign the color value to an alias (but you also access the alias value from a .then()).


Related Query

More Query from same tag