89

Is it possible to redirect or capture Cypress browser log and command log to output?

I read some Cypress github issues on this topic. But I don't know how to make it work.

Basically, I want to capture all the Cypress GUI command logs in the headless non-GUI mode. If I can include browser console log will be even better. The purpose is to understand what happened when a test fails.

I use teamcity as ci. Here is an example of my build log. I want to see all the command log here too. Actually, any console.log run on the server side using cy.task is displayed in the build log. Running cy.task('log',message) is too manual. Any smarter ways?

[09:49:08][Step 1/1] 2 of 4: new actions (52s)
[09:50:00][Step 1/1] 3 of 4: new actions (52s)
[09:50:53][Step 1/1] 4 of 4: new actions (53s)
[09:51:47][Step 1/1]   (Results)
[09:51:47][Step 1/1] 
[09:51:47][Step 1/1]   ┌─────────────────────────────────────┐
[09:51:47][Step 1/1]   │ Tests:        8                     │
[09:51:47][Step 1/1]   │ Passing:      8                     │
[09:51:47][Step 1/1]   │ Failing:      0                     │
[09:51:47][Step 1/1]   │ Pending:      0                     │
[09:51:47][Step 1/1]   │ Skipped:      0                     │
[09:51:47][Step 1/1]   │ Screenshots:  0                     │
[09:51:47][Step 1/1]   │ Video:        true                  │
[09:51:47][Step 1/1]   │ Duration:     3 minutes, 38 seconds │
[09:51:47][Step 1/1]   │ Estimated:    1 minute, 8 seconds   │
[09:51:47][Step 1/1]   │ Spec Ran:     action/action_spec.js │
[09:51:47][Step 1/1]   └─────────────────────────────────────┘
5
  • It looks like they are trying to produce an all-encompassing feature rather than a simple hook, which is what most people seem to want. jantoebe's code here looks promising. Might be worth asking him where he adds it in the tests.
    – user8745435
    Commented Aug 29, 2018 at 20:47
  • How about sending client's browser log back to the server? Using jantoebe's idea of replacing console and somehow send the log back to node's console log. Do I need to make an endpoint to listen on a port using cy.task?
    – Jake He
    Commented Aug 29, 2018 at 22:02
  • Use something like this github.com/krakenjs/beaver-logger. It might be an overkill.
    – Jake He
    Commented Aug 29, 2018 at 22:03
  • Yes, definitely a cy.task to run the code into Node env. Then just fs to a file, but with some nice formatting. I am also interested in capturing the command log formatted html (not just the test) for documentation, so document.getElementById('root').innerHTML looks like the way to do that.
    – user8745435
    Commented Aug 29, 2018 at 23:18
  • 1
    Please also see cypress-failed-log which enumerates the commands - looks quite useful for CI scenario.
    – user8745435
    Commented Aug 30, 2018 at 0:53

7 Answers 7

112

As of Cypress 3.0.0, you can use cy.task() to access node directly and output to the node console. From the docs:

// in test
cy.task('log', 'This will be output to the terminal')
// in plugins file
on('task', {
  log (message) {
    console.log(message)
    return null
  }
})

See here for more info.

I don't know of a way to mirror the Cypress logs to the console directly, but this is at least a workable alternative.

2
  • 4
    Somehow this is not working for me. I'm on latest Cypress version. Commented Sep 7, 2020 at 0:45
  • 4
    @AmitKumarGupta This answer was taken directly from the doc page for cy.task. If you follow the steps from the latest documentation and it doesn't work, then you may have found a bug, which you can report on Github. Commented Sep 8, 2020 at 15:49
40

Setting the ELECTRON_ENABLE_LOGGING environment variable to 1 will cause all Chrome internal logging to be printed to the console.

ELECTRON_ENABLE_LOGGING=1 npx cypress run

ELECTRON_ENABLE_LOGGING

Prints Chrome's internal logging to the console.

With this enabled, in addition to capturing any existing logging, this will also allow you to manually log within a test using console.log:

console.log('Response JSON: ' + json)
4
  • 5
    Too bad that this only works when tests run in electron, and not e.g. when using chrome (ELECTRON_ENABLE_LOGGING=1 npx cypress run --browser chrome)
    – LGenzelis
    Commented Dec 2, 2021 at 14:41
  • 1
    @LGenzelis even with electron its not working for me
    – wamster
    Commented Jan 15, 2023 at 17:52
  • 4
    hey @wamster , check my answer. Or just go to npmjs.com/package/cypress-terminal-report. It's been a game changer for me :)
    – LGenzelis
    Commented Jan 17, 2023 at 12:59
  • @LGenzelis Looks like that answer was deleted the same day you posted it, since it didn't explain how the plugin helped solve this issue.
    – M. Justin
    Commented May 27 at 21:23
10

FYI:

Cypress community is going to provide native support so that we don't have to do any workarounds to print the logs on non-GUI(headless) CLI.

Ongoing issue: https://github.com/cypress-io/cypress/issues/448 includes reference to 3 existing workarounds https://github.com/cypress-io/cypress/issues/448#issuecomment-613236352

1
  • 2
    Sadly, they decided to only fix it for cloud customers :( Commented Nov 27, 2023 at 0:20
8

Expanding on @Joshua-wade's answer, you can overwrite cy.log to redirect all calls to it to the log task. Just as the following:

Cypress.Commands.overwrite('log', (subject, message) => cy.task('log', message));

Note: there's a small drawback to this: when you run the test using the Test Runner, instead of seeing LOG my message in the command log, you'll see TASK log, my message. But IMHO it's negligible.

1
  • when i try this i get The 'task' event has not been registered in the setupNodeEvents method. You must register it before using cy.task() - Cypress: 10.7.0
    – hanshenrik
    Commented Jun 29, 2023 at 13:47
3
// cypress.config.js
const { defineConfig } = require("cypress");

module.exports = defineConfig({
  projectId: "cnpkrh",
  e2e: {
    setupNodeEvents(on, config) {
      on('task', {
        log(message) {
          console.log(message);
          return null;
        },
      });
    },
  },
});


// cypress/e2e/spec.cy.js
cy.task('log', 'This will be output to the terminal')
2
  • This is a very good alternative and worked for me. Especially simply using: cy.task('log', 'This will be output to the terminal') anywhere in the code. Commented Sep 7, 2023 at 0:57
  • A good followup to this is to override the log function if in headless mode. See hughsk's answer here: stackoverflow.com/a/73827995/5327131 Commented Feb 21 at 0:29
1

I agree with Araon's approach using overwrite on the log function. Another approach, if you want to keep cy.log the default behavior, would be create a custom command. Doc here

Example: Cypress.Commands.add("printLog", (message) => { cy.task("log", {message}); })

This way you can call function printLog vs cy.task("log", {message});

0

Disclaimer: I am the plugin author

You can capture Cypress CI GUI commands in headless mode, together with all the network requests, browser console messages and DOM tree changes using this free open-source plugin: https://github.com/currents-dev/cypress-debugger

As an alternative, you can also use a set of other well-known plugins:

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.