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:
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');
// ...
});
Request
object, call its timing()
method to retrieve the timing information:const timing = request.timing();
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.
If you still have questions, please ask a question and I will try to answer it.
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].