Capture network calls and performance stats





Capturing Network calls:

To capture the network calls while loading a page, we need to use java script executor, ]the following example may helps:

    ChromeOptions options = new ChromeOptions();
    options.addArguments("disable-info");
    options.addArguments("start-maximized");
    DesiredCapabilities capabilities = DesiredCapabilities.chrome();
    capabilities.setCapability(ChromeOptions.CAPABILITY, options);
    driver = new ChromeDriver(capabilities);
    driver.get(url);
    //switch to Browser  Functions tab for code implementation
    String netData = ((JavascriptExecutor) driver).executeScript(scriptToExecute).toString();
    BrowserFunctions.waitUntilPageLoads(driver);
    String scriptToExecute = "var performance = window.performance || window.mozPerformance || window.msPerformance || window.webkitPerformance || {}; var network = performance.getEntries() || {};
return network;";
    System.out.println(netData);

Note: The out put in json format.



Capture Network performance:

DesiredCapabilities d = DesiredCapabilities.chrome();
LoggingPreferences logPrefs = new LoggingPreferences();
logPrefs.enable(LogType.PERFORMANCE, Level.ALL);
d.setCapability(CapabilityType.LOGGING_PREFS, logPrefs);
WebDriver driver = new ChromeDriver(d);
driver.get("https://www.google.co.in/");
driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
LogEntries les = driver.manage().logs().get(LogType.PERFORMANCE);
for (LogEntry le : les) {
    System.out.println(le.getMessage());
}