Rayrun
← Back to Discord Forum

monitor api response time?

_vibhanshu_posted in #help-playwright
Open in Discord
_vibhanshu_

Hey guys I am looking a way to monitor the api response time with the help of playwright TS

I know on traces I can we see the time for element to load however, multiple api calls will be there to load a single element.

Any leads will be helpful Thanks #api #reponsetime

This thread is trying to answer question "How can I monitor the API response time with the help of Playwright TS?"

1 reply
butchmayhew
butchmayhew

Simple example could be.

const start = Date.now();

    <MAKE API REQUEST HERE>

    const finish = Date.now();
    const responseTime = (finish - start) / 1000;

    console.log(responseTime);
    expect(responseTime).toBeLessThan(0.25); //less than 250ms

I don't think this is built into Playwright yet, but if you create a feature request on github I'll add my support!

Answer

Sure, let's dive into how you can monitor API response time with Playwright in TypeScript.

First, you need to import the expect function from @playwright/test.

import { expect } from '@playwright/test';

Then, use the expect function with an async arrow function to make the API request and check its response status. You can set custom timeout intervals for retrying the assertion with the toPass method.

await expect(async () => {
  const response = await page.request.get('https://ray.run');
  expect(response.status()).toBe(200);
}).toPass({
  intervals: [1000, 2000, 10000],
  timeout: 60000
});

In this example, Playwright will retry the assertion at intervals of 1000, 2000, and 10000 milliseconds until it passes or the 60-second timeout is reached.

This approach lets you ensure your API calls respond within acceptable limits. If an assertion fails or times out during any interval within the timeout period, an error is thrown.

For more details on handling flaky tests and retry APIs in Playwright, check out these blog posts: Mastering the Art of Detecting and Handling Flaky Tests in Playwright and Exploring the Various Retry APIs of Playwright for Robust Testing.

Related Discord Threads

TwitterGitHubLinkedIn
AboutQuestionsDiscord ForumBrowser ExtensionTagsQA Jobs

Rayrun is a community for QA engineers. I am constantly looking for new ways to add value to people learning Playwright and other browser automation frameworks. If you have feedback, email [email protected].