
Playwright Java API Testing | How to test GET requests?

In the previous tutorial blog of the Playwright Java API Testing series, we learnt how to test POST requests.
In this tutorial blog, we will be discussing testing GET requests using Playwright Java.
Getting Started
The details about prerequisite, setup and configuration are already discussed in the previous tutorial blog. It is recommended to check out my previous blog if you are not familiar with project creation, set up and configuring Playwright with Java.
Application Under Test
We will be using free to use restful-ecommerce APIs from demo E-Commerce application that is available over GitHub.
Restful-Ecommerce — A simple demo node E-Commerce application for practising API Testing
The project can be set up locally using node or docker. It has multiple APIs related to order management, like creating, updating, fetching and deleting orders.
What is a GET Request?
GET request retrieves the data from the specified source. It is the most commonly used Http method. It does not require to send any request body for fetching the data from the source, however, filter parameters called Query Params and Path Params may be required to be provided as a header in order to filter and fetch the specified record.
The GET request generally returns the status code 200 along with the data that was requested for. Depending upon the Content-Type the data may be received in JSON format or XML format.
In the following example of restful-ecommerce APIs, there are 2 API endpoints that we will be testing in this blog.
- GET /getAllOrders endpoint — Gets all the available orders in the system

This API returns status Code 200 in the response with the order data in JSON format.

If no orders are found, it returns status code 404 with a message “No order found!!”
2. GET /getOrder endpoint — retrieves the order based on the filter applied on either order id, user id, product_id or all of them.

