/**
* To capture browser network logs
*/
public void captureBrowserNetworkLogs() {
if (getBrowserName().equalsIgnoreCase("CHROME") || getBrowserName().equalsIgnoreCase("EDGE")) {
if (getBrowserName().equalsIgnoreCase("CHROME")) {
devTools = ((ChromeDriver) driver).getDevTools();
}
if (getBrowserName().equalsIgnoreCase("EDGE")) {
devTools = ((EdgeDriver) driver).getDevTools();
}
// Create a session with DevTools
devTools.createSession();
devTools.send(Network.clearBrowserCache());
devTools.send(Network.setCacheDisabled(true));
// Enable network tracking
devTools.send(
Network.enable(java.util.Optional.empty(), java.util.Optional.empty(), java.util.Optional.empty()));
// final RequestId[] requestIds = new RequestId[1];
// Add event listener to capture network requests
devTools.addListener(Network.requestWillBeSent(), request -> {
RequestId requestId = request.getRequestId();
String requestUrl = request.getRequest().getUrl();
String requestMethod = request.getRequest().getMethod();
String requestHeaders = request.getRequest().getHeaders().toString();
// String requestBody = request.getRequest().getPostDataEntries().toString();
String requestBody = devTools.send(Network.getRequestPostData(requestId)).toString();
APP_LOGS.info("Request Url: " + requestUrl);
APP_LOGS.info("Request Method: " + requestMethod);
APP_LOGS.info("Request Headers: " + requestHeaders);
APP_LOGS.info("Request Body: " + requestBody);
});
// Add event listener to capture network responses
devTools.addListener(Network.responseReceived(), response -> {
RequestId requestId = response.getRequestId();
if (!response.getResponse().getStatus().equals(200)) {
String responseUrl = response.getResponse().getUrl();
if (responseUrl.contains("salesforce")) {
int responsStatus = response.getResponse().getStatus();
String responseType = response.getType().toJson();
String statusText = response.getResponse().getStatusText();
String responseHeaders = response.getResponse().getRequestHeaders().toString();
String responseBody = devTools.send(Network.getResponseBody(requestId)).getBody();
APP_LOGS.error("Response Url: " + responseUrl);
APP_LOGS.error("Response Status Code: " + responsStatus);
APP_LOGS.error("Response Headers: " + responseHeaders);
APP_LOGS.error("Response Type: " + responseType);
APP_LOGS.error("Response Text: " + statusText);
APP_LOGS.error("Response Body: " + responseBody);
}
}
});
} else {
APP_LOGS.info("Browser network log not captured for: " + getBrowserName());
}
}