This thread is trying to answer question "How can I monitor the API response time with the help of Playwright TS?"
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!
Related Ask AI answer for "How do I use Playwright in TypeScript to check the status of an API response within a certain time limit?".
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.
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].