How to capture scenario result in cypress-cucumber-preprocessor?

635 Views Asked by At

I am Migrating Protractor Cucumber framework to Cypress using cypress-cucumber-preprocessor I need to capture Scenario run result -pass or fail value also the scenario name

Earlier with cucumber,the result were available in the after hooks and I was using scenario.result.status to store the result refer below code

After(async function (scenario){
  let name: string = scenario.pickle.name;
  result = scenario.result.status;
});

I found "window.testState.currentScenario" in the web,but where does window come from ? I am getting error Cannot find name window. Reference https://github.com/badeball/cypress-cucumber-preprocessor/issues/285 Not finding similar implementation with cypress-cucumber-preprocessor, Is there any Solution to this?

Another solution given in the issue-285 was by @amitguptagwl was to add the code Cypress.env('currentScenario', scenario) to lib/createTestFromScenario.js in runTest() to get the current scenario detail But how to access result after this step?

2

There are 2 best solutions below

0
Sagarika On BEST ANSWER

After a lot of debugging on web, I found resolution for the problem statement,Sharing my code snippet here

afterEach(function() {
 const name = Cypress.currentTest.title
 cy.log(name)
 const sceanrioStatus=(Cypress as any).mocha.getRunner().suite.ctx.currentTest.state
 cy.log(sceanrioStatus)
});

Here Cypress.currentTest.title will fetch the scenario name and (Cypress as any).mocha .. state will fetch the scenario pass or fail result at run time (So Need not have to fetch this from JSON Report) This function will capture result after each scenario/test case run

4
Wandrille On

To get the result, you can check the following official documentation to export the result in a JSON file: https://github.com/badeball/cypress-cucumber-preprocessor/blob/master/docs/json-report.md.

In the JSON, you will retrieve all the scenarios, the results, the screenshots, ...

EDIT:

Otherwise, you can get the result after each Feature (not scenario), with:

on('after:spec', (spec, results) => {
   console.log(results)
})

in your cypress.config.{ts,js}. Warning, the flag experimentalInteractiveRunEvents should be activated. See https://docs.cypress.io/api/plugins/after-spec-api