Rayrun
← Back to Discord Forum

API Testing - how to get REQUEST details?

I want to see somehow the request details for debugging purpose. Sadly I can only get the response details in the debugger. How should I see them? Note: using const resp = await request.post() in my tests

This thread is trying to answer question "How can I see the request details for debugging purposes during API testing?"

3 replies

I think I found the answer myself: running the tests in the --ui mode and observing the "Network" tab

is there another way? I am curious

hm.. it looks like the request is always empty - https://github.com/microsoft/playwright/issues/21951

Answer

Debugging API Requests with Playwright

You can debug API requests in your tests using Playwright's request fixture. This handy tool respects your playwright.config.ts file's configuration options like baseURL and extraHTTPHeaders.

Set your baseURL to the API endpoint you're testing. All your test requests will go to this endpoint. Add any required headers like authorization tokens to the extraHTTPHeaders object.

Here's a quick example of how to use the request fixture in a test:

const REPO = 'test-repo-1';
const USER = 'github-username';

test('should create a bug report', async ({ request }) => {
  const newIssue = await request.post(`/repos/${USER}/${REPO}/issues`, {
    data: {
      title: '[Bug] report 1',
      body: 'Bug description',
    },
  });

  expect(newIssue.ok()).toBeTruthy();

  const issues = await request.get(`/repos/${USER}/${REPO}/issues`);
  
  expect(issues.ok()).toBeTruthy();
  
  expect(await issues.json()).toContainEqual(expect.objectContaining({
    title: '[Bug] report 1',
    body: 'Bug description'
  }));
});

In this test, we're creating a bug report on GitHub. We send a POST request to create the bug report, then a GET request to retrieve all issues. We then check if our new bug report is in the list of issues.

The request fixture lets you inspect request details like headers, payload, and response status code. This makes it easier to debug your API requests and troubleshoot any issues.

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

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].