0

I want to get the logs written in a log file for the execution.

I am trying to create a cypress spec and I want to have log files generated for the operations performed on webpage.

There is cy.log() to log something custom but the logs are there during the run, but after it I can only see them in the video.

I want to have logs on a .log file that I can export after the cypress run is completed.

logs in cypress

Mordecai.S
  • 23
  • 3

3 Answers3

3

You can get the raw log records into a json file by adding code to catch the log events, and saving to a file at the end.

cypress/support/e2e.js

const logs = {}
Cypress.on('log:added', (log) => {
  logs[log.id] = log
})
Cypress.on('log:changed', (log) => {
  logs[log.id] = log
})

after(() => {
  cy.writeFile(`logs/${Cypress.spec.name}.log.json`, logs)
})

If you use the experimentalRunAllSpecs flag to run all, the log will be written to a file logs/__all.log.json. This is how the run-all feature is working now, the specs are combined into one uber spec called __all.

Fody
  • 23,754
  • 3
  • 20
  • 37
0

You can use the cypress-log-to-file plugin to generate log files for your Cypress tests. To use this plugin, you first need to install it using npm:

npm install cypress-log-to-file

Then, in your cypress/plugins/index.js file, include the following code:

const logToFile = require('cypress-log-to-file/lib/logToFile'); module.exports = (on, config) => { logToFile(on, config); };

This will create a cypress.log file in your project's root directory that contains the logs generated during your tests' execution. You can access this file after your tests have completed and view the logs for debugging purposes.

0

Checkout Cypress terminal report you can use it in your cypress.config file like below:

setupNodeEvents(on, config) {
  // ...
  const options = {
    outputRoot: config.projectRoot + '/logs/',
    outputTarget: {
      'out.txt': 'txt',
      'out.json': 'json',
    }
  };

  require('cypress-terminal-report/src/installLogsPrinter')(on, options);
  // ...
}

more examples on the GitHub repo readme file.

JansenHa
  • 11
  • 4