柏树-断言某事,然后再次断言



我试图在元素1上断言一些东西,但我不希望柏树在条件失败时显示错误。相反,我希望cypress检查其他东西,只有当第二个断言失败时,我希望cypress抛出一条消息。

//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)');

你可以做一个条件检查,像这样:

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

通过在选择器

之间使用逗号,可以断言其中一个或另一个元素具有颜色值(因为两种情况下的条件是相同的)
cy.get('[data-cy="xxx"] > [data-cy="yyy"] > .x > .y, element2')
.should('have.css', 'color', 'rgb(227, 11, 11)')

最新更新