Rayrun
โ† Back to Discord Forum

How to log the response from an API endpoint

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?"

9 replies

console.log would output it to console, if you using the VSCode extenstion you need to click show test output

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!

keep in mind the VSCode extenstion won't show the attachemnts

and I think you have a mistake in the log statment, it should be await response.body() not response.body

I've tried both body() and body, both didn't output anything usefull

did you try to await it? It's a promise

I'll give it a shot

yeah I got it now! I've tried await a few times, but probably not the combination with body(). Thank you so much ๐Ÿ˜„

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.

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 [email protected].