31. Full-Stack API Testing & Hybrid API-to-UI Validation

Playwright APIRequestContext

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();

Task

  • Implement verifyResponseStatus:
  • 1. Accept APIResponse response and int expectedStatus.
  • 2. Return response != null && response.status() == expectedStatus.
πŸ’‘Common doubts
Did you like the problem?
Lesson 31 of 38
Java
Loading code editor…
Next