This thread is trying to answer question "Is it possible to log the API request in Playwright to see what exactly is being sent to the api endpoint?"
I am not sure if there is a way to have the request log to console, it would already be in the network traffic. But if you really require this, you could intercept the route, then console log the request after that fulfill the route. Have a look at Mocking requests and responses, you could use this in theory to intercept the request and pass it after logging it. But would be extra work, and you code may become hard to mainntain if not designed to handle this correctly. https://playwright.dev/docs/next/mock#modify-api-responses
This page suggests setting the DEBUG environment value:
DEBUG="pw:api"
Related Ask AI answer for "How do I log API requests in Playwright/test?".
Sure, you can log API requests in Playwright. It's a great way to see what's being sent to your API endpoint.
Playwright lets you intercept and modify network traffic. You can use the page.route()
method to handle network requests made by a page.
import { test, expect } from '@playwright/test';
test('log API request', async ({ page }) => {
await page.route('https://ray.run/api/endpoint', (route, request) => {
console.log(request.url());
route.continue();
});
await page.goto('https://ray.run');
});
Inside the callback function of page.route()
, you can log the request details. For instance, you can print out the URL of each request with console.log(route.request().url())
.
console.log(route.request().url());
If you need more details about the request, like its headers and payload data, you can use route.request().headers()
and route.request().postData()
.
console.log(route.request().headers());
console.log(route.request().postData());
By logging these details, you'll see exactly what's being sent to your API endpoint. It's a handy tool for debugging or verifying your application is sending the correct data.
For more on API testing with Playwright, check out this blog post.
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].