1. What is a DELETE API?
- A DELETE API is an HTTP method used in RESTful web services to remove a resource from the server.
- It typically requires an identifier (like id) of the resource to be deleted.
- Example: DELETE /users/123 would delete the user with ID 123.
- The response usually confirms deletion with a status code:
404 Not Found → resource doesn’t exist.
401/403 → authentication or authorization issues.
Why test DELETE API?
Testing ensures that:
- The resource is actually removed from the server.
- The correct HTTP status codes are returned.
- Unauthorized or invalid requests are handled properly.
- No unintended resources are affected.
- Initialize an API request context with the base URL and authentication headers if needed.
- Use the DELETE method with the resource endpoint (like /users/{id}).
- Attach headers (Authorization, Content-Type) if required.
- Check if the response status code matches expectations (e.g., 204 for success).
- Some APIs return a confirmation message or the deleted resource details.
- Validate the message or structure if it exists.
- Optionally send a GET request for the same resource ID.
- Ensure the response is 404 Not Found (confirming the resource is gone).
To test a DELETE API using Playwright in Java, you can use Playwright’s ability to perform raw HTTP requests using the APIRequestContext
interface.
Steps to Test DELETE API in Playwright Java
- Create Playwright and APIRequestContext instance
- Send DELETE request using
delete()
method - Validate response (status code, body, etc.)
- Close the context and playwright
<dependency> <groupId>com.microsoft.playwright</groupId> <artifactId>playwright</artifactId> <version>1.44.0</version> </dependency>
import com.microsoft.playwright.*; import java.util.HashMap; import java.util.Map; public class DeleteAPITest { public static void main(String[] args) { try (Playwright playwright = Playwright.create()) { // Set up base URL and headers APIRequest.NewContextOptions options = new APIRequest.NewContextOptions() .setBaseURL("https://reqres.in") // Example DELETE test API .setExtraHTTPHeaders(Map.of( "Content-Type", "application/json" )); APIRequestContext request = playwright.request().newContext(options); // Send DELETE request APIResponse response = request.delete("/api/users/2"); // Print and assert the response System.out.println("Status Code: " + response.status()); System.out.println("Status Text: " + response.statusText()); if (response.status() == 204) { System.out.println("DELETE API test passed"); } else { System.out.println("DELETE API test failed"); } request.close(); } } }
Code explanation
- setBaseURL("https://reqres.in") sets the base URL.
- .delete("/api/users/2") sends the DELETE request to delete user with ID 2.
- The expected status code is 204 No Content — common for successful DELETEs.
- You can also validate the response body, headers, etc., if needed.
Suggested Posts:
1. Automate GET API in Playwright
2. Automate POST API in Playwright
3. Automate PUT API in Playwright
4. Automate Lombok API in Playwright
5. Test API by POJO Class in Playwright