Rayrun

How do I make HTTP requests using Playwright?

Answer

To make a curl request with @playwright/test, you can use the built-in request fixture. This fixture respects configuration options like baseURL or extraHTTPHeaders and is ready to send requests. You can use the request fixture to do a stand-alone request from node.js to an API endpoint.

Here's an example of how to do a GET request using the request fixture:

import { test, expect } from '@playwright/test';

test('context fetch', async ({ request }) => {
  // Do a GET request
  const response = await request.get('https://ray.run/foo.json');
  // ...
});

To make a POST request, replace get with post and pass in data as an object:

import { test, expect } from '@playwright/test';

test('context fetch', async ({ request }) => {
  // Do a POST request
  const response = await request.post('https://ray.run/foo.json', {
    data: {
      key: 'value',
    },
  });
  // ...
});

Additionally, Playwright supports response interception by combining API testing with request interception. For example, you can blur all images on a web page by intercepting their responses and processing them with an image processing library like Jimp.

In summary, Playwright offers several ways to make HTTP requests, including built-in fixtures and standalone fixtures for advanced use cases.

Thank you!
Was this helpful?
Still have questions?

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

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