2

I am looking for an option to get the name and the result (fail/pass) of each test to save this to an external file.

How can I get this information easily?

Currently, I can get only the title within the it() section with:

cy.log(this.test.title)

however, I am not able to get the result there.

It would be ideal to have this option from afterEach() section. So that in one place I can save the title and result.

I cannot do this.test.state because Cannot read property 'test' of undefined

I am not able to use Cypress.on('test:after:run', (test)) because this part of code is not starting. I have no idea why.

Thanks!

harmider
  • 373
  • 6
  • 18
  • 1
    How about using the [test reporters](https://docs.cypress.io/guides/tooling/reporters.html#Installed-locally) that Cypress allows? They generate XML files containing more details about the test and its results. Not exactly user-friendly, but you can parse the data and retrieve whatever it is you want to, after the fact – natn2323 Feb 11 '21 at 18:54
  • `Cypress.on('test:after:run')` not starting should be solvable. Could you post the code for that (and where it is). – Ackroydd Feb 11 '21 at 19:22

1 Answers1

1

Use the below snippet, it will work:

Cypress.mocha.getRunner().suite.ctx.currentTest.state 
  • it will tell you status as passed or failed
  • You only need to integrate mocha plugin with the cypress project.

You can the above scripts like shown below:

if (Cypress.mocha.getRunner().suite.ctx.currentTest.state === 'failed') {
            cy.log('Failed')
        } else {
            cy.log('Passed')
 }
Sudheer Singh
  • 555
  • 5
  • 10