It will return status code 200 with the filtered order in the response otherwise will return status code 404 with message “No order found with the given parameter”
Having understood about the APIs under test, let’s now learn how to test Get APIs using Playwright Java.
How to test GET APIs using Playwright Java?
Let’s first discuss the test scenarios that we will be using to write the API tests using Playwright Java.
Test Scenario 1 — Getting all the Orders
- Start the service for restful-ecommerce app
- Hit the GET /getAllOrders endpoint to retrieve all the available orders in the system
- Check that the status code 200 is received in the response
- Check that the correct data is retrieved in the response
Note: We had already generated 4 orders using the POST request previously. We would be using the retrieving the same order data using this GET /getAllOrders endpoint
Test Implementation
Let’s create a new test method testShouldGetAllOrders() in the existing test class HappyPathTests.
Just to reiterate, two test classes have been created as previously HappyPathTests and SadPathTests, also a BaseTest class has been added to configure and set up Playwright. More details about this could be found in the previous blog here.
@Test
public void testShouldGetAllOrders() {
final APIResponse response = this.request.get("/getAllOrders");
final JSONObject responseObject = new JSONObject(response.text());
final JSONArray ordersArray = responseObject.getJSONArray("orders");
assertEquals(response.status(), 200);
assertEquals(responseObject.get("message"), "Orders fetched successfully!");
assertEquals(this.orderList.get(0).getUserId(), ordersArray.getJSONObject(0).get("user_id"));
assertEquals(this.orderList.get(0).getProductId(), ordersArray.getJSONObject(0).get("product_id"));
assertEquals(this.orderList.get(0).getTotalAmt(), ordersArray.getJSONObject(0).get("total_amt"));
}
The result of the API request will be stored in the response variable that is defined for the type APIResponse The GET request will be send using the this.request.get("/getAllOrders") which will handle the GET request and fetch the data from the system.
The response will be received using response.text() method of Playwright and will be stored in the responseObject variable that is of JSONObject type(it is a class of org.json library). The following is the response received in JSON format for the GET /getAllOrders endpoint.
{
"message": "Orders fetched successfully!",
"orders": [
{
"id": 1,
"user_id": "3",
"product_id": "331",
"product_name": "Aerodynamic Cotton Bench",
"product_amount": 416,
"qty": 4,
"tax_amt": 47,
"total_amt": 1711
},
{
"id": 2,
"user_id": "2",
"product_id": "331",
"product_name": "Incredible Linen Keyboard",
"product_amount": 716,
"qty": 1,
"tax_amt": 45,
"total_amt": 761
},
{
"id": 3,
"user_id": "2",
"product_id": "332",
"product_name": "Gorgeous Concrete Bench",
"product_amount": 900,
"qty": 2,
"tax_amt": 38,
"total_amt": 1838
},
{
"id": 4,
"user_id": "2",
"product_id": "331",
"product_name": "Ergonomic Concrete Gloves",
"product_amount": 584,
"qty": 2,
"tax_amt": 21,
"total_amt": 1189
}
]
}
As the outer part of the response is a JSON Object hence the values are stored using the JSONObject class.
final JSONObject responseObject = new JSONObject(response.text());
However, the order details are fetched as JSON Array hence, the order details are stored in the ordersArray variable.
final JSONArray ordersArray = responseObject.getJSONArray("orders");
Finally, the assertions will be performed to check that the GET /getAllOrders API returns status code 200. The response.status() method will return the status code.
The responseObject variable holds the response received, so, we would be getting the message field from this JSON object to check that it has the text “Orders fetched successfully!”
In our previous post, we used the OrderData class to generate the order data on run time using DataFaker and Lombok and in the POST request tests, we declared List with a variable named orderList. The same object will be called in the assertion to check the data integrity of the order details fetched. The following is the JSON Array that is fetched in the response.
"orders": [
{
"id": 1,
"user_id": "3",
"product_id": "331",
"product_name": "Aerodynamic Cotton Bench",
"product_amount": 416,
"qty": 4,
"tax_amt": 47,
"total_amt": 1711
},
{
"id": 2,
"user_id": "2",
"product_id": "331",
"product_name": "Incredible Linen Keyboard",
"product_amount": 716,
"qty": 1,
"tax_amt": 45,
"total_amt": 761
},
{
"id": 3,
"user_id": "2",
"product_id": "332",
"product_name": "Gorgeous Concrete Bench",
"product_amount": 900,
"qty": 2,
"tax_amt": 38,
"total_amt": 1838
},
{
"id": 4,
"user_id": "2",
"product_id": "331",
"product_name": "Ergonomic Concrete Gloves",
"product_amount": 584,
"qty": 2,
"tax_amt": 21,
"total_amt": 1189
}
]
We would be checking the first object from the order array.
{
"id": 1,
"user_id": "3",
"product_id": "331",
"product_name": "Aerodynamic Cotton Bench",
"product_amount": 416,
"qty": 4,
"tax_amt": 47,
"total_amt": 1711
}
The following lines of code will fetch the user_id , product_id and total_amt from the object and accordingly use the data that was supplied using orderList variable(data generated on runtime using Datafaker and Lombok).
assertEquals(this.orderList.get(0).getUserId(), ordersArray.getJSONObject(0).get("user_id"));
assertEquals(this.orderList.get(0).getProductId(), ordersArray.getJSONObject(0).get("product_id"));
assertEquals(this.orderList.get(0).getTotalAmt(), ordersArray.getJSONObject(0).get("total_amt"));
Test Execution
We first need to make sure that the restful-ecommerce app is up and working. Checkout the steps here to start the app on your local machine.
We will be executing the POST request(generated using previous blog)and the GET request using the following testng.xml file. The POST request will generate the orders and GET will retrieve the orders and perform the required assertion.
http://testng.org/testng-1.0.dtd">
The following screenshot from IntelliJ IDE shows that the test execution was successful and orders were fetched successfully.

