-
Notifications
You must be signed in to change notification settings - Fork 3.3k
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
Comments
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 |
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) |
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
}) |
Oh, we didn't speak about the same thing. I'll create a separate issue |
+1 on this. We use the |
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:
On OSX
|
This comment has been minimized.
This comment has been minimized.
Just FYI the one easy solution is just to 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. |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
A massive hack is to do something like
Than, just watch the video artefact and pause it at the right time 🤣 |
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 // 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 |
@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 So add you need to do in a support file somewhere.. Cypress.on('window:before:load', (win) => {
cy.spy(win.console, "log")
}) |
Hi, I have added this to Cypress.on('window:before:load', (win) => {
cy.spy(win.console, "log")
}) |
I am having the same issue! |
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? |
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.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 module.exports = (on, config) => {
on('task', {
log(message) {
console.log(message)
return null
},
})
} |
@siowyisheng I tried your approach, but never see the logs via |
So i'm having the same issue as @JesterXL. |
@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". |
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 |
@darenyong I had the same issue, where my test did not work as expected but the video from The steps I used to do this are here: cypress-debug.md |
This actually works very well! |
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 (
|
Note: in order to avoid the error if ((win.console.error as any).restore) {
(win.console.error as any).restore();
}
cy.spy(win.console, 'error'); |
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. |
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! |
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.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);
});
}); |
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! |
how can we type into cypress browser console and assert on the returned value using cypress |
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. |
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. 🙏🏻 |
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.
The text was updated successfully, but these errors were encountered: