Last Updated On
Playwright can be used to get access to the REST API of the application.
In this tutorial, I will go through the steps to execute the POST requests in Playwright.
Table of Contents
- What is POST Request?
- Setup Your Playwright Testing Environment
- Update playwright.config.ts
- Writing the API Test
- Execute the tests through Test Explorer View
- Execute the tests through Command Line
- Evaluate Test Results
What is POST Request?
A POST request is a type of HTTP request used to send data to a server to create or update a resource. It is one of the most common methods used in web services for communicating between a client (such as a web browser) and a server.
1. Setup Your Playwright Testing Environment:
Verify that the Playwright installed in your project. If you haven’t yet installed it, you can use:
npm install playwright
2. Update playwright.config.ts
Playwright Test comes with a few built-in reporters for different needs and ability to provide custom reporters. We will add detail in playwright.config.ts to generate the html report, once the test execution is finished.
import { defineConfig } from '@playwright/test';
export default defineConfig({
reporter: 'html',
});
3. Writing the API Test
Here’s an example script demonstrating API testing for POST operation using Playwright:
import { test, expect } from '@playwright/test';
test('API Testing - POST with Playwright', async ({ request }) => {
// Perform a POST request
const postResponse = await request.post('https://jsonplaceholder.typicode.com/users', {
data: {
title: 'Manager',
body: 'Test',
userId: 10,
}
});
// Check the response status code
expect(postResponse.status()).toBe(201);
// Parse the response data
const postResponseBody = await postResponse.json();
console.log(postResponseBody);
// Validate the response
expect(postResponseBody).toHaveProperty('title', 'Manager');
expect(postResponseBody).toHaveProperty('body', 'Test');
expect(postResponseBody).toHaveProperty('userId', 10);
});
Explanation:
1. Imports
This line imports the test and expect functions from Playwright’s testing library. These functions provide a structure for writing tests and asserting conditions within them.
import { test, expect } from '@playwright/test';
2. Test Definition
This defines an asynchronous test case named “API Testing – POST with Playwright”. The test receives a request object as a parameter, which is used to perform HTTP requests.
test('API Testing - POST with Playwright', async ({ request })
3. Performing a POST Request
Sends a POST request to the specified URL. data transmits the body of the request.
const postResponse = await request.post('https://jsonplaceholder.typicode.com/users', {
data: {
title: 'Manager',
body: 'Test',
userId: 10,
}
});
4. Checking the Response Status Code
Retrieve HTTP status code for response validation.
expect(postResponse.status()).toBe(201);
5. Parsing the Response Data
Parse response data into JSON format
const postResponseBody = await postResponse.json();
6. Logging the Response
This outputs the response body to the console, allowing you to see the returned JSON data.
console.log(postResponseBody);
7. Perform assertions on the response
The assertions verify that the response contains expected data.
- Confirms the response contains a title property with the value Manager.
- Asserts that the body property matches ‘Test’.
- Checks that the userId property matches 10.
expect(postResponseBody).toHaveProperty('title', 'Manager');
expect(postResponseBody).toHaveProperty('body', 'Test');
expect(postResponseBody).toHaveProperty('userId', 10);
4. Execute the tests through Test Explorer View
Go to the funnel shape icon called “Testing” or “Test Explorer View”. You can run the tests from the test Explorer view too now. You can run all of the tests in all of the test specs in this directory.
You can also come in and run all of the tests in a particular specification file. Alternatively, run individual tests within a specification file. Here, I want to run the tests of api_post_tests.spec.ts file.

The output of the above program is

5. Execute the tests through Command Line
You can also run the tests through the command line. Go to the terminal and use the below command:-
npx playwright test api_post_tests.spec.ts
This command runs all the tests present in this file.

6. Evaluate Test Results
Analyze console outputs and assertions to validate the response data, ensuring that your API endpoints are functioning as designed.
Playwright generates a test report after the execution of the tests. This test report can be viewed by using the below command:
npx playwright show-report
The output of the above command is

Below is the Test Report generated by Playwright.


To know more about Playwright UI mode, please refer to this website – https://playwright.dev/docs.