cypress-io / cypress

Fast, easy and reliable testing for anything that runs in a browser.
https://cypress.io
MIT License
46.71k stars 3.16k forks source link

Still unable to add extra information to Mocha Test object #3654

Open vvanmol opened 5 years ago

vvanmol commented 5 years ago

Current behavior:

This is a follow-up of issue #1910.

We're using mochawesome in combination with Cypress in order to generate a HTML Report. Mochawesome provides a feature to add information to the test context and have that information in the generated HTML Report. This is done through the addContext() function.

Thanks to @LVCarnevalli and his comment on #1910 it is possible to use the addContext() function from some Cypress Event Handler (test:after:run event from the comment) successfully.

But using the addContext() function directly from a test does not work. If I execute the following test:

const addContext = require('mochawesome/addContext');

describe('example context missing', () => {
    it('should have context', function () {
        expect(1+1).to.eq(2);
        addContext(this, 'some context'); // <== This is still not working
    });
});

I never get this some context text in my generated report (HTML or JSON).

Desired behavior:

Calling addContext() function from a test should push new information to the test context.

Steps to reproduce: (app code and test code)

Versions

cypress: 3.1.5 mocha: 5.2.0 mochawesome: 3.1.1 mochawesome-report-generator: 3.1.5

MarS919 commented 5 years ago

Exactly the same issue here, and it still exists with cypress 3.2.0.

Other versions: mocha: 5.2.0 mochawesome: 3.1.1 mochawesome-merge: 1.0.7 mochawesome-report-generator: 3.1.5

jhepam commented 5 years ago

I have the same issue. Any solutions?

HNiuca commented 5 years ago

Not yet. But as soon as I find the solution I’ll let you known.

On May 31, 2019, at 13:33, jhepam notifications@github.com wrote:

I have the same issue. Any solutions?

— You are receiving this because you commented. Reply to this email directly, view it on GitHub, or mute the thread.

lots0logs commented 5 years ago

I posted a solution for adding screenshots to the mochawesome report. Perhaps it can be tweaked to suit your needs? You can check it out here: https://github.com/cypress-io/cypress/issues/1910#issuecomment-514067968

kedmenec commented 5 years ago

This has solved the issue for us:

it('Should shine the test report!!!', () => {
  cy.get('li').should('have.length.greaterThan', 0);
  addTestContext('String','giphy');
  addTestContext('Link','https://giphy.com');
  addTestContext('Image','https://media.giphy.com/media/tIIdsiWAaBNYY/giphy.gif');
  addTestContext('Image','https://media.giphy.com/media/tIIdsiWAaBNYY/giphy.gif');
});

function addTestContext(title, value) {
  cy.once('test:after:run', test => addContext({ test }, { title, value }));
}

https://stackoverflow.com/questions/53343181/detailed-reporting-cypress-mochawesome/53356856

LVCarnevalli commented 5 years ago

Hi, more informations view question: https://stackoverflow.com/a/57928604/5495629

peterkrieg commented 4 years ago

Still seeing this on cypress 4.5.0. If I debug into mochawesome code, it looks like the context field is being stripped somehow, it is lost at the point it reaches mochawesome. Anyone know if there's an official fix from cypress, or an alternative current "best" way to retain that field? Thanks in advance 🙂

samtsai commented 4 years ago

Still seeing this on cypress 4.5.0. If I debug into mochawesome code, it looks like the context field is being stripped somehow, it is lost at the point it reaches mochawesome. Anyone know if there's an official fix from cypress, or an alternative current "best" way to retain that field? Thanks in advance 🙂

The best way until a fix comes (if one does) is to use the above: https://github.com/cypress-io/cypress/issues/3654#issuecomment-521887307

I've created a custom command:

import addContext from "mochawesome/addContext"

Cypress.Commands.add("addContext", (context) => {
  cy.once("test:after:run", (test) => addContext({ test }, context))
})

Then in your tests you can do:

cy.addContext("some additional context")

I've been looking to fix it in Cypress-proper but I haven't found a clear solution yet.

leomartinez2020 commented 4 years ago

This post has been so helpful. I spent two days trying to figure out a solution without any success. Thanks to the contributors.

ColeSiegelTR commented 2 years ago
Cypress.Commands.add("addContext", (context) => {
  cy.once("test:after:run", (test) => addContext({ test }, context))
})

This doesn't appear to fire after a test fails due to an AssertionError

image

Is there a way to keep the context on failing tests? Those are the ones which we actually need additional context on.

cytesting commented 2 years ago

Maybe you could try:

Cypress.on('test:after:run', (test, runnable) => { if (test.state === 'failed') { addContext({test}, { title: "msg", value:'Something went wrong with ${test.title}' }) } })

ColeSiegelTR commented 2 years ago

@cytesting thanks for the suggestion! That works to get logs appearing for each test instead of only passing ones, but it doesn't work to actually pass a different context. If I pass a different context, the first one is just copied to subsequent tests. Any idea I could be going wrong?I just want to be able to write custom logs within each test and read them in the mochawesome report.

import addContext from "mochawesome/addContext";

Cypress.Commands.add("addContext", (context) => {
  Cypress.on("test:after:run", (test, runnable) => addContext({ test }, test.title));
  Cypress.on("test:after:run", (test, runnable) => addContext({ test }, context));
});

image

AlanCodega commented 1 year ago

No news about that? in 2023 we are still having this issue, in put in the context of mochawesome-cypress report a simple text?

SanjeetDutt commented 1 year ago

mochawesome is not supporting the arrow function. I tried following code and it worked for me

import addContext from "mochawesome/addContext.js"
.
.
.

      it("STARTING NEW ENQUIRY", async function () {
        addContext(this, "SOME TEXT HERE")
        assert.equal(response, STATUS.OK)
      })