Skip to content

Instantly share code, notes, and snippets.

@mehmetg
Last active December 9, 2015 21:45
Show Gist options
  • Save mehmetg/1db503ec62461137ea86 to your computer and use it in GitHub Desktop.
Save mehmetg/1db503ec62461137ea86 to your computer and use it in GitHub Desktop.
Helper class for setting selenium browser logging capabilities.
package com.yourcompany.Utils;
import org.openqa.selenium.logging.LogType;
import org.openqa.selenium.logging.LoggingPreferences;
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.ie.InternetExplorerDriverLogLevel;
import java.rmi.UnexpectedException;
import java.util.logging.Level;
/**
* Created by mehmetgerceker on 12/9/15.
*/
public class SeleniumBrowserLogHelper {
public static void setLogCapabilities(DesiredCapabilities capabilities, Level logLevel)
throws UnexpectedException{
String browser = capabilities.getBrowserName();
if (browser.contentEquals(DesiredCapabilities.firefox().getBrowserName()) ||
browser.contentEquals(DesiredCapabilities.chrome().getBrowserName())){
LoggingPreferences logPrefs = new LoggingPreferences();
logPrefs.enable(LogType.BROWSER, logLevel);
capabilities.setCapability(CapabilityType.LOGGING_PREFS, logPrefs);
} else if(browser.contentEquals(DesiredCapabilities.operaBlink().getBrowserName()) ||
browser.contentEquals(DesiredCapabilities.opera().getBrowserName())) {
//Code below is not supported yet!
//capabilities.setCapability("opera.logging.level", logLevel.getName());
System.err.printf("Console log for browser: %s is not supported!\n", browser);
} else if(browser.contentEquals(DesiredCapabilities.safari().getBrowserName())) {
System.err.printf("Console log for browser: %s is not supported!\n", browser);
} else if(browser.contentEquals(DesiredCapabilities.internetExplorer().getBrowserName())) {
System.err.printf("Console log for browser: %s is not supported!\n", browser);
//Code below not supported yet!
/*InternetExplorerDriverLogLevel ieLogLevel;
if (logLevel == Level.ALL){
ieLogLevel = InternetExplorerDriverLogLevel.TRACE;
} else if (logLevel == Level.INFO) {
ieLogLevel = InternetExplorerDriverLogLevel.INFO;
} else if (logLevel == Level.WARNING) {
ieLogLevel = InternetExplorerDriverLogLevel.WARN;
} else if (logLevel == Level.SEVERE) {
ieLogLevel = InternetExplorerDriverLogLevel.ERROR;
} else { //all else is debug
ieLogLevel = InternetExplorerDriverLogLevel.DEBUG;
}
if (logLevel != Level.OFF) {
capabilities.setCapability(InternetExplorerDriver.LOG_LEVEL, ieLogLevel);
}*/
} else if(browser.contentEquals(DesiredCapabilities.edge().getBrowserName())) {
System.err.printf("Console log for browser: %s is not supported!\n", browser);
} else {
throw new UnexpectedException("Browser not valid!");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment