CrashNeb
CrashNeb

Reputation: 494

How do I save Cypress's testrunner's "console log" (left hand side) to a file

I would like to save the data from the left-hand-side of the TestRunner to a text file (json, plain text, or any kind of text).

I feel like this should be very easy, and that I'm simply just missing something. However, I cannot find anything to explain this. I have checked this other S.O. question: Cypress pipe console.log and command log to output, which references this currently open issue -- but this appears to be focused on collecting the browsers console log.

I even tried one of the workarounds suggested in the discussion of that open issue, cypress-log-to-output - but that put a ton of output in the terminal from which I launched the test. I did try to correlate the extra output to the relatively few entries from the TestRunner's left-hand-side, but did not see anything to match them up.

I'm just hoping to get a text file that looks like this (with perhaps a bit of detail for each entry):

1 visit /
(xhr) GET 200 /todos
2 wait @todos
(req) GET /todos Received todos
...

Or perhaps JSON.

My motivation comes from having to write Cypress tests for our CI that will be testing a very old AjaxSwing based application that makes heavy use of XHR requests, and it can be a different number of XHR requests for each test run (sometimes 8, sometimes 12 just to load the first page).

The AjaxSwing app is not changing, so I have to figure this out as best as possible. So I wanted to see a whole text file with all the information from the TestRunner's left hand side. Perhaps even compare separate runs to see if I can spot some "header" or "body" value I could use to distinguish the right XHR request to wait for.

Any help would be appreciated.

Upvotes: 5

Views: 3856

Answers (1)

Dizzy Al
Dizzy Al

Reputation: 607

One approach using the log:added event

// top of spec
const logs = []
Cypress.on('log:added', (log) => {
  const message = `${log.consoleProps.Command}: ${log.message}`
  logs.push(message)
})

it('writes to logs', () => {

  ...  // some commands that log

  cy.writeFile('logs.txt', logs)
});

Upvotes: 4

Related Questions