
Playwright Java API Testing | How to test DELETE requests?

In the previous tutorial blog, we learned to write test automation scripts for PATCH API requests using Playwright Java.
In this blog, we will discuss and learn about DELETE API requests and how to handle them using Playwright Java. The following points will be covered in this tutorial blog:
- What is a DELETE request?
- How to test DELETE APIs using Playwright Java?What is a PATCH Request?
What is a DELETE request?
A DELETE API request deletes the specified resource from the server. Ideally, there is no response body in the DELETE requests.
The resource is specified by a URI, and the server permanently deletes it. DELETE requests are neither considered safe nor idempotent, as they may cause side effects on the server, like removing data from a database.
The following are some of the limitations of DELETE requests:
- The data deleted using DELETE request is not reversible so it should be handled carefully.
- It is not considered to be a safe method as it can directly delete the resource from the database causing conflicts in the system.
- It is not an idempotent method, meaning calling it multiple times for the same resource may result in different states. For example, in the first instance when DELETE is called it will return 204 status code stating that the resource has been deleted and if DELETE is called again on the same resource it may give a 404 NOT FOUND as the given resource is already deleted.
The following is an example of the DELETE API endpoint from the restful-ecommerce project. This same API will be further used in this blog for demo purposes for showcasing the testing of DELETE requests using Playwright Java.
DELETE /deleteOrder/{id} — Deletes an order by ID

This API requires the order_id to be supplied as Path Parameter in order to fetch for the respective order and delete it. There is no request body required to be provided in this request. However, as a security measure, the token is required to be provided as a header to delete the order.
Once the API is executed, it deletes the specified order from the system and returns Status Code 204.
In case of the order is not found or the token is not valid or not provided it will accordingly show the following response:
- Status Code — 400 — Failed to authenticate the token
- Status Code — 404 — When no order with the given order_id is found in the system
- Status Code 403 — When token is missing in the request
How to test DELETE APIs using Playwright Java?
API test automation can be performed using Playwright Java as it provides the required methods that could be used for writing the API test automation scripts.
Getting Started
In my previous tutorial blog, I have provided details about the prerequisite, setup and configuration of Playwright framework with Java. Do check it out to get yourself clarified with the basics before we move on to handling DELETE API requests.
Application Under test
We will be using the restful-ecommerce APIs that are free to use and are available over GitHub
Restful-Ecommerce — A simple demo node E-Commerce application for practising API Testing
Test Scenario 1 — Delete a valid order
- Start the restful-ecommerce service
- Using POST request create some orders in the system
- Delete the order with order_id “1” using DELETE request
- Check that the status code 200 is returned in the response

Test Implementation
The following steps are required to be performed to implement this test scenario:
- Add new orders using POST request
- Hit the /auth API to generate token
- Hit the /deleteOrder/ API endpoint with the token and the order_id to delete the order.
A new test method — testShouldDeleteTheOrder() is created in the existing test class HappyPathTests. This method performs all the 3 steps we just discussed.
@Test
public void testShouldDeleteTheOrder() {
final APIResponse authResponse = this.request.post("/auth", RequestOptions.create().setData(getCredentials()));
final JSONObject authResponseObject = new JSONObject(authResponse.text());
final String token = authResponseObject.get("token").toString();
final int orderId = 1;
final APIResponse response = this.request.delete("/deleteOrder/" + orderId, RequestOptions.create()
.setHeader("Authorization", token));
assertEquals(response.status(), 204);
}
The POST /auth API endpoint is hit first to generate the token and use it in the test further . The token received in response is stored in the token variable. This variable will be further used in the test for executing the DELETE request.

Next, new orders will be generated using the testShouldCreateNewOrders() method that is already discussed in the previous tutorial where we talked about the POST request testing using Playwright Java.
After the orders are generated, the next and final step is to hit the DELETE request with the validorder_id

We will be deleting the order with the order_id “1”. The delete() method provided by Playwright will delete the resource.
Accordingly, when the order is deleted, the Status Code 204 is returned in response which will be asserted. This is the only assertion that we can perform in case of delete as there is no body returned in response in case of delete.
Test Execution
We will be creating a new testng.xml named“testng-restfulecommerce-deleteorders.xml”. This xml file will allow us to execute the tests as per the priority we define in it.
http://testng.org/testng-1.0.dtd">
So, the first testShouldCreateNewOrders() test method will be executed and it will create new orders. Next, the testShouldDeleteTheOrder() test method order will be executed to test the delete order API.
The following screenshot of the test execution performed using IntelliJ IDE shows that the tests were executed successfully.

Now, let’s verify that the order was correctly deleted by writing a new test that will call the GET /getOrder API endpoint with the deleted order_id
Test Scenario 2 — Retrieve the deleted order
- Delete a valid order with order_id “1”
- Using GET /getOrder API try retrieving the order with order_id “1”
- Check that the Status Code 404 is returned with the message “No Order found with the given parameters!” in the response.
Test Implementation
Let’s create a new test method testShouldNotRetrieveDeletedOrder() in the existing class HappyPathTests.
@Test
public void testShouldNotRetrieveDeletedOrder() {
final int orderId = 1;
final APIResponse response = this.request.get("/getOrder", RequestOptions.create().setQueryParam("id", orderId));
assertEquals(response.status(), 404);
final JSONObject jsonObject = new JSONObject(response.text());
assertEquals(jsonObject.get("message"), "No Order found with the given parameters!");
}
The test implementation of this scenario is pretty simple, we will be executing the GET /getOrder API and to fetch the deleted order with order_id “1”.
Checks are applied next to verify that the GET API should return the Status Code 404 in the response with the message “No Order found with the given parameters!”.
This test ensures that the delete order API worked fine and the order was deleted from the system.
Test Execution
Let’s update the testng.xml file and add this test at the end after the delete test.
http://testng.org/testng-1.0.dtd">
Now, all the three tests should run in sequence. The first one will create orders, the second one will delete the order with order_id “1” and the last test will hit the GET API to fetch the order with order_id “1” returning Status code 404.

The screenshot above shows that all the three tests were executed successfully.
Summary
DELETE API requests allow deleting the resource from the system. As delete is an important CRUD function, it is important to test it and verify that the system is working as expected. However, it should be noted that Delete is an irreversible process so it should always be used with care.
We learnt how to perform DELETE API automation testing using Playwright with Java. I would recommend to trying your hands with Playwright and learning how to perform API testing using it.
Happy Testing!