Cypress 如何在失败时发出请求

Blu*_*ero 5 events request cypress

我试图在测试失败时触发一个操作(这将向我发送一封电子邮件)。

Cypress.on('fail', (error, runnable) => {
cy.request('POST', 'https://mysite/api/', { action: 'cypress', error : error, runnable: runnable })
    throw error
})

describe('Test https://xxx/', function() {
    it('Visits the Page', function() {
        cy.visit('https://xxx/yyy/')
        .location('pathname').should('eq', '/yyy/thank-you')
    })
})
Run Code Online (Sandbox Code Playgroud)

但是当我执行它时,我得到了这个前提错误:

CypressError: Cypress detected that you returned a promise from a command while also invoking one or more cy commands in that promise.

The command that returned the promise was:

  > cy.location()

The cy command you invoked inside the promise was:

  > cy.request()
Run Code Online (Sandbox Code Playgroud)

有没有办法从失败事件中调用发布请求?

谢谢

Ric*_*sen 2

尝试使用普通的fetch(). 请注意,它runnable具有循环引用,因此您可能需要删除消息的某些部分。

Cypress.on('fail', (error, runnable) => {
  fetch('https://mysite/api/', {
    method: "POST",
    headers: {
      "Content-Type": "application/json",
    },
    body: JSON.stringify(runnable.title), // body data type must match "Content-Type" header
  });
  throw error
})
Run Code Online (Sandbox Code Playgroud)

在我的测试中,这不会引发嵌套的 Promise 错误,并且我可以在网络选项卡中看到 POST 待处理,因此假设它将与 kosha api 一起使用。