I'm checking if Playwright would be good for testing our back-end services (so no front-end). I'm trying to log the response I'm getting from my API endpoint, but no luck so far. Am I missing something really obvious? It's my first time using Javascript, I've always used Java before.
Simplified version of the call:
test('test', async ({ request }) => {
const response = await request.get(<url>
, {
headers: {
'Authorization': Bearer ${jwt}
}
});
expect(response.ok()).toBeTruthy();
console.log('RESPONSE: ' + response.body);
});
This thread is trying to answer question "How to log the response from an API endpoint using Playwright?"
you could use attachemtns as well, which will be included in both UI mode and reports https://playwright.dev/docs/api/class-testinfo#test-info-attach
thanks for your response! I've been using the extension in VSCode, and checking the test output there, but I still can't see the API Response. I've already changed the log level in the extension settings ("DEBUG": "pw:api") so I can see more information, but still not seeing the response. I'll check out the attachments though, hopefully that'll get me there!
Related Ask AI answer for "What is the method to intercept and log API responses in Playwright?".
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.
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].