Checkout our demo site to practice selenium https://magento.softwaretestingboard.com/

0 like 0 dislike
629 views
by Contributing Tester (49 points)

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());
        }
    }

 

1 Answer

0 like 0 dislike
by

If you are using TestNG, you can just throw an exception.

TestNG will capture the exception and mark the test as a fail.

Something like this for example.

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());

              throw new Exception(new Date(entry.getTimestamp()) + " " + entry.getLevel() + " " + entry.getMessage());

           }
        }
    }

Make sure not to use function analyzeBrowserLogs() inside try-catch block or function that is using the function analyzeBrowserLogs inside a try-catch block.


This site is for software testing professionals, where you can ask all your questions and get answers from 1300+ masters of the profession. Click here to submit yours now!

...