How to Test DELETE API By Playwright























What Is a DELETE API? Complete Guide to Testing DELETE API Using Playwright Java

In modern web applications and RESTful services, APIs are the backbone of communication between client and server. While GET retrieves data, POST creates new resources, and PUT updates existing ones, the DELETE API is responsible for removing resources from the server.

If you are an automation tester, backend developer, or preparing for API automation interviews, understanding how DELETE API works and how to test it using Playwright Java is extremely important.

In this comprehensive guide, you will learn:

  • What is a DELETE API?

  • How DELETE API works in REST architecture

  • Common response status codes

  • Why DELETE API testing is important

  • Step-by-step guide to testing DELETE API using Playwright Java

  • Java code example with explanation

  • How to validate deletion properly

  • Negative test scenarios

  • Best practices for DELETE API testing

This article is written in a natural, human-friendly tone, fully SEO optimized, and AdSense compliant for blog publishing.


What Is a DELETE API?

A DELETE API is an HTTP method used in RESTful web services to remove an existing resource from the server.

It typically requires a unique identifier (such as an ID) to specify which resource should be deleted.

Simple Definition

DELETE = Remove an existing resource from the server


Example of DELETE API

If you have a user stored in a database with ID 123, you can delete that user using:

DELETE /users/123

This request tells the server to remove the user with ID 123 permanently.


Common Response Status Codes for DELETE API

When testing a DELETE API, understanding response codes is essential.

Status CodeMeaning
200 OKDeletion successful (with response body)
204 No ContentDeletion successful (no response body)
404 Not FoundResource does not exist
401 UnauthorizedAuthentication required
403 ForbiddenPermission denied
500 Internal Server ErrorServer-side issue

Most DELETE APIs return 204 No Content when deletion is successful.


Why Test DELETE API?

Testing a DELETE API is critical to ensure system integrity and data safety.

Here’s why:

1. Confirm Resource Is Actually Removed

Just because API returns success doesn’t mean data is deleted. Testing verifies actual deletion.

2. Validate Correct Status Codes

Ensures server follows API contract.

3. Check Unauthorized Access Handling

Ensures only authorized users can delete data.

4. Prevent Accidental Deletions

Ensures no unintended resources are removed.

5. Validate Error Scenarios

Ensures proper handling of invalid or non-existing resource IDs.

In production systems, improper DELETE API handling can lead to major data loss issues.


How DELETE API Works in REST Architecture

The flow is simple:

  1. Client sends DELETE request with resource ID.

  2. Server verifies authentication and authorization.

  3. Server removes resource from database.

  4. Server returns appropriate HTTP status code.

Example:

DELETE https://example.com/api/users/2


Testing DELETE API Using Playwright Java

Many people know Playwright as a UI automation tool. However, it also provides powerful API testing capabilities using APIRequestContext.

With Playwright Java, you can:

  • Send DELETE requests

  • Send GET, POST, PUT requests

  • Validate status codes

  • Validate headers

  • Verify resource deletion

  • Combine API and UI automation

And all of this without launching a browser.


Example Scenario

We will test this DELETE API:

https://reqres.in/api/users/2

This example API is taken from reqres.in for demonstration purposes.


Step-by-Step Guide to Test DELETE API in Playwright Java

Let’s go through the steps in detail.


Step 1: Add Maven Dependency

Add the Playwright dependency in your pom.xml file:

<dependency> <groupId>com.microsoft.playwright</groupId> <artifactId>playwright</artifactId> <version>1.44.0</version> </dependency>

Always use the latest stable version available.


Step 2: Java Code to Test DELETE API

Here is the complete working example:

import com.microsoft.playwright.*; 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") .setExtraHTTPHeaders(Map.of( "Content-Type", "application/json" )); APIRequestContext request = playwright.request().newContext(options); // Send DELETE request APIResponse response = request.delete("/api/users/2"); // Print response details 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 (Step-by-Step)

Let’s break down the code for better understanding.


(a) Set Base URL

