Accessing Network properties of open URL using Selenium Java

If you want to access the Header information, Request Payload and Response using Selenium, then it’s easy with Chrome.

All you have to do is to add following capabilities in the Chrome.

DesiredCapabilities cap = DesiredCapabilities.chrome();
// logging prefs
LoggingPreferences loggingprefs = new LoggingPreferences();
loggingprefs.enable(LogType.BROWSER, Level.ALL);
loggingprefs.enable(LogType.CLIENT, Level.ALL);
loggingprefs.enable(LogType.PERFORMANCE, Level.ALL);
loggingprefs.enable(LogType.PROFILER, Level.ALL);
cap.setCapability(CapabilityType.LOGGING_PREFS, loggingprefs);

// Creating Chrome options
ChromeOptions chromeOptions = new ChromeOptions();
chromeOptions.addArguments("--disable-xss-auditor");
chromeOptions.addArguments("--ignore-certificate-errors");
chromeOptions.addArguments("--disable-infobars");
chromeOptions.addArguments("--disable-extensions");
chromeOptions.setCapability("goog:loggingPrefs", loggingprefs);
cap.setCapability(ChromeOptions.CAPABILITY, chromeOptions);
chromeOptions.merge(cap);

 System.setProperty(PROPERTY_NAME_CHROME, PATH_CHROME_EXE);
Webdriver driver =  driver = new ChromeDriver(chromeOptions);

// Add following methods to call logging

 public void logBrowserConsoleLogs(WebDriver driver) {
        all(LogType.PERFORMANCE, driver);
    }

    public void all(String logTypes, WebDriver driver) {
        String var;
        String authorizationtoken = "";
        String host = "";
        List<LogEntry> logEntries = driver.manage().logs().get(logTypes).getAll();
        for (LogEntry entry : logEntries) {
            var = new Date(entry.getTimestamp()) + " " + entry.getLevel() + " " +   entry.getMessage();
            System.out.println(var);
        }
        System.out.println("DeubiAuthorizationToken: " + authorizationtoken);
        System.out.println("DeubiAuthorizationHost" + host);
        System.out.println("=======================================================");
    }

logBrowserConsoleLogs(); will print all the logs on the console. Now you can play with the parameters you need from the Network tab, which you mostly use for manual testing to see what’s happening on the page.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s