1

I've finished writing my first Cypress test. Everything is good except I'm struggling to post the result data to a website. Because I want to send the result data and also if any errors occurs the result screenshot to our coworker telegram group.

For the last two days I've tried everything and couldn't find any solution.

I've tried those in my test script (cypress/integration/test.js);

Cypress.on('test:after:run', (test, runnable) => {

  console.log('test,runnable', test, runnable)

  const details = {
    projectKey: Cypress.env('zephyr-project-key'),
    testName: test.invocationDetails.relativeFile,
    status: test.status,
    error: runnable.err.message,
    retries: runnable.retries.length,
    duration: test.wallClockDuration,
    startTime: test.wallClockStartedAt
  }
  
  cy.request('POST', 'http://mywebsite.com/notify.php', { body: details })

  fetch('http://mywebsite.com/notify.php')
         
})

Also this didn't work (cypress/plugins/index.js);

module.exports = (on, config) => {
  // `on` is used to hook into various events Cypress emits
  // `config` is the resolved Cypress config
    on('after:run', (results) => {
        if (results) {
        // results will be undefined in interactive mode
         console.log(results.totalPassed, 'out of', results.totalTests, 'passed')

          fetch('http://mywebsite.com/notify.php');
     
         }
     })
}

Edit: This is day 3 and I still couldn't solve this. What I've seen from Cypress help page is that cy.task() calls do not fire in 'test:after:run' event block;

https://github.com/cypress-io/cypress/issues/4823

I've seen some telegram groups who can do what I'm trying to do. All I need is to be able to get the results and post it to my website.

Sebastian
  • 61
  • 7

1 Answers1

1

The third parameter to cy.request() is body, you don't have to wrap it.

Cypress.on('test:after:run', (test, runnable) => {

  const details = {
    projectKey: Cypress.env('zephyr-project-key'),
    testName: test.invocationDetails.relativeFile,
    status: test.status,
    error: runnable.err?.message,           // need err?.message if there is no error
    retries: runnable.retries.length,
    duration: test.wallClockDuration,
    startTime: test.wallClockStartedAt
  }
  
  cy.request('POST', 'http://mywebsite.com/notify.php', details)  // don't wrap details
    .then(res =>  expect(res.status).to.eq(201))                  // confirm result

})
Fody
  • 23,754
  • 3
  • 20
  • 37
  • Thank you very much for quick reply and helping @Fody This code also didn't work. What I've seen from Cypress help page is that cy.task() calls do not fire in 'test:after:run' event block https://github.com/cypress-io/cypress/issues/4823 So that's why I wanted to try "fetch" function. Do you have any information about this? – Sebastian Mar 13 '22 at 08:50
  • 1
    The `test:after:run` works (I tested it, my server received the data). The task code isn't needed (it may also work with revisions, but I didn't try it). Likely your server isn't accepting the POST. – Fody Mar 13 '22 at 08:55
  • 1
    `fetch()` may also work but you forgot to specify `POST` method and body details. – Fody Mar 13 '22 at 08:58
  • 1
    `cy.request()` has a `form:true` option that may be needed for your situation. See [HTML form submissions using form option](https://docs.cypress.io/api/commands/request#HTML-form-submissions-using-form-option) – Fody Mar 13 '22 at 09:09
  • Should I use cypress/plugins/index.js file or cypress/integration/test.js file? Which one are you using? – Sebastian Mar 13 '22 at 09:24
  • I'll try to use another server. Can I just ping a php file in my server with this POST code? So If I put an fwrite line in my php, then I'll check the text file and know it worked. – Sebastian Mar 13 '22 at 09:27
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/242888/discussion-between-sebastian-and-fody). – Sebastian Mar 13 '22 at 09:41
  • Can you help me – Sebastian Mar 14 '22 at 21:52