Playwright + Java · Lesson 31 of 38

0/38 done

Module 31: API Testing with Playwright Java

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

18 minlesson

Your task

Implement verifyResponseStatus:

  1. Accept APIResponse response and int expectedStatus.
  2. Return response != null && response.status() == expectedStatus.

Write it in the editor, then press Check answer (Ctrl + Enter).

Playwright APIRequestContext

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

Key Takeaways & Walkthrough Summary

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.

Common Doubts & Lesson Q&A

Why use Playwright's API client instead of RestAssured in Java?

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.

Java
Skip