How to capture the browser console if a test fails due to UI error?

In the automation framework using Selenium+TestNG+Java. I want to check browser logs, if there are any SEVERE logs while executing any tests, it should mark the test as FAIL.

public void analyzeBrowserLogs() {
        String type = "browser";
        List<LogEntry> entries = driver.manage().logs().get(type).getAll();
        System.out.println(entries.size() + " " + type + " log entries found");
        for (LogEntry entry : entries) {
            //if(entry.getMessage().contains("SEVERE"))
                System.out.println(new Date(entry.getTimestamp()) + " " + entry.getLevel() + " " + entry.getMessage());
        }
    }
2 Likes