Rayrun

What is the method to intercept and log API responses in Playwright?

Answer

Sure, you can easily log API responses using Playwright. It's all about intercepting the network request and accessing the response data.

Here's a quick example:

await page.route('**/api/endpoint', async (route) => {
  const response = await route.fetch();
  const responseBody = await response.text();

  console.log('Response:', responseBody);
  
  await route.continue();
});

await page.goto('https://ray.run/api/endpoint');

In this code, page.route() intercepts requests that match the URL pattern **/api/endpoint. The route handler fetches and logs the response body with response.text(). Then, route.continue() ensures the original request proceeds.

By inspecting responseBody, you get detailed info about the API's response. You can extract specific fields or perform assertions on it as needed for your tests.

Remember, this is just one way to log API responses with Playwright. Depending on your needs, there might be other methods or techniques to explore.

References

Thank you!
Was this helpful?
Still have questions?

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

Related Discord Threads

Related Questions

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.