I am trying to get the result of the test (each scenario ) in After hook [Referring https://stackoverflow.com/questions/66153949/get-test-status-after-each-test-in-cypress]
Cypress.mocha.getRunner().suite.ctx.currentTest.state
I am getting error "Property 'mocha' does not exist on type Cypress & CyEventEmitter"
I am using Cypress,Typescript with cucumber ,but since mocha is default with cypress , cypress.mocha should work in after hook
Any idea in how to fix this issue?
tsconfig.json file
{
"compilerOptions": {
"target": "es2016",
"lib": ["es6"],
"module": "commonjs",
"types": ["cypress","cypress-xpath","node"],
"resolveJsonModule": true,
"esModuleInterop": true,
"strict": true,
"forceConsistentCasingInFileNames": true,
"skipLibCheck": true
}
}
Do I need change anything in tsconfig.json file ?
Update : I could solve the error(TypeScript Property 'mocha' does not exist on type Cypress & CyEventEmitter) by using [Referring link https://github.com/cypress-io/cypress/issues/2972#issuecomment-671779458] (Cypress as any).mocha.getRunner().suite...
But I am now facing Cannot read properties of undefined (reading 'state') Any help on this ?
Update: Cannot read properties of undefined (reading 'state') This resolved after replacing after hook with afterEach,Complete Resolution Provided in the Comment section
Note I am using cypress 12,node 18,cypress-cucumber-preproccesor 4.3
From Cypress 6.0, the cy.state() command has been removed from the public API and it is no longer available to access the internal state of Cypress at runtime. Cypress intentionally restricts access to its internal state to prevent test code from depending on or modifying internal implementation details. For Cypress 12 to get the title and status of test case I found the following code
afterEach(function() {
const name = Cypress.currentTest.title
cy.log(name)
const sceanrioStatus=(Cypress as any).mocha.getRunner().suite.ctx.currentTest.state
cy.log(sceanrioStatus)
});
In the above code Cypress.currentTest.title will fetch the name of the test case (in my case sceanrio name) and state will fetch the pass/fail status