Get API Request using Playwright Java

Profile picture for user arilio666

This article will show how we can GET API requests in playwright using java.

reqres site shall be used for this example, and we will get the response code of the list of users, which should be 200.
We will also get the response code of the single user not found request.

Which should return 400.

        Playwright playwright = Playwright.create();
        Browser browser = playwright.chromium().launch(new BrowserType.LaunchOptions().setHeadless(false));
        Page page = browser.newPage();
        Response response = page.navigate("https://reqres.in/api/users?page=2");
        int status = response.status();
        System.out.println(status);
        assertEquals(status, 200);
        

So here, we took the navigate method and named its respective return type response.
With this, we got response status and asserted.
The same shall be done for the other endpoint of reqres.

https://reqres.in/api/users/23 can be used to get a 400 status code.

We can refer to the official documentation link for more operations using response.