skip to Main Content

using cypress i’m intercepting an API call and want to get how much the response time it took

cy.intercept("PATCH", "https://api.website.com/b2b2/**").as("myAPP")
cy.wait("@myAPP").then(({ request, response }) => {
    expect(response.duration).to.be.lessThan(2000)// => expected undefined to be a number or a date
    expect(response.statusCode).to.equal(200)
}) 

2

Answers


  1. In JavaScript, you can measure how fast an API call is by using performance API.

    For example:

    const start = performance.now();
    fetch('https://api.example.com/data')
        .then(response => response.json())
        .then(data => {
          const end = performance.now();
          const responseTime = end - start;
          console.log(`Response time: ${responseTime} milliseconds`);
        });
    Login or Signup to reply.
  2. there are many ways but You may try with

    1. using JavaScript (Node.js and Fetch API):
    const fetch = require('node-fetch');
    
    (async () => {
        const startTime = Date.now();
    
        const response = await fetch('https://api.example.com/data');
        const data = await response.json();
    
        const endTime = Date.now();
        const responseTime = endTime - startTime;
    
        console.log(`Response Time: ${responseTime} ms`);
    })();
    1. Using Postman
      Postman, a popular API testing tool, provides an easy way to measure response time.

    Open Postman.
    Send the API request.
    After the request is completed, Postman displays the response time in the bottom-right corner under the "Response" section.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search