Cypress:当“应该”断言失败时如何捕获错误

Nor*_*yan -2 javascript cypress

should当断言失败时我需要捕获错误。想象一下 .should('not.exist') 的第一个断言失败了(元素仍然可见)

我需要的示例(但这不适用于 Cypress 和 JS):

   this.containsInIframe('tr', assetName)
  .find('div.loader', { timeout: 100000 })
  .should('not.exist')
  .catch((error) => {
    this.reloadPage();
    this.containsInIframe('tr', assetName)
      .find('div.loader', { timeout: 100000 })
      .should('not.exist');
  });
Run Code Online (Sandbox Code Playgroud)

使用Cypress和JS如何处理这种情况?谁能提出解决这个问题的方法?

如何捕获 cypress 'should' 断言抛出的错误?

小智 5

捕获错误相当复杂,因为赛普拉斯并不完全按照这种方式运行。

如果您想重试(如捕获代码所示),该功能内置于 Cypress 运行程序中。

测试重试

it('retries the test', {retries: {runMode: 2, openMode: 1}}, function() {
  .. test code 
})
Run Code Online (Sandbox Code Playgroud)