如何处理赛普拉斯错误?

How to handle cypress erros?

假设我有一个简单的测试:

/// <reference types="cypress" />

describe('Something important', () => {

  it('First test', () => {
    ...
  })

})

我需要做的是处理 First test 中发生的事情,我正在尝试类似的方法,但这种方式不起作用:


it('First test', () => {
    try {
      ...
    } catch (e) {
      ... // send email, for example
    }  
})

所以,在某种程度上,我需要处理测试中发生的任何事情。可能吗?像这样,但对于整个测试:

cy.get('button').contains('hello')
  .catch((err) => {
    // oh no the button wasn't found
    // (or something else failed)
    cy.get('somethingElse').click()
  })

这是您需要的吗Catching Test Failures

Cypress.on('fail', (error, runnable) => {
  debugger

  // we now have access to the err instance
  // and the mocha runnable this failed on

  throw error // throw error to have test still fail
})

这将在测试失败时触发,因此您可以执行电子邮件或其他操作。

但通常赛普拉斯希望您知道按钮就在那里。你不应该在页面上有太多不可预测的东西(或者不在页面上,视情况而定)。