Rayrun
← Back to Discord Forum

Recorder with waitForResponse ?

Hi everyone !

Quick question here : Is there any way to automatically records waitForResponse when an API call occurs ? Maybe using hooks on browser activity ? Unfortunately, I couldn't find anything in the documentation, even if I would like to implement it myself using existing tools :).

Anyway, thank you for this incredible tool and your kind support, I'm definitely in love with Playwright 🙂

Alann

This thread is trying to answer question "Is there any way to automatically record `waitForResponse` when an API call occurs, possibly using hooks on browser activity?"

1 reply

Not sure if i fully understood your problem. However could you put a listener on network events and then add logic to perform waitForResponse or other actions when the URL matches your API endpoint.

Answer

Intercepting API Calls with Playwright

You can definitely intercept and record API responses with Playwright. It's a powerful tool that provides various methods to handle network requests and responses.

Setting Up the Route

First, use the page.route() method to set up a route for the API endpoint you want to monitor. Here's how:

await page.route('**/api/fetch_data', route => {
  route.fulfill({ status: 200, body: testData });
});

In this code, any requests matching '**/api/fetch_data' will be intercepted.

Triggering the API Call

Next, perform actions on your web page that trigger the API call. For example:

await page.goto('https://ray.run');
await page.getByText('Update').click();

Waiting for the Response

Then, wait for the intercepted response using page.waitForResponse(). Like this:

const response = await page.waitForResponse('**/api/fetch_data');

Accessing the Response

Finally, access the response's properties. You can do something like:

console.log(response.status());
console.log(await response.json());

And that's it! You've just intercepted and recorded an API response. Remember, this is just one way to do it. Playwright offers other methods for handling network requests, so feel free to explore!

For more details, check out this blog post on API testing using Playwright.

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