How to get the description entered in describe block? (typescript)

Viewed 626

I am using cypress with typescript for my current project, I am in a situation to get the name of a saved screenshot. the screenshot name is automatically saved by cypress which starts with the description given in the describe block.

for example

describe('Admin Portal', () => {
  it('Login Test', () => {

  });
});

the screenshot will be saved as 'Admin Portal login -- Login Test (failed).png'

Cypress.on('test:after:run', (test, runnable) => {
if (test.state === 'failed') {
    const screenshotFileName = `${test.title} (failed).png`
    addContext({ test }, `assets/${Cypress.spec.name}/${screenshotFileName}`)
}

the above line of code is actually retrieving the name defined in the It block, how to get the name defined in the describe block??

2 Answers

Try something like this:

Cypress.on('test:after:run', (test, runnable) => {
if (test.state === 'failed') {
    const screenshotFileName = `${Cypress.config('screenshotsFolder')}/${Cypress.spec.name}/${runnable.parent.title} -- ${test.title} (failed).png`
    addContext({ test }, screenshotFileName)
}

})

I have tried console.log("=======>>>", Cypress.mocha.getRunner().suite.title) in the beforeEach() section and it is displaying me the suite title name in the console.log(). So can you add Cypress.mocha.getRunner().suite.title and try the below code and let me know if it is working:

Cypress.on('test:after:run', (test, runnable) => {
if (test.state === 'failed') {
    const screenshotFileName = `Cypress.mocha.getRunner().suite.title (failed).png`
    addContext({ test }, `assets/${Cypress.spec.name}/${screenshotFileName}`)
}
})

enter image description here

Related