What is a GET API?
- A GET API is an HTTP request method used to retrieve data from a server.
- It doesn’t change or update data; it only fetches information (for example: getting user details, product listings, weather data, etc.).
- Example: If you open https://jsonplaceholder.typicode.com/users, the server responds with user data in JSON format.
In automation, testing a GET API means checking whether the API:
- Returns the correct status code (e.g., 200 OK).
- Provides the expected response body (data correctness).
- Responds within an acceptable time (performance).
- Returns correct headers (e.g., content type, cache control).
How to Test GET API with Playwright Java
Playwright is not only for UI automation; it also provides an API testing capability.
1. Create APIRequestContext
- Playwright provides a special context to send API requests.
- This context allows you to perform GET, POST, PUT, DELETE requests without opening a browser.
2. Send GET Request
- Use the APIRequestContext to call the GET endpoint (e.g., /users, /posts).
- Playwright sends the request and stores the response.
3. Validate Response Status
- Check if the status code is what you expect (200 for success, 404 for not found, etc.).
4. Validate Response Body
- Extract the response in JSON or text format.
- Verify key fields and values (e.g., userId, name, email).
- Ensure response data structure matches the API contract.
5. Validate Headers
- Check headers like Content-Type to ensure the response is in the correct format (e.g., application/json).
6. Validate Response Time
- Ensure the API responds within performance thresholds.
Why Use Playwright for GET API Testing?
- Unified framework: You can test both UI and API in the same test suite.
- Useful for scenarios like:
Validation → Compare UI results with API results for consistency.
- Lightweight and fast compared to browser-based UI automation.
To test a GET API using Playwright in Java, you can use Playwright’s built-in APIRequestContext to send HTTP requests. This is useful for API testing in addition to browser automation.
Steps to Test a GET API using Playwright Java:
- Initialize Playwright and APIRequestContext
- Send GET request to the API endpoint
- Validate the response: Status code, body content, headers, etc.
- Close Playwright
Example: Test GET API in Playwright Java
Let’s test a public API like:https://jsonplaceholder.typicode.com/posts/1
Maven Dependency
<dependency> <groupId>com.microsoft.playwright</groupId> <artifactId>playwright</artifactId> <version>1.43.0</version> <!-- use latest --> </dependency>
Java Code to Test GET API
import com.microsoft.playwright.*; import com.microsoft.playwright.options.*; public class GetApiTest { public static void main(String[] args) { // Step 1: Initialize Playwright try (Playwright playwright = Playwright.create()) { // Step 2: Create APIRequest context APIRequest request = playwright.request(); APIRequestContext requestContext = request.newContext(); // Step 3: Send GET request APIResponse response = requestContext.get("https://jsonplaceholder.typicode.com/posts/1"); // Step 4: Validate the response System.out.println("Status: " + response.status()); // 200 expected System.out.println("Status Text: " + response.statusText()); // Step 5: Validate body content String responseBody = response.text(); System.out.println("Response Body: \n" + responseBody); // Optional: Assert status code and content if (response.ok()) { System.out.println("API responded successfully."); } else { System.out.println("API test failed."); } // Close API request context requestContext.dispose(); } } }
Code explanation:
(a) Step 1: Initialize Playwright
(b) Step 2: Create APIRequest context
(c) Step 3: Send GET request
(d) Step 4: Validate the response
(e) Step 5: Validate body content
(f) Optional: Assert status code and content
(g) Close API request context
Expected Output:
Status: 200 Status Text: OK Response Body: { "userId": 1, "id": 1, "title": "sunt aut facere repellat provident occaecati excepturi optio reprehenderit", "body": "..." } API responded successfully.
As shown above, status code of API is 200, that means API is successfully returned the response. And response body is given in JON format.
You Can Also Validate Specific Fields:
import org.json.JSONObject;
import org.testng.*;JSONObject json = new JSONObject(response.text()); int id = jsonObject.getInt("id"); Assert.assertEquals(id, 1); System.out.println("JSON fields validated.");
For validating response, we can use JSONObject class.
Suggested Posts:
1. Automate POST 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