简体   繁体   中英

Cypress how to make an request on fail

I am trying to trigger an action when the test failed (which will send me an email).

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

But when i execute it i get this premise error:

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()

Is there a way to call a post request from a fail event?

Thank you

Try it with a plain fetch() . Note that runnable has circular references, so you may want to pluck out some parts for the message.

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

In my test this doesn't throw the nested promise error, and I can see the POST pending in the network tab so presume it's going to work with a kosha api in place.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM