Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cypress pipe console.log and command log to output

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]   └─────────────────────────────────────┘ 
like image 351
Jake He Avatar asked Aug 29 '18 05:08

Jake He


People also ask

What is the Cypress command log API?

This is the internal API for controlling what gets printed to the Command Log. Useful when writing your own custom commands. Pass in an options object to Cypress.log (). Overrides name only for display purposes. We want the Command Log and the console in the DevTools to log specific properties of our custom command.

How to add custom logs in Cypress runner?

To add some logs, you can just use cy.log () command as you would use any other command in your test. But you can take things one step further with Cypress.log () api. Let’s add some custom logs to our custom command: This will now display our custom command in Cypress runner.

Can the console log and cypress log be saved to files?

These logs could be prepended by the testcase name and current URL for context, adding more stuff would be noice. As others have commented, it feels rather bad that the console log and Cypress command log can not be saved to files (separately or together, by configuration) 2.5 years after the issue ( #300) came up.

How do I print a message to the Cypress command log?

Print a message to the Cypress Command Log. cy.log(message) cy.log(message, args...) Message to be printed to Cypress Command Log. Accepts a Markdown formatted message.


1 Answers

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.

like image 151
Joshua Wade Avatar answered Sep 20 '22 08:09

Joshua Wade