.setBaseURL("https://reqres.in")

This defines the base API endpoint.

Instead of writing full URL every time, we only use relative path.


(b) Add Headers

.setExtraHTTPHeaders(Map.of("Content-Type", "application/json"))

Headers are necessary when API expects specific content type or authentication tokens.


(c) Send DELETE Request

request.delete("/api/users/2");

This sends DELETE request to remove user with ID 2.


(d) Validate Status Code

response.status();

Expected successful status = 204 No Content


(e) Close Request Context

request.close();

Always close context to release resources.


Expected Output

Status Code: 204 Status Text: No Content DELETE API test passed

This confirms that the resource was successfully deleted.


How to Verify Resource Deletion (Important Step)

A professional way to confirm deletion is:

  1. Send DELETE request.

  2. Send GET request for same ID.

  3. Validate GET returns 404 Not Found.

Example:

APIResponse getResponse = request.get("/api/users/2"); System.out.println("GET after DELETE Status: " + getResponse.status());

Expected:

404 Not Found

This confirms the resource is permanently removed.


Negative Testing for DELETE API

Always test negative scenarios.

1. Delete Non-Existing Resource

DELETE /users/9999

Expected response:

404 Not Found


2. Unauthorized Delete Request

If authentication token is missing:

Expected:

401 Unauthorized


3. Forbidden Request

If user doesn’t have permission:

Expected:

403 Forbidden

Negative testing ensures system security and stability.


Measuring Response Time

Performance matters in API testing.

Example:

long start = System.currentTimeMillis(); APIResponse response = request.delete("/api/users/2"); long end = System.currentTimeMillis(); System.out.println("Response Time: " + (end - start) + " ms");

You can define acceptable threshold such as:

  • Response time < 2000 ms


Best Practices for DELETE API Testing

Here are professional best practices:

1. Always Validate Status Code

Never assume success.

2. Confirm Actual Deletion

Send GET request after DELETE.

3. Test Security

Validate authentication and authorization.

4. Test Edge Cases

Invalid IDs
Large data sets
Concurrent deletion requests

5. Use Assertions Framework

Integrate with TestNG or JUnit for structured validation.


Combining API and UI Testing

Playwright allows you to combine API and UI tests efficiently.

Example scenario:

  1. Use POST API to create user.

  2. Use DELETE API to remove user.

  3. Launch browser.

  4. Verify deleted user does not appear in UI.

This approach increases automation reliability and speed.


Common Interview Questions on DELETE API

  1. What is DELETE API?

  2. What status code is returned for successful DELETE?

  3. What does 204 No Content mean?

  4. How do you confirm resource deletion?

  5. How to automate DELETE API using Playwright?

  6. What are common negative scenarios?

If you are preparing for automation interviews, mastering DELETE API testing is essential.


Advantages of API Testing

  • Faster than UI testing

  • More stable and reliable

  • Detect backend issues early

  • Suitable for CI/CD pipelines

  • Supports microservices testing

Modern automation frameworks always include API testing as a core component.


Conclusion

A DELETE API is used to remove an existing resource from the server in RESTful applications. Proper testing ensures that:

  • Resource is actually deleted

  • Correct status codes are returned

  • Unauthorized access is blocked

  • No unintended data is affected

Using Playwright Java, you can:

  • Send DELETE requests

  • Validate status codes

  • Confirm deletion with GET request

  • Measure response time

  • Handle negative scenarios

  • Integrate API and UI testing seamlessly

If you are learning automation testing or preparing for interviews, mastering DELETE API testing with Playwright will significantly enhance your skills.


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

How to Test PUT API by Playwright

















What Is a PUT API? Complete Guide to Testing PUT API Using Playwright Java

In modern web applications and RESTful services, APIs are responsible for communication between client and server. While GET is used to retrieve data and POST is used to create data, the PUT API plays a crucial role when it comes to updating existing resources.

If you are an automation tester, backend developer, or preparing for API automation interviews, understanding PUT API and how to test it using Playwright Java is extremely important.