Test Scenario 2 — Get Order filtering on order_id
- Start the service for restful-ecommerce app
- Hit the GET /getOrder endpoint to retrieve order filtering on order_id using Query parameter
- Check that the status code 200 is received in the response
- Check that the correct data is retrieved in the response
Test Implementation
Let’s create a new test method testShouldGetOrderUsingOrderId() and implement the test scenario 2 in it.
@Test
public void testShouldGetOrderUsingOrderId() {
final int orderId = 1;
final APIResponse response = this.request.get("/getOrder", RequestOptions.create().setQueryParam("id", orderId));
final JSONObject responseObject = new JSONObject(response.text());
final JSONArray ordersArray = responseObject.getJSONArray("orders");
assertEquals(response.status(), 200);
assertEquals(ordersArray.getJSONObject(0).get("id"), orderId);
assertEquals(responseObject.get("message"), "Order found!!");
}
Since we already have some orders created in the system. We would be fetching the order with order_id = 1
For retrieving the order with order_id filter we need to provide the order_id as a query parameter. This can be achieved using the following line of code:

The query parameter can be passed using the setQueryParam() method from the RequestOptions interface in Playwright. The parameter name and its required value needs to be supplied as a parameter to the setQueryParam() method.
Next, we need to parse the response object and get it in the JSON object format. The response.text() returns the response of the API in String format. This String will be converted to the parsed JSON object using the JSONObject class of org.json library.

The parsed JSON object will be further used to perform assertions. There won’t be any check related to data integrity here, we are checking that the response object has the same order_id that we gave in the request and the status code 200 is returned well in the response.
One step ahead, we are also checking that themessage field in the response has the text “Order found!!”
Test Execution
Let’s add this method to the same testng.xml file, restart the restful-ecommerce app so we have a fresh app with no data and run all the tests again.
http://testng.org/testng-1.0.dtd">
The following screenshot of the test execution shows that the tests were executed successfully and orders were fetched as per the scenario.

Test Scenario 3 — Get Order filtering on multiple Query Parameters
- Start the service for restful-ecommerce app
- Hit the GET /getOrder endpoint to retrieve order filtering on order_id , product_id and user_id query parameters
- Check that the status code 200 is received in the response
- Check that the correct data is retrieved in the response
Test Implementation
The test implementation of this test scenario is pretty much the same as we did for Test Scenario 2. The only addition here would be to add product_id and user_id query parameters to the test method.
A new test method testShouldGetOrdersUsingOrderIdProductIdAndUserId() is added to existing HappyPathTests class.
@Test
public void testShouldGetOrdersUsingOrderIdProductIdAndUserId() {
final int orderId = 2;
final String productId = "332";
final String userId = "2";
final APIResponse response = this.request.get("/getOrder", RequestOptions.create()
.setQueryParam("id", orderId)
.setQueryParam("product_id", productId)
.setQueryParam("user_id", userId));
final JSONObject responseObject = new JSONObject(response.text());
final JSONArray ordersArray = responseObject.getJSONArray("orders");
assertEquals(response.status(), 200);
assertEquals(responseObject.get("message"), "Order found!!");
assertEquals(ordersArray.getJSONObject(0).get("id"), orderId);
assertEquals(ordersArray.getJSONObject(0).get("product_id"), productId);
assertEquals(ordersArray.getJSONObject(0).get("user_id"), userId);
}
In this test method, the setQueryParam is called three times in order to pass on the required parameters.
The rest of the implementation related to parsing the response and checking the status code and message remains the same.
Additionally, there are assertions added in this method to check that the order details response received in the response has the given order_id , product_id and user_id . This assertion will ensure that the correct order details were retrieved as per the query parameters.
Test Execution
The following test execution result from IntelliJ shows that the order details as per the query parameters were fetched successfully

Summary
GET requests fetch the required data from the specified resource. It is the most commonly used HTTP method. It generally returns the data in the response with status code 200. GET requests can also consider the required filters using query param or path param for querying the data.
With GET requests, we need to validate that the data received in the response is correct or not. This validation would be for all the data fetched at once or as per the query parameters provided.
Playwright exposes the get() method in the APIRequestContext interface that could be used for retrieving the data. There are certain Request parameters like headers, query params, etc which are also supported.
I hope this blog provides you with the sufficient knowledge required to automate the GET requests using Playwright Java, do try your hands on it and let me know if you face any issue.
Happy Testing!