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

Print browser console information to Command Log (console.log/info/warn/error) #300

Closed
honzabrecka opened this issue Nov 20, 2016 · 38 comments
Labels
pkg/driver This is due to an issue in the packages/driver directory stage: proposal 💡 No work has been done of this issue type: feature New feature that does not currently exist

Comments

@honzabrecka
Copy link

Is it possible to get output from the console? Especially warnings and errors. We are using it as a very simple and quick check that the commit didn't introduce any runtime error.

@brian-mann
Copy link
Member

Cypress catches uncaught exceptions and will display them because they fail the test.

What warnings / errors do you want to get? Also what do you mean "get output from the console"?

Are you saying you want console.log to be output in headless mode?

@jennifer-shehane jennifer-shehane added the stage: needs information Not enough info to reproduce the issue label Nov 29, 2016
@MathRobin
Copy link

Hi !

Maybe I can suggest you something. I need more info in console too. In browser. When an assertion failed, concerning an element, it could be great to console.log it.

For example, I would like to know if all the images in my page has src attribute and if this attribute starts for each one by "https://". If one not, the cypress.each will say me it has failed. But on which one? I have to edit my test, console.log each element and find myself. Not really useful (juste for this part, cypress itself is very useful)

@honzabrecka
Copy link
Author

honzabrecka commented Nov 30, 2016

To clarify, in nightwatch I have a custom command/assert to check that the code haven't produced any runtime warning/error. For example 'React in development mode warns that markup produced on server differs from markup produced on client'. Something like this:

