2

I run Windows10, I have a project made on Cypress and I want to log the test results both on a file AND on console: I tried just printing on a file, using in my package.json this script:

"scripts": { "open": "./node_modules/.bin/cypress open", "runheadless": "./node_modules/.bin/cypress run --headless --browser chrome --spec 'cypress/integration/webpages.spec.js' > cypresstest.log" } 

And this runs smoothly; my issue is that there are more than 100 tests and it takes very long time (like 20 minutes); so I can't check if something got frozen or is working fine, because nothing is printed on console.

So I tried with

"runheadless": "./node_modules/.bin/cypress run --headless --browser chrome --spec 'cypress/integration/webpages.spec.js' | tee cypresstest.log" 

But since I'm on windows, it says

tee is not recognized as internal or external program 

Is there a way, or a plugin, or something I can do to simply print both on console AND on a file log?

1 Answer 1

1

Cypress-terminal-report has such a feature, or you can use a custom command including cy. task instead of cy.log - for example:

cypress plugin file

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

custom command:

Cypress.Commands.add("logInAndOut", (message) => { cy.log(message) cy.task('log', message) }); 

test file

cy.logInAndOut('My log') 

Edit: I found another solution - Commands. overwrite(), and I`ll list an example, but I do not recommend it, since if other people try to use the code after you - won't know the change:

Cypress.Commands.overwrite('log', (originalFn, message, args...) => { console.log(message, args...) // originalFn is the existing `log` command that you need to call // and it will receive whatever you pass in here. // // make sure to add a return here! return originalFn(message, args...) }) 
Sign up to request clarification or add additional context in comments.

5 Comments

Thanks, I'll try the plugin!
I improved my suggestion since I tried it and it started to show only on the console
Added a third option on how to do it.
well, I've finally tried the plugin but sadly does not do what I meant to do: it prints a json (or similar) file with a resume of errors, but sadly it doesn't print exactly what it prints on terminal; moreover for some weird reasons, if I try to use it on my project using cypress interface instead of headless, it loops forever. I will try your custom suggestion..
According to other users this should solve your problem: custom command: Cypress.Commands.overwrite('log', (subject, message) => cy.task('log', message)); => event in plugins/index.js: on('task', { log (message) {console.log(message); return null; }}) stackoverflow.com/questions/52070262/…

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.