Shelex / cypress-allure-plugin

cypress plugin to use allure reporter api in tests
https://shelex.github.io/cypress-allure-plugin-example/
Apache License 2.0
159 stars 44 forks source link

Screenshots taken in a afterEach hook are not being displayed in allure report #26

Closed rodolforodriguess closed 3 years ago

rodolforodriguess commented 4 years ago

Hey guys,

My aim is to see screenshots for each test. Well, my code takes the screenshot in a afterEach hook and then I could see the .png added to the allure-results folder as shown in the image below.

image

The allure report opens nicely but the screenshot is not displayed although there is a kind of a section left to that. See below an image of the report:

image

So I tried a workaround by using attachment method of Allure API in order to add it programatically. However, I didn't have any success.

So, to summarize, it's seems attachment method of Allure API is not working properly.

I added the exact name of the screenshot generated by cypress as cypress documentation: https://docs.cypress.io/api/commands/screenshot.html#Notes which worked in a mochawesome report.

Here is the code which I put in index.js under support folder.

import './commands'
import './login.js'
//import addContext from 'mochawesome/addContext'
import '@shelex/cypress-allure-plugin'

Cypress.on('test:after:run', (test, runnable) => {
  if (test.state === 'failed') {
    const screenshotFileName = `${runnable.parent.title} -- ${test.title} (failed).png`
    const path = `../reports/screenshots/${Cypress.spec.name}/${screenshotFileName}`
    cy.allure().attachment('screenshot', new Buffer.from(path,'base64'),'image/png')

  }
  else {
    const screenshotFileName = `${runnable.parent.title} -- ${test.title} -- after each hook.png`
    const path = `../reports/screenshots/${Cypress.spec.name}/${screenshotFileName}`
    cy.allure().attachment('screenshot', new Buffer.from(path,'base64'),'image/png')
  }
})

In my test I just have a afterEach calling cy.screenshot() image

My OS is a Linux Mint 20 Ulyana and allure is in its most recent version I believe which is 2.13.6.

Shelex commented 4 years ago

Hi @rodolforodriguess Thank you for opening an issue.

  1. Why it is not automatically attached: afterEach mocha hook is fired when test itself is finished, and because of that there is no logger for test available in allure at that time and it is trying to add screenshot to current executable, which is afterEach hook logger, and then this hook is attached to suite logger with no screenshot available as allure does not support showing it. Solution for such issue could be done in terms of #17 with weird idea to handle hooks as steps for test, as order of mocha events not allow to do that natively.

  2. Why it cannot be processed in test:after:run: The issue is that cypress internal test:after:run event has no relation to mocha runner as according to documentation test:after:run fires after the test and all afterEach and after hooks run. While allure reporter writer itself is an afterEach hook, it basically cannot access what is going on in test:after:run.

Shelex commented 3 years ago

Addressed in v1.10.0