flotwig / cypress-log-to-output

A Cypress plugin that sends all logs that occur in the browser to stdout in the terminal.
146 stars 19 forks source link

Option to only record the logs? #18

Open spschlegel opened 3 years ago

spschlegel commented 3 years ago

Its great being able to record the console logs in addition to printing them to the terminal. I would like to store the logs during the cypress tests for our CI builds but printing them to the terminal output as well is flooding the output. Is there a way to only record the logs and not print them to the terminal output?

nischaytv commented 2 years ago

How can we retrieve the recorded logs. Is it possible to store these logs in a file? If yes how can this be done?

spschlegel commented 2 years ago

From the Readme: The logs will be stored in an internal buffer. They can be accessed using the getLogs exported function. The buffer can be cleared using the clearLogs exported function.

So we used the getLogs function to retrieve the logs and then wrote them to a file using fs.createWriteStream

nischaytv commented 2 years ago

Do you have any code sample for this? I tried using getLogs, but didn't get it.

spschlegel commented 2 years ago

Something like this:

import {
  getLogs
} from 'cypress-log-to-output';

const logs = getLogs();
Arahort commented 2 years ago

Something like this:

import {
  getLogs
} from 'cypress-log-to-output';

const logs = getLogs();

May be stupid question, but how use this in test step?

laviniaSer07 commented 2 years ago

Something like this:

import {
  getLogs
} from 'cypress-log-to-output';

const logs = getLogs();

May be stupid question, but how use this in test step?

I'm also interested about how to use it in a test step.

spschlegel commented 2 years ago

I don't think I really understand what you want to do. Using this is not tied to tests in general. You import the getLogs function and then you call it whenever you want to retrieve the current logs. You will get them as an array and then it's up to you to do something with that array.

Arahort commented 2 years ago

I don't think I really understand what you want to do. Using this is not tied to tests in general. You import the getLogs function and then you call it whenever you want to retrieve the current logs.

Then test crashed, and cypress-log-to-output - breaks other packages, such as cypress-audit. I had to remove it altogether so that the rest of the packages would work again.

oliverstr commented 2 years ago

Something like this:

import {
  getLogs
} from 'cypress-log-to-output';

const logs = getLogs();

May be stupid question, but how use this in test step?

The plugin and your spec file run in different environments, to access the node process from your test spec you need to use cy.task.

When installing the plugin, add:

import { getLogs, install } from 'cypress-log-to-output';

module.exports = (on, config) => {
  /** the rest of your plugins... **/
  const options = { recordLogs: true };
  install(on, filterCallback, options);

  on('task', {
     getDebuggerLogs(){
         return getLogs();
     }
  });
}

Then on your spec file access it:

cy.task('getDebuggerLogs').then((logs) => {
   // do whatever with the logs here
}