// this would be the last step
cy
  .getConsoleLog()
  .should(function(log) {
    // log = [['warn', 'warning message'], ['log', 'regular log message'] ['warn', 'another warning message']]
    // check that log does not contain any 'warn' or 'error' entry
    const disallowedLevels = new Set(['warn', 'error'])
    expect(log.some(([level]) => disallowedLevels.has(level)).false
  })

@MathRobin
Copy link

Oh, we didn't speak about the same thing. I'll create a separate issue

@craffit
Copy link

craffit commented Feb 28, 2017

+1 on this. We use the cypress run command in continuous integration. Currently we are looking at a test which fails using cypress run, but succeeds when using the non-headless version. The movie shows that our webgl canvas is not loaded in the headless version, but we have no information on where to start debugging (the movie just shows the canvas absent). I would like to see the console log with errors coming from javascript when executing cypress run.

@brian-mann
Copy link
Member

brian-mann commented Feb 28, 2017

There is a non-documented way of seeing the headless GUI and being able to use dev tools on it. You'll have to run this from a local machine (cannot do this in CI environment).

You invoke the binary directly like so:

On linux:

/home/<user>/.cypress/Cypress/Cypress --project <fully_qualified_project_path> --show-headless-gui

On OSX

/Applications/Cypress.app/Contents/MacOS/Cypress --project <fully_qualified_project_path> --show-headless-gui

@jennifer-shehane jennifer-shehane added type: duplicate This issue or pull request already exists and removed stage: needs information Not enough info to reproduce the issue labels Mar 21, 2017
@jennifer-shehane

This comment has been minimized.

@brian-mann
Copy link
Member

Just FYI the one easy solution is just to spy on console functions.

cy.window().then((win) => {
  cy.spy(win.console, "log")
})

That will print a command log every time that function is called, and you could also then assert on what has been logged.

@ianwalter

This comment has been minimized.

@bahmutov

This comment has been minimized.

@brian-mann

This comment has been minimized.

@ianwalter

This comment has been minimized.

@ianwalter

This comment has been minimized.

@brian-mann

This comment has been minimized.

@ianwalter

This comment has been minimized.

@ghost
Copy link

ghost commented Apr 16, 2018

A massive hack is to do something like

<div>{ JSON.stringify(this.state) }</div>

Than, just watch the video artefact and pause it at the right time 🤣

@arsduo
Copy link

arsduo commented Apr 17, 2018

One note for others who want to use @brian-mann's solution of spying on the console log: the spy should be set up in a listener for the window:before:load event:

// globally somewhere like cypress/support/index.js, or for an individual test
beforeEach(() => {
  cy.on("window:before:load", (win) => {
    cy.spy(win.console, "log");
  })
})

Calling it directly in a global beforeEach block won't raise errors but also won't track log statements (the window object seems to be swapped out); calling it directly in a beforeEach block in a test complains that the function is already spied on (not sure why).

@brian-mann
Copy link
Member

@arsduo it complains because unless you are spying on the remote window, you'd be spying on the spec window, which would have already been spied on.

Also you don't have to put cy.on(...) in a beforeEach. cy listeners are removed after each test, and that's why the Cypress listeners exist (this is documented in the same place).

So add you need to do in a support file somewhere..

Cypress.on('window:before:load', (win) => {
  cy.spy(win.console, "log")
})

@zhex900
Copy link

zhex900 commented Aug 28, 2018

Hi,
I would like to see the brower's console log in the headless output.

I have added this to support/index.js But I cannot see any console log. What else should I do?

Cypress.on('window:before:load', (win) => {
  cy.spy(win.console, "log")
})

@arthsiddh
Copy link

I am having the same issue!

@darenyong
Copy link

darenyong commented Aug 31, 2018

For some context: I'm running a cypress run --record inside a docker container. Cypress test runner outside of docker works fine, but I'm having issues running tests when running cypress inside a docker container, using the official cypress/base:8 image as a foundation. Hence I'm trying to debug whats going on when cypress runs headlessly inside of docker.

It seems to work for me, however the only place I can see the log is in the video (see attached screenshot (at step SPY-1)). However my log is clipped by the size of the left command pane... so the usefulness is low.

Is there a way to see the command log without watching the video?

screen shot 2018-08-31 at 3 24 34 pm

@jennifer-shehane jennifer-shehane added type: feature New feature that does not currently exist stage: needs investigating Someone from Cypress needs to look at this and removed type: duplicate This issue or pull request already exists labels Sep 14, 2018
@siowyisheng
Copy link

siowyisheng commented Nov 13, 2018

Here's a workaround I'm using to show console logs and console errors while in headless mode. It also(as an unintended side effect) fails the tests which have any console logs/errors.

in cypress/support/index.js:

Cypress.on('window:before:load', win => {
  cy.stub(win.console, 'log', msg => {
    cy.task('log', `console.log --> ${msg}`)
  })
  cy.stub(win.console, 'error', msg => {
    cy.task('log', `console.error --> ${msg}`)
  })
})

in cypress/plugins/index.js(this is a snippet from the docs):

module.exports = (on, config) => {
  on('task', {
    log(message) {
      console.log(message)
      return null
    },
  })
}

@jennifer-shehane jennifer-shehane added the stage: proposal 💡 No work has been done of this issue label Nov 28, 2018
@jennifer-shehane jennifer-shehane removed the stage: needs investigating Someone from Cypress needs to look at this label Nov 28, 2018
@JesterXL
Copy link

@siowyisheng I tried your approach, but never see the logs via cy.log or files if I write 'em via fs.writeFileSync. Cypress keeps complaining I'm doing a cy.task inside the cy.visit. Is this what you meant by causing all to fail intentionally? Is there some detail I'm missing?

@glomotion
Copy link

So i'm having the same issue as @JesterXL.

@dwelle
Copy link

dwelle commented Jan 7, 2019

@glomotion @JesterXL I've written an SO answer with one possible workflow, summing up some of the points from this topic + addressing the cypress "complaints".

@jennifer-shehane
Copy link
Member

Related issue involving printing console information to CLI output: #700 If you're goal is to see the console information printed within the CLI, please comment in that issue.

I am leaving this issue open and repurposing it as "Print console information to Command Log" as that has been the bulk of the conversation of this thread.

This issue is still related to the larger issue of logging ALL events and commands to display in the Dashboard here: #448

@jennifer-shehane jennifer-shehane changed the title Get console output Print console information to Command Log Jan 9, 2019
@jennifer-shehane jennifer-shehane added pkg/driver This is due to an issue in the packages/driver directory difficulty: 3️⃣ labels Jan 9, 2019
@jennifer-shehane jennifer-shehane changed the title Print console information to Command Log Print console information to Command Log (console.log/info/warn/error) Jan 9, 2019
@dylanburati
Copy link

@darenyong I had the same issue, where my test did not work as expected but the video from cypress run did not give enough information to fix it. I ended up running the CI job locally using circleci-cli, and adding steps so that I could create an SSH session with X forwarding. Then, I manually ran cypress open and used the browser's development tools to find the problem.

The steps I used to do this are here: cypress-debug.md

@satyavh
Copy link

satyavh commented Jun 25, 2019

@glomotion @JesterXL I've written an SO answer with one possible workflow, summing up some of the points from this topic + addressing the cypress "complaints".

This actually works very well!

@benrobot
Copy link

The following worked for me and it even includes the full error message.

cypress/support/index.js

beforeEach(() => {
    cy.window().then((win) => {
        cy.spy(win.console, "error");
        cy.spy(win.console, "warn");
    });    
});

afterEach( () => {
    cy.window().then((win) => {
        expect(win.console.error).to.have.callCount(0);
        expect(win.console.warn).to.have.callCount(0);
    });
});

Here's a sample of the output when a console error or console warning has occurred:
image

@benrobot
Copy link

The solution I provided below doesn't work 100% of the time (more like 1% of the time) because it seems that a new window (win) object is created after each call to cy.visit('/some/page'). So make sure you create the spy after each call to cy.visit(). This was clarified for me in #4808.

The following worked for me and it even includes the full error message.

cypress/support/index.js

beforeEach(() => {
    cy.window().then((win) => {
        cy.spy(win.console, "error");
        cy.spy(win.console, "warn");
    });    
});

afterEach( () => {
    cy.window().then((win) => {
        expect(win.console.error).to.have.callCount(0);
        expect(win.console.warn).to.have.callCount(0);
    });
});

Here's a sample of the output when a console error or console warning has occurred:
image

@macjohnny
Copy link

Note: in order to avoid the error attempted to wrap error which is already wrapped, make sure to restore the spy after each test, or simply before setting up a new spy:

        if ((win.console.error as any).restore) {
            (win.console.error as any).restore();
        }
        cy.spy(win.console, 'error');

@pcj
Copy link

pcj commented May 4, 2020

The feature that I am looking for is: dump the entire content of the debug console to a file at the end of a test so it can be saved as a test artifact.

@csvan
Copy link

csvan commented May 7, 2020

Recently migrated from Protractor to Cypress and was surprised a simple API for reading from the browser console does not exist yet. Very much looking forward to this feature!

@5mutawa
Copy link

5mutawa commented Sep 8, 2020

This does exactly what I needed for catching any error in the console and do an assertion of the logs count. Just add the following in cypress/support/index.js

Cypress.on('window:before:load', (win) => {
  cy.spy(win.console, 'error');
  cy.spy(win.console, 'warn');
});

afterEach(() => {
  cy.window().then((win) => {
    expect(win.console.error).to.have.callCount(0);
    expect(win.console.warn).to.have.callCount(0);
  });
});

@pardamike
Copy link

Our team wanted to log the console output to the terminal for debugging some issues. We ended up using a great package/plugin (https://github.com/flotwig/cypress-log-to-output) that was able to let us see what was going wrong in the headless Chrome's console from the CI terminal. Hopefully this package helps someone else!

@jennifer-shehane jennifer-shehane changed the title Print console information to Command Log (console.log/info/warn/error) Print browser console information to Command Log/stdout (console.log/info/warn/error) Feb 9, 2021
@jennifer-shehane jennifer-shehane changed the title Print browser console information to Command Log/stdout (console.log/info/warn/error) Print browser console information to Command Log (console.log/info/warn/error) Feb 9, 2021
@mudit94
Copy link

mudit94 commented Nov 1, 2022

how can we type into cypress browser console and assert on the returned value using cypress

@jimmux
Copy link

jimmux commented Jul 18, 2023

There are some great suggestions here. I'm getting encouraging results in e2e tests with this addition to the support file:

Cypress.on("window:before:load", (win) =>
  ["error", "warn"].forEach((logLevel) => Cypress.sinon.mock(win.console).expects(logLevel).never())
);

It helps if you don't want failed assertions in your afterEach() to skip remaining tests.

Still experimenting with the component version, but it will probably be similar.

@jennifer-shehane
Copy link
Member

jennifer-shehane commented Oct 3, 2023

Closing this issue as making browser console info available for review during the run is part of Cypress Test Replay. Test Replay allows you to see all console logs, including errors, info and warnings, as they displayed when your tests ran.

If you'd like to log console output to the terminal, there are some great open source plugins available to facilitate this also like https://github.com/flotwig/cypress-log-to-output.

Thanks for all of your feedback on this issue. 🙏🏻

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
pkg/driver This is due to an issue in the packages/driver directory stage: proposal 💡 No work has been done of this issue type: feature New feature that does not currently exist
Projects
None yet
Development

No branches or pull requests