【发布时间】:2022-06-11 10:33:09
【问题描述】:
我的测试用例中有以下 sn-p:
cy.get('item_here').should('not.exist');
当“item_here”确实存在时,cypress 可以给我一条自定义错误消息吗?
谢谢,
【问题讨论】:
标签: cypress
我的测试用例中有以下 sn-p:
cy.get('item_here').should('not.exist');
当“item_here”确实存在时,cypress 可以给我一条自定义错误消息吗?
谢谢,
【问题讨论】:
标签: cypress
您可以将日志消息链接到现有代码,并且仅当元素不存在时才会运行。
cy.get('item_here').should('not.exist')
.then(() => cy.log('no such element found')) // Note; this is an additional log
同时更改“成功”和“失败”消息很困难,因为赛普拉斯喜欢在任何失败或抛出错误时显示红色的 AssertionError 块。
你可以使用should()回调版本,但是请在里面使用expect(),否则你没有重试,
cy.get('item_here').should($el => {
expect($el, 'Cannot be found').to.not.exist // expect causes retry for 4 seconds
Cypress.log({
name: 'Missing',
message: 'Cannot be found'
})
})
【讨论】:
您可以在.should() 的回调函数中抛出自己的错误。
cy.get(".does-not-exist")
.should("not.exist")
.then(($el) => {
if ($el == null) {
throw new Error("Item does not exist in DOM");
}
});
【讨论】:
!== 替换为 >=。
cy.get('item_here') 将失败,.should() 将永远不会运行。您需要在回调中使用.should('not.exist') 或expect 来修改cy.get('item_here') 的行为