Skip to content

Instantly share code, notes, and snippets.

@mehmetg
Created February 4, 2016 22:18
Show Gist options
  • Save mehmetg/32630591fdfe3b48cf10 to your computer and use it in GitHub Desktop.
Save mehmetg/32630591fdfe3b48cf10 to your computer and use it in GitHub Desktop.
/* The code below is a snippet. It won't run or compile unless integrated in the appropriate class
** This is only supported on recent versions Firefox and Chrome when used with recent versions of
** Firefox and Chrome driver.
*/
import org.openqa.selenium.logging.LogEntries;
import org.openqa.selenium.logging.LogEntry;
import org.openqa.selenium.logging.LogType;
import org.openqa.selenium.logging.LoggingPreferences;
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.remote.DesiredCapabilities;
//Recommended while initializing the driver.
public void updateLoggingCaps(DesiredCapabilities desiredCaps) {
LoggingPreferences logPrefs = new LoggingPreferences();
logPrefs.enable(LogType.BROWSER, Level.ALL);
desiredCaps.setCapability(CapabilityType.LOGGING_PREFS, logPrefs);
}
//I'd recommend doing this on test failures only.
public void ExtractJSLogs(Webdriver driver) {
LogEntries logEntries = driver.manage().logs().get(LogType.BROWSER);
for (LogEntry entry : logEntries) {
System.out.println(new Date(entry.getTimestamp()) + " " + entry.getLevel() + " " + entry.getMessage());
//you can write it to a file
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment