javascript - How to list and record all network requests and response times in Cypress - Stack Overflow

I am looking to convert all requests from cy.intercept() into json objects that include: {'method&

I am looking to convert all requests from cy.intercept() into json objects that include: {'method':'____', 'url':'____', 'response_time':'____'} so that they can be written to a json file for performance analysis.

I am currently able to show all of the request methods and URL's but not their response times.

Current code to get network requests:

cy.intercept({method:'GET', url:'**'}).as('gets');
cy.intercept({method:'POST', url:'**'}).as('posts');
cy.visit('url');

Is it possible to iterate through these individual requests with their response times and save them within an array?

I have tried saving the value returned from intercept() as a variable but it doesn't show all of the requests or their response times.

var gets = cy.intercept({method:'GET', url:'**'}).as('gets');
var posts = cy.intercept({method:'POST', url:'**'}).as('posts');
cy.visit('url');

cy.writefile('file1.json', gets);
cy.writefile('file2.json', posts);

Thanks in advance.

I am looking to convert all requests from cy.intercept() into json objects that include: {'method':'____', 'url':'____', 'response_time':'____'} so that they can be written to a json file for performance analysis.

I am currently able to show all of the request methods and URL's but not their response times.

Current code to get network requests:

cy.intercept({method:'GET', url:'**'}).as('gets');
cy.intercept({method:'POST', url:'**'}).as('posts');
cy.visit('url');

Is it possible to iterate through these individual requests with their response times and save them within an array?

I have tried saving the value returned from intercept() as a variable but it doesn't show all of the requests or their response times.

var gets = cy.intercept({method:'GET', url:'**'}).as('gets');
var posts = cy.intercept({method:'POST', url:'**'}).as('posts');
cy.visit('url');

cy.writefile('file1.json', gets);
cy.writefile('file2.json', posts);

Thanks in advance.

Share Improve this question asked Mar 12, 2022 at 18:43 ed123ed123 331 silver badge3 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 5

You want to use cy.intercept() callbacks to get the duration.

The request callback fires when the request is intercepted, and the response callback is fired when the reponse returns.

const gets = []

const logGet = (request) => {
  const start = Date.now()
  request.continue((response) => {
    const duration = Date.now() - start
    gets.push({url: request.url, duration})
  })
}

cy.intercept('*', logGet)

cy.intercept('**/tracking.min.js').as('done')           // last call I'm interested in

cy.visit('https://docs.cypress.io/api/mands/intercept')

cy.wait('@done').then(() => {                          // wait for last
  console.log(gets)
})

Also, chose a network request that marks the end on the stream. Cypress does not know when calls have ended, so you should cy.wait() on the last one instead of wait(3000).

In above example, I chose the Cypress tracking script.

To gain detailed information about the request and response including the response time, you can utilize the HAR file format (http://www.softwareishard./blog/har-12-spec/). To generate a HAR file on the fly during the execution of your Cypress tests 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.');

    // ...
  });
});

To obtain access to the HAR file within your tests, you can create a custom mand for this purpose:

/// <reference types="cypress" />

namespace Cypress {
  import { Har } from 'har-format';

  interface Chainable<Subject> {
    findHar(fileName?: string): Chainable<Har>;
  }
}

Cypress.Commands.add('findHar', (fileName?: string) =>
  cy
    .readFile(fileName ?? Cypress.spec.name.replace('.ts', '.har'))
    .then(data => cy.wrap<Har>(JSON.parse(data)))
);

Then, you can utilize it to iterate over the entries and map them to the desired structure:

cy.findHar()
  .its('log.entries')
  .then((entries: Entry[]) =>
    entries.map(({ request, time }: Entry) => ({
      response_time: time,
      method: request.method,
      url: request.url,
    }))
  );

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742358296a4428879.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信