Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Abbility to import logs into report #180

Closed
irapths opened this issue Mar 14, 2023 · 6 comments
Closed

Abbility to import logs into report #180

irapths opened this issue Mar 14, 2023 · 6 comments
Assignees

Comments

@irapths
Copy link

irapths commented Mar 14, 2023

Hello

This plugin is very helpfull during debuging failed tests.
But when run on CI it is not very convinient to search into the expoted log files to locate the failed test.

I'd like be able to import the collected logs of failed tests into the Mochawesome report as "additiona context" on test:after:run.
I can underestant that the log files are not yet created at this state, but later.
So I'm thinking of a different kind of data exposure and use it on test:after:run.
But "auto-import" by the plugin - activated by a new flag - could be also OK, if not even better.

For example I can import the video with the following simple code

// @mension: https://glebbahmutov.com/blog/the-awesome-battle/
Cypress.on('test:after:run', function (test, runnable) {
    if (Cypress.Mochawesome) {
        if (Cypress.config('video')) {
            // Add video on failed test
            const videoFile = `${Cypress.config('videosFolder')}/${Cypress.spec.name}.mp4`
            Cypress.Mochawesome.context.push({
                title: 'Spec file execution video',
                value: videoFile
            })
        }
    }
});

An idea of the code to be used to add the logs into report is:

Cypress.on('test:after:run', function (test, runnable) {
    if (Cypress.Mochawesome) {
        if (Cypress.TerminalLogs) {  // TerminalLogs: Placeholder for cypress-terminal-report presence check
            // Add collected terminal log
            Cypress.Mochawesome.context.push({
                title: `Execution log`,
                value: Cypress._.join(testLogs, '\n')  // testLogs: Placeholder for collected logs of the just-ended test
            })
        }
    }
});

Thank you for your work on this plugin
.

@archfz
Copy link
Owner

archfz commented Mar 14, 2023

I have quickly glanced into the mochawesome report. I suppose we are talking here about the html generated output. And you would like to include the logs generated from this plugin into that right?

I haven't used mochawesome, but I am open to it and contribution is welcome.

Wouldn't this also require formatting the logs for proper html display? Also besides all this have you also considered configuring the plugin to export logs in files only for failed tests?

In any case it seems to me that this should be enabled by a new config on the support install, and might also need a little bit of refactoring on how we send logs to node plugin side, as we would also need to direct them into this context push too on the support js side ( changes to be made in log collector controllers) + necessary formatting.

@irapths
Copy link
Author

irapths commented Mar 15, 2023

Hi

I have the plugin configured to print on CLI and store to files per spec file (this is the most awesome think with this plugin). But when run on CI and multiple tests failed it is not so easy to locate the one you want in the files.
Side note, I don't like any reporting tool/way I checked until now, but mockawesome is more OK than the others. And if screenshot and executed commands added - the way cypress-terminal-report do - it will be more usable.

As for the mochawesome, you are correct, we are talking about insert the logs into html file. I'm not sure if they will be added automatically into any other supported format, but it is not important.
There is no need to format the logs since the Cypress.Mochawesome.context.push(...) - or any other more convenient way that I don't know yet since I'm new in JS + Cypress + mochawesome - handles it automatically.

I managed to import the logs as a try and as an example of how it will look, by reading the current test logs from the created file on afterEach() and using the Cypress.Mochawesome.context.push(...).
BUT it is the already existing log and not the one of the current run since it is not yet written.
example-mochawesome-report-with-embeded-logs

What I'm thinking is, if is possible to expose the collected logs of the current test via a method. Then it could be used inside the test:after:run the way I "use" them in the second code snippet of the first comment.
There is no need for logging the before() and after() if is not possible.

As for the importing by this plugin to the other, this can be done as next step.
I'm afraid I cannot help on the implementation because of the lack of knowledge on JS and node. But I can (and I want to) try if someone guide me.

Thanks for the really quick reply and about the interest on this I-hope-possibly-to-be-an-implemented feature.

@archfz
Copy link
Owner

archfz commented Mar 16, 2023

I am thinking that maybe we can go with something simple. This would be simpler if the processing of the logs for output into files would have happened in the browser, but they happen on node side.

We could add a Cypress.TerminalReport.on('logs', (logs) => {} . Also we might be able to extract the txt formatting and reuse it in the browser by Cypress.TerminaReport.formatLogs('txt', logs). So in the end you could do on your side:

Cypress.TerminalReport.on('logs', (logs) => {
     Cypress.Mochawesome.context.push({
           title: `Execution log`,
           value: Cypress.TerminalReport.formatLogs('txt', logs)
      })
});

@irapths
Copy link
Author

irapths commented Mar 17, 2023

This really nice aproach, since inside Cypress.TerminalReport.on('logs', (logs) => {}) the user is free to use the collected TC logs whatever way he wants and at the right time (after TC completed).

Only one possible problem I can see here (and maybe is not valid at all), if the Cypress.TerminalReport.on('logs', (logs) => {}) will be triggered wille Cypress.Mochawesome instance exists or not.

To be honest, I like the txt format but I think the JSON gives more freedom on formating the output. Or even better expose both formats.

@danyg
Copy link

danyg commented Mar 31, 2023

Hi we also looking forward to this feature!
It would be way better for us to it to work in this way.

afterEach(() => {
  const data = Cypress.TerminalReport.getLogsOfCurrentTest().join('\n']);
  cy.addTestContext(`---[ Logs ]-------------------------------------------------------------:\n${data}`);
});

Although the proposed way also works, we could collect the logs per test in some way or another.
Using cypress-mochawesome-reporter which adds the cy.addTestContext command

@archfz archfz self-assigned this Apr 2, 2023
archfz added a commit that referenced this issue Apr 2, 2023
#180: Implement support for mochawesome integration.
@archfz
Copy link
Owner

archfz commented Apr 2, 2023

Added support in 5.1.1; see readme

@archfz archfz closed this as completed Apr 2, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants