【问题标题】:cypress log request from interceptorcypress log request from interceptor
【发布时间】:2022-12-28 03:52:56
【问题描述】:

How can I log a "request.body" from cypress interceptor. Here is the code

  beforeEach(() => {
    cy.log("---- -- Running beforeEach");
    cy.intercept("POST", "/graphql", (req) => {
      cy.log("-- --- -- loging from interceptor", req.body);
      return req;
    });
  });

I get this error :

Cypress detected that you returned a promise from a command while also invoking one or more cy commands in that promise.

If I remove cy.log no errors are raised. So how can I log this? This runs on CI.

Will use https://github.com/flotwig/cypress-log-to-output plugin if no other way.

【问题讨论】:

  • I would try req.continue() instead of return req -- is there a specific reason you are returning the request?
  • either return or req.continue() the issue persists. It is the cy.log within the interceptor that is causing the error.

标签: cypress interceptor cypress-intercept


【解决方案1】:

One way you can access the request body would be using cy.should() callback as follows. First you define your intercept command and add an alias to it:

// intercept some post request
cy.intercept('POST', '/api/**').as('yourPostRequest');

After that, you append cy.should() with callback function to the cy.wait() command which allows you to access and log the request body like:

// wait and log request body
cy.wait('@yourPostRequest').should(($obj) => {
  const requestBody = $obj.request.body;
  cy.log(requestBody);
});

You can also find more helpful information regarding network calls and Cypress in this blog post.

【讨论】:

    【解决方案2】:

    You may be able to use the synchronous Cypress.log instead

    cy.intercept("POST", "/graphql", (req) => {
      Cypress.log({ displayName: 'Request body', message: req.body })
      ...
    })
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-03-17
      • 2021-11-13
      • 2018-01-03
      • 2022-12-28
      • 1970-01-01
      • 2022-12-01
      • 2017-12-09
      • 1970-01-01
      相关资源
      最近更新 更多