allure-framework / allure-js

Allure integrations for JavaScript test frameworks
https://allurereport.org/
Apache License 2.0
225 stars 121 forks source link

No way to attach video of failed test to allure report in afterTest hook #929

Closed lifanov-rza closed 2 months ago

lifanov-rza commented 6 months ago

Describe the bug

From PW documentation:

Videos are saved upon browser context closure at the end of a test. If you create a browser context manually, make sure to await browserContext.close().

So, if we need to get video of failed test we should wait for browserContext to be closed.

The implementation for allure-legacy plugin is present below:

 async _after() {
    const Playwright = container.helpers('Playwright');
    const allure = container.plugins('allure');
    await Playwright.browserContext.close();
    if (this.#artifacts.video) {
      output.debug(`Attaching a video: ${this.#artifacts.video}`);
      allure.addAttachment('Video', fs.readFileSync(this.#artifacts.video), 'video/webm');
    }
    allure.currentTest = null;
  }

Tried this implementation for allure-codeceptjs plugin but faced with the problem that currentTest is equal to null when test finished (passed / failed). The step 'allure.addAttachment' copies video file to allure-results repository but don't update <%.result.json> file by adding one more attachment.

Expected result:

"attachments": [
    {
      "name": "Console errors",
      "type": "text/plain",
      "source": "40b3cb14-4fde-42cf-b871-0d4c3812884b-attachment.txt"
    },
    {
      "name": "Main session - Last Seen Screenshot",
      "type": "image/png",
      "source": "c2ecc7ea-c805-4553-82ec-84a02260cd34-attachment.png"
    },
    {
      "name": "Video",
      "type": "video/webm",
      "source": <path to video file.webm>
    }
  ],

Actual result:

"attachments": [
    {
      "name": "Console errors",
      "type": "text/plain",
      "source": "40b3cb14-4fde-42cf-b871-0d4c3812884b-attachment.txt"
    },
    {
      "name": "Main session - Last Seen Screenshot",
      "type": "image/png",
      "source": "c2ecc7ea-c805-4553-82ec-84a02260cd34-attachment.png"
    }
  ],

Tried to add an entry about the video attachment and run 'allure serve' -> the video was present in the report.

To Reproduce Steps to reproduce the behavior:

  1. Create AllureVideoHelper in codeceptJS project:
const Helper = require('@codeceptjs/helper');
const { event, container, output } = require('codeceptjs');
const fs = require('fs');

class AllureVideoHelper extends Helper {
  #artifacts;

  /**
   * Constructor
   *
   * @param {object} config - Helper config
   */
  constructor(config) {
    super(config);
    event.dispatcher.on(event.test.started, (test) => {
      this.#artifacts = test.artifacts;
    });
  }

  /**
   * After test execution close browser context and add video to Allure report
   *
   */
  async _after() {
    const Playwright = container.helpers('Playwright');
    const allure = container.plugins('allure');
    await Playwright.browserContext.close();
    if (this.#artifacts.video) {
      output.debug(`Attaching a video: ${this.#artifacts.video}`);
      allure.addAttachment('Video', fs.readFileSync(this.#artifacts.video), 'video/webm');
    }
  }
}

module.exports = AllureVideoHelper;
  1. Add video: true in codeceptJS configuration file
Playwright: {
     ....
      video: true,
     ....
},
  1. Run test with a failure
  2. Run allure serve
  3. Check if video present as attach in allure report

Expected result The video is present

Actual result The video is NOT present

Desktop (please complete the following information):

dimas-rymera commented 5 months ago

I have a similar issue with Cypress, code allure.attachment is not working when I put in hook afterEach here is the sample code

afterEach(function () {
  let path = Cypress.spec.absolute.split('/cypress/tests/')[0]

  cy.readFile(`${path}/cypress/1.png`, null).then((file) => {
    allure.step('Get screenshot', () => {
      allure.attachment('fileName', file, "image/png");
    });
  });

This code will now show on the allure report.. Maybe someone can check it? or maybe you guys don't support this on this hook ?

baev commented 3 months ago

@dimas-rymera support for Cypress hooks introduced in 3.0.0-beta.4, please give it a try

lifanov-rza commented 3 months ago

@dimas-rymera support for Cypress hooks introduced in 3.0.0-beta.4, please give it a try

@baev Do the fixes cover CodeceptJS hooks?

baev commented 3 months ago

@lifanov-rza, we added support for beforeEach & afterEach CodeceptJS hooks; however, hook methods in helpers are not reported by CodeceptJS, so we're still figuring out how we should handle it.