TestProject Forum
Powered by leading experts in the test automation community

Selenium - capture browser logs

Is there a method to capture all browser logs while running selenium automated tests ?

Use the following classes and set up logging before running your tests:

DesiredCapabilities capabilities = DesiredCapabilities.chrome(); LoggingPreferences logPreferences = new LoggingPreferences(); logPreferences.enable(LogType.BROWSER, Level.ALL); capabilities.setCapability(CapabilityType.LOGGING_PREFS, logPreferences); driver = new ChromeDriver(capabilities);

After the test runs have completed, you can extract the logs like so:

LogEntries logEntries = driver.manage().logs().get(LogType.BROWSER);
for (LogEntry logEntry : logEntries) {
    System.out.println(logEntry.getTimestamp()) + ", " + logEntry.getLevel() + ", " + logEntry.getMessage());

thanks again Dimitri!