What is POST API?
A POST API is an API endpoint that allows clients to send data to a server so that the server can process it and often create a new resource. Unlike a GET API, which is only used to retrieve data, a POST API typically involves submitting information such as form data, JSON payloads, or file uploads. For example, creating a new user account, submitting a login form, or uploading a file are common use cases of POST APIs.
How to Test a POST API with Playwright Java
Playwright is primarily a UI automation tool, but it also provides ways to send API requests directly without interacting with the browser. In Java, you can use Playwright’s APIRequestContext to test POST APIs. The high-level steps are:
Create an APIRequestContext
- Initialize a Playwright request context that acts as a client for making API calls.
- This context allows you to set a base URL, headers, or authentication details.
Prepare the POST Request
- Define the API endpoint you want to test.
- Construct the body of the request, which might include JSON, form-data, or raw text.
- Attach required headers such as Content-Type: application/json or authorization tokens if needed.
Send the POST Request
- Use the context to send the POST request along with the payload.
- The server processes the request and returns a response.
Validate the Response
- Check the status code (e.g., 200 for success, 201 for resource created, 400 for bad request).
- Validate the response body to ensure that the server returned the correct data or confirmation.
- Verify response headers if necessary.
Assertions in TestNG/JUnit
- In actual testing, you would assert expected vs. actual values.
- For example, if you created a new user, you’d assert that the response contains the user ID or success message.
Integration with UI Tests (Optional)
- Sometimes you may chain API and UI testing.
- For example: Use the POST API to create a user. Then, log in with that user in the UI using Playwright’s browser automation.
Steps to Test a POST API using Playwright Java:
- Initialize Playwright and create an APIRequestContext.
- Use
post()
method to send a POST request. - Pass headers, payload (body), and endpoint URL.
- Read and validate the response (status code, body, etc.).
- Close the context.
Example: POST API Test in Playwright Java
Let’s take an example of a fake JSON placeholder API:
Endpoint: https://jsonplaceholder.typicode.com/posts
Method: POST
Payload:
{ "title": "foo", "body": "bar", "userId": 1 }
import com.microsoft.playwright.*; import java.util.*; public class PostApiTest { public static void main(String[] args) { try (Playwright playwright = Playwright.create()) { // Create APIRequestContext APIRequest apiRequest = playwright.request(); APIRequestContext requestContext = apiRequest.newContext(); // Define the payload Map<String, Object> payload = new HashMap<>(); payload.put("title", "foo"); payload.put("body", "bar"); payload.put("userId", 1); // Send POST request APIResponse response = requestContext.post("https://jsonplaceholder.typicode.com/posts", RequestOptions.create() .setHeader("Content-Type", "application/json") .setData(payload) ); // Validate response System.out.println("Status Code: " + response.status()); System.out.println("Response Body: " + response.text()); if (response.status() == 201) { System.out.println("POST API test passed!"); } else { System.out.println("POST API test failed!"); } // Close context requestContext.dispose(); } } }
Status Code: 201 Response Body: { "title": "foo", "body": "bar", "userId": 1, "id": 101 } POST API test passed!
Above is the output, having status code 201 as this is POST request and response body is JSON object which can be validate by JSONObject class
Suggested Posts:
1. Automate GET API in Playwright
2. Automate PUT API in Playwright
3. Automate DELETE API in Playwright
4. Automate Lombok API in Playwright
5. Test API by POJO Class in Playwright