Yoda
Yoda

Reputation: 18068

Can you configure Cypress to log requests and responses to a file?

When I run my Cypress tests I randomly get HTTP 405 Method not allowed code when I submit the form, but the HTTP method is a correct one - POST. If I open the Developer Tools to see the outgoing request the HTTP 405 is never returned, in other words the error only happens when Developer Tools are closed. No combination of cy.pause(), cy.wait() alleviates the problem.

Question: Can you configure Cypress so it logs all the outgoing requests and responses to a file so I don't have to open DevTools?

Upvotes: 0

Views: 1309

Answers (2)

Artsem Dzerauniuk
Artsem Dzerauniuk

Reputation: 11

Utilizing an interceptor solely for logging purposes is not very efficient.

You can generate a HAR file that includes detailed information about the network activity during the execution of your Cypress tests.

Especially for this purpose, you can use the cypress-har-generator.

describe('my tests', () => {
  before(() => {
    // start recording
    cy.recordHar();
  });

  after(() => {
    // save the HAR file
    cy.saveHar({ waitForIdle: true });
  });

  it('does something', () => {
    cy.visit('https://example.com');

    // ...
  });
});

Upvotes: 0

user16695029
user16695029

Reputation: 4440

Should be possible with cy.intercept() functional handler.

General info here Using the routeHandler function

cy.intercept(url, (req) => {
  cy.writeFile('my-log', req, { flag: 'a' })  // append
  req.continue((res) => {
    cy.writeFile('my-log', res, { flag: 'a' })  // append
  })
})

Upvotes: 3

Related Questions