Understanding and capturing network call responses becomes indispensable for validating test outcomes, monitoring API interactions, and extracting valuable data when automating web applications. This article will delve into the intricacies of fetching network call responses in Playwright using Java.
Most modern web browsers have built-in developer tools that allow us to view and capture network calls.
We can access the developer tools by right-clicking on the webpage and selecting the “inspect” option.
Selecting the “Inspect” option will open the browser developer tools, where we can see a bunch of tabs. Select the “Network” tab to see and capture the network calls.
Now, let’s capture the network call responses using Playwright.
Capture network call responses using Playwright Java
The Playwright’s Page interface includes an onResponse() method that is triggered whenever a response status and headers are received.
One more method, “onRequestFinished()“, is fired when a request successfully finishes itself.
Note: onResponse() method is fired before the onRequestFinished() method.
We can subscribe to the onResponse() method, allowing us to perform any necessary operations whenever an event is triggered.
// capturing the network call responses
page.onResponse(response -> {
// write code here
});
Get status code of the network call
Response interface in Playwright provides a status() method which returns the status code of the network call.
page.onResponse(response -> {
System.out.println("Response Status: "+ response.status());
});
Get response headers
- The Response interface offers a headers() method that retrieves a network call’s response headers.
- But it doesn’t return the security-related headers, including cookie information.
- We can use the allHeaders() function to retrieve all headers, including cookie details.
page.onResponse(response -> {
System.out.println("Response Headers: "+ response.headers());
});
We can also get an individual header value using the headerValue() method.
Get Response Body of a network call
page.onResponse(response -> {
System.out.println("Response Body: " + response.text());
});
text() method returns the response body of a network call, but there is a catch here.
If a network call is redirected, we won’t be able to access the response body. In such situations, we’ll receive the following error message –
Exception in thread "main" com.microsoft.playwright.PlaywrightException: Error {
message='Response body is unavailable for redirect responses
name='Error
stack='Error: Response body is unavailable for redirect responses
As we know, status codes for redirected calls fall within the 3xx series. Therefore, we can create an if statement to retrieve the response body only if the request is not part of the 3xx series.
page.onResponse(response -> {
if (!String.valueOf(response.status()).startsWith("3")) {
System.out.println("Response Body: " + response.text());
}
});
Whole code combining status, headers and response body
import com.microsoft.playwright.Browser;
import com.microsoft.playwright.BrowserContext;
import com.microsoft.playwright.BrowserType;
import com.microsoft.playwright.Page;
import com.microsoft.playwright.Playwright;
public class CodekruTest {
public static void main(String[] args) {
Playwright playwright = Playwright.create();
// Launching the browser
Browser browser = playwright.chromium().launch(new BrowserType.LaunchOptions().setHeadless(false));
// creating a BrowserContext
BrowserContext browserContext = browser.newContext();
// creating the page
Page page = browserContext.newPage();
// capturing the network call responses
page.onResponse(response -> {
System.out.println("Response Status: " + response.status() + ", Response Headers: " + response.headers());
if (!String.valueOf(response.status()).startsWith("3")) {
System.out.println(", Response Body: " + response.text());
}
});
page.navigate("https://www.makemytrip.com");
browser.close();
playwright.close();
}
}
We hope that you have liked the article. If you have any doubts or concerns, please write to us in the comments or mail us at admin@codekru.com.