In this comprehensive guide, you will learn:

  • What is a PUT API?

  • How PUT API works in REST architecture

  • Key characteristics of PUT method

  • Difference between PUT and POST

  • Real-world use cases

  • How to test PUT API using Playwright Java

  • Step-by-step explanation with Java code

  • How to validate status code, response body, and headers

  • Error handling and negative testing scenarios

  • Best practices for PUT API testing

This article is written in a natural, human-like tone, fully SEO optimized and AdSense-friendly for blog publishing.


What Is a PUT API?

A PUT API is an HTTP request method used in RESTful web services to update or replace an existing resource on the server.

Unlike POST, which is typically used to create new resources, PUT is generally used when the client wants to update an existing record.

Simple Definition

PUT = Update or Replace existing resource


Example of PUT API

Imagine you have a user profile stored in a database:

{ "id": 2, "name": "Rahul", "job": "Tester" }

If you want to update this user’s job to “Software Developer”, you send a PUT request with the updated information.

After PUT request:

{ "id": 2, "name": "Rahul", "job": "Software Developer" }

The existing resource is updated.


Important Characteristics of PUT API

Understanding these characteristics is essential for interviews and real projects.

1. Idempotent Nature

PUT is idempotent.

This means:

Sending the same PUT request multiple times results in the same final state.

For example:
If you update a user’s job to "Developer" 10 times using the same request, the result remains the same.

This is different from POST, where multiple requests may create multiple resources.


2. Complete Resource Replacement

Traditionally, PUT requires sending the complete representation of the resource.

If partial updates are needed, PATCH method is used instead.

However, some APIs allow partial updates using PUT, depending on implementation.


Real-World Use Cases of PUT API

PUT APIs are commonly used in real-world applications such as:

  • Updating user profile details

  • Changing password

  • Updating product information in e-commerce

  • Editing blog posts

  • Updating employee records

  • Modifying order details

Any scenario where an existing record needs to be updated uses PUT API.


Difference Between PUT and POST API

Many beginners get confused between PUT and POST.

Here is a clear comparison:

FeaturePOSTPUT
PurposeCreate new resourceUpdate existing resource
IdempotentNoYes
Resource URLUsually collection endpointSpecific resource endpoint
Example/users/users/2

Example:

POST → Create new user
PUT → Update user with ID 2


Testing a PUT API with Playwright Java

Playwright is mainly known for UI automation, but it also provides powerful API testing capabilities.

Using Playwright Java, you can:

  • Send PUT requests

  • Send GET requests

  • Send POST requests

  • Send DELETE requests

  • Validate responses

  • Combine API and UI testing

And the best part? No browser launch is required for API testing.


Steps to Test a PUT API in Playwright Java

Let’s understand step-by-step how to test PUT API using Playwright in Java.


Example Scenario

We will update a user using this endpoint:

https://reqres.in/api/users/2

Method:

PUT


Sample JSON Request Body

{ "name": "Himanshu", "job": "Software Developer" }

This JSON will update the user’s name and job.


Step 1: Initialize Playwright and APIRequestContext

We need to create a request context to interact with APIs.


Step 2: Java Code to Test PUT API

Here is the complete working example:

import com.microsoft.playwright.*; import com.microsoft.playwright.options.*; import java.util.*; public class PutApiTest { public static void main(String[] args) { try (Playwright playwright = Playwright.create()) { APIRequest request = playwright.request(); APIRequestContext requestContext = request.newContext(new APIRequest.NewContextOptions() .setBaseURL("https://reqres.in/api")); // Prepare JSON request body Map<String, Object> data = new HashMap<>(); data.put("name", "Himanshu"); data.put("job", "Software Developer"); // Send PUT request APIResponse response = requestContext.put("/users/2", RequestOptions.create() .setHeader("Content-Type", "application/json") .setData(data) ); // Print status code System.out.println("Status Code: " + response.status()); // Print response body System.out.println("Response Body: " + response.text()); // Validate status code if (response.ok()) { System.out.println("PUT request successful."); } else { System.out.println("PUT request failed."); } } } }


Code Explanation (Step-by-Step)

Let’s break it down in simple language.


(a) Create APIRequestContext

APIRequestContext requestContext = request.newContext();

This creates a lightweight HTTP client for sending API requests.

We also set a base URL:

.setBaseURL("https://reqres.in/api")

So we don’t need to repeat the full URL.


(b) Prepare JSON Request Body

Map<String, Object> data = new HashMap<>();

We create a Map object and add key-value pairs to simulate JSON.


(c) Send PUT Request

requestContext.put("/users/2", options);

This sends a PUT request to update user with ID 2.

We also add:

  • Content-Type header

  • JSON payload


(d) Print Status Code

response.status();

Expected successful status code = 200 OK


(e) Print Response Body

response.text();

This prints the JSON response from server.


(f) Validate Status Code

response.ok();

Returns true if status code is between 200–299.


Expected Output

Status Code: 200 Response Body: { "name": "Himanshu", "job": "Software Developer", "updatedAt": "2025-07-28T12:13:45.012Z" } PUT request successful.

Status 200 means update was successful.

The response also contains updated timestamp.


How to Validate Specific Fields

You can validate response fields like this:

String name = response.json().get("name").toString(); System.out.println("Updated Name: " + name);

Or use JSONObject for better validation:

import org.json.JSONObject; JSONObject jsonObject = new JSONObject(response.text()); String job = jsonObject.getString("job"); System.out.println("Updated Job: " + job);


Negative Testing for PUT API

Professional API testing always includes negative scenarios.

1. Invalid Data

If you send invalid JSON:

Expected status → 400 Bad Request


2. Non-Existing Resource

If you update:

/users/9999

Expected status → 404 Not Found


3. Unauthorized Access

If authorization token is missing:

Expected status → 401 Unauthorized

Always test error scenarios.


Measuring Response Time

Performance testing is important.

Example:

long start = System.currentTimeMillis(); APIResponse response = requestContext.put("/users/2", options); long end = System.currentTimeMillis(); System.out.println("Response Time: " + (end - start) + " ms");

You can define threshold like:

  • Should respond within 2000 ms


Best Practices for PUT API Testing

Here are some professional tips:

1. Always Validate Status Code

Never assume API works correctly.

2. Validate Response Schema

Ensure response matches API documentation.

3. Test Idempotency

Send same PUT request multiple times and verify result remains same.

4. Use Assertions Framework

Integrate with TestNG or JUnit.

5. Store Base URL in Config File

Avoid hardcoding.


Combining API and UI Testing

Playwright allows powerful integration.

Example flow:

  1. Use PUT API to update user profile.

  2. Launch browser.

  3. Login using that user.

  4. Validate updated details appear in UI.

This improves automation efficiency.


Common Interview Questions on PUT API

  1. What is PUT API?

  2. What is idempotent method?

  3. Difference between PUT and PATCH?

  4. What status code indicates successful update?

  5. How to test PUT API using Playwright?

  6. How to handle negative scenarios?

Mastering these concepts will help in automation interviews.


Advantages of API Testing

  • Faster than UI testing

  • More stable

  • Detect backend issues early

  • Supports CI/CD pipelines

  • Useful in microservices architecture

Modern automation frameworks always include API testing.


Conclusion

A PUT API is used to update or replace an existing resource in RESTful services. It is idempotent and typically requires a complete representation of the resource in the request body.

Using Playwright Java, you can easily:

  • Send PUT requests

  • Add headers

  • Pass JSON payload

  • Validate status codes

  • Validate response body

  • Test performance

  • Combine API and UI automation

If you are learning automation testing or preparing for interviews, mastering PUT API testing with Playwright is highly recommended.


Suggested Posts:

1. Automate GET API in Playwright
2. Automate POST API in Playwright
3. Automate DELETE API in Playwright
4. Automate Lombok API in Playwright
5. Test API by POJO Class in Playwright