Rayrun

How do I obtain the duration of an HTTP GET request using Playwright?

Answer

Get Duration of an HTTP GET Request in Playwright

To obtain the duration of an HTTP GET request in Playwright, you can use the resource timing information provided by the timing() method on Request objects. Here's how you can do it:

  1. First, wait for the requestfinished event on a Page object after making your HTTP GET request using await page.goto(url):
import { test, expect } from '@playwright/test';

test('Get request duration', async ({ page }) => {
  await page.goto('https://ray.run/');
  const request = await page.waitForEvent('requestfinished');
  // ...
});
  1. Once the event occurs and you have the Request object, call its timing() method to retrieve the timing information:
const timing = request.timing();
  1. The timing object contains several properties such as startTime, domainLookupStart, and responseEnd. Calculate the duration using these properties:
const duration = timing.responseEnd - timing.startTime;

Here's the complete example:

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

test('Get request duration', async ({ page }) => {
  await page.goto('https://ray.run/');
  const request = await page.waitForEvent('requestfinished');
  const timing = request.timing();
  const duration = timing.responseEnd - timing.startTime;
  console.log(`Request duration: ${duration} ms`);
});

This code will output the duration of the HTTP GET request in milliseconds. Remember to wait for all necessary events before calling any methods on your Request objects to ensure accurate timing information.

Thank you!
Was this helpful?
Still have questions?

If you still have questions, please ask a question and I will try to answer it.

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 luc@ray.run.