Intercepting HTTP Response
Fore intercepting the response we will be using Network.responseReceived event. This fired when the HTTP response is available, with this we can listen to URL, response headers, response code, etc. To get the response body use the method Network.getResponseBody
public static void validateResponse(String applicationfractionUrl) {
System.out.println("**********Getting Network Tab Response Details****************");
final RequestId[] requestIds = new RequestId[1];
devTools.send(Network.enable(Optional.of(100000000), Optional.empty(), Optional.empty()));
devTools.addListener(Network.responseReceived(), responseReceived -> {
if (responseReceived.getResponse().getUrl().contains(applicationfractionUrl)) {
System.out.println("URL: " + responseReceived.getResponse().getUrl());
System.out.println("Status: " + responseReceived.getResponse().getStatus());
System.out.println("Type: " + responseReceived.getType().toJson());
responseReceived.getResponse().getHeaders().toJson().forEach((k, v) -> System.out.println((k + ":" + v)));
requestIds[0] = responseReceived.getRequestId();
System.out.println("Response Body: \n" + devTools.send(Network.getResponseBody(requestIds[0])).getBody() + "\n");
}
});
System.out.println("--------------end of response-----------------------------------");
}
=============================================================
Capture Console Logs
When the application is opened, the console error logs published by the application are captured.
public static void consoleLogs(){
System.out.println("************Getting Console Logs****************");
devTools.send(Log.enable());
devTools.addListener(Log.entryAdded(),
logEntry -> {
System.out.println("log: "+logEntry.getText());
System.out.println("level: "+logEntry.getLevel());
});
System.out.println("--------------end of Console logs-----------------------------------");
}
After fetching logs disable and clear the logs
devTools.send(Log.clear());
devTools.send(Log.disable());
-------------------------------------------------------------------------------------------
Performance MetricsWith DevTools we can get performance metrics while running automation tests. public static void getPerformanceMetrics(){ System.out.println("************Getting Performance Metrics****************"); devTools.send(Performance.enable(Optional.empty())); List<Metric> metrics = devTools.send(Performance.getMetrics()); metrics.forEach(metric-> System.out.println(metric.getName() +" : "+ metric.getValue())); /** * for(Metric m : metricList) { System.out.println(m.getName() + " = " + m.getValue()); } */ System.out.println("--------------end of performnce Metrics-----------------------------------"); } ========================================================================================== | |
Validating Cookies
With chrome dev tools API we can play around with browser cookies. We can clear browser cookies, delete all cookies, get and set cookie or cookies.
public void getAllCookies() {
devTools.send(Network.enable(Optional.empty(), Optional.empty(), Optional.empty()));
//Getting all cookies
List<Cookie> cookies = devTools.send(Network.getAllCookies());
cookies.forEach(cookie -> System.out.println(cookie.getName()));
List<String> cookieName = cookies.stream().map(cookie -> cookie.getName()).sorted().collect(Collectors.toList());
Set<org.openqa.selenium.Cookie> seleniumCookie = driver.manage().getCookies();
List<String> selCookieName = seleniumCookie.stream().map(selCookie -> selCookie.getName()).sorted().collect(Collectors.toList());
Assert.assertEquals(cookieName, selCookieName);
//Clearing browser cookies
devTools.send(Network.clearBrowserCookies());
List<Cookie> cookiesAfterClearing = devTools.send(Network.getAllCookies());
Assert.assertTrue(cookiesAfterClearing.isEmpty());
}
=======================================================================================
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.