Playwright + Java · Lesson 31 of 38
Module 31: API Testing with Playwright Java
Your task
Implement verifyResponseStatus:
Write it in the editor, then press Check answer (Ctrl + Enter).
import com.microsoft.playwright.*;
import com.microsoft.playwright.options.RequestOptions;
import static org.junit.jupiter.api.Assertions.*;
APIRequestContext request = playwright.request().newContext(
new APIRequest.NewContextOptions().setBaseURL("https://api.targetqa.com")
);
// POST request creating a new course:
String payload = "{"title": "Playwright Java", "category": "Automation"}";
APIResponse response = request.post("/api/v1/courses",
RequestOptions.create().setHeader("Content-Type", "application/json").setData(payload));
assertEquals(201, response.status());
assertTrue(response.text().contains("Playwright Java"));
// Clean up:
request.dispose();Playwright isn't just for UI; it contains a world-class HTTP client called APIRequestContext. In this lesson, you will make GET, POST, PUT, and DELETE requests, validate JSON payloads and status codes, and seed test data via API before verifying in the UI. Let's master API testing!
Part of the Playwright + Java course (Playwright with Java). Practice this concept live in the Playwright Online Compiler or explore our free automation practice form.
Playwright's APIRequestContext shares cookies and storage state directly with your BrowserContext, making hybrid API-to-UI workflows seamless and faster.
Ready? Solve the task in the editor
Use Check answer to mark it complete. Hints are there if you need them.
___