1

I am using this code to detect console errors logged while executing automated UI tests with ChromeDriver:

public static List<LogEntry> getJavaScriptErrors(Level level) {
    return getDriver().manage().logs().get(LogType.BROWSER).filter(level);
}

Prior to executing a step I would like to clear the console log, so I only see errors which were caused by the particular action I am looking to test and not earlier errors during the test setup.

How can I explicitly clear errors from logs?

infojolt
  • 5,244
  • 3
  • 40
  • 82

2 Answers2

1

I created this method which clears the console in the browser (first 3 lines) and clears the log returned to Selenium (the last line).

public void clearConsoleErrors(){
    JavascriptExecutor js = (JavascriptExecutor)getDriver();
    String script = "console.clear();";
    js.executeScript(script);
    getDriver().manage().logs().get(LogType.BROWSER).filter(Level.ALL)
}
infojolt
  • 5,244
  • 3
  • 40
  • 82
0

Firstly if you want to see logs anyway in a .log file you can set property for it.

System.setProperty("webdriver.chrome.logfile", "\\path\\chromeDriver.log");

After that you can set console logging as silent with below code.

System.setProperty("webdriver.chrome.silentOutput", "true");
sayhan
  • 1,168
  • 1
  • 16
  • 22