browser console error logs testing

Validate Console ErrorLog with Selenium WebDriver

Browser console error logs testing! While doing Automation of Enterprise application, you may come across a situation where you may need to test browser console error logs and report all the console log errors to Developer. Manually testing these are quite a time consuming, boring for Functional Tester and also require frequent testing due to the huge number of code deployment.

This test is also required when we perform cross browser compatibility issues. Most of the web applications developed with Javascript and due to syntax error causes errors on Console.

How Selenium can help you in automation testing of ‘Browser Console Error’ logs?

Selenium can really play an effective role here & you can automate this testing process very easily and in an effective way.

For this, Selenium provides logs methods to get all console logs and available log types.

Selenium log methods

This tutorial will help you in writing end to end to selenium test to verify console messages and error logs

Pre-Requisites:

  1. Selenium Project should be set up

Steps to test the Error logs occuring on Browser Console:

Sample of Console Errors:

Console errors
Sample Console Errors
  • Open Selenium Project and create one test class

Don’t Know how to setup Selenium project? Then, read out this detailed tutorial first & back here after that – Selenium Installation & Setup Tutorial with Example

  • Write code to open browser and navigate to test (the page you want to test)
WebDriverManager.chromedriver().version("79.0").setup();
WebDriver driver = new ChromeDriver();
driver.get("http://newtours.demoaut.com/");
Thread.sleep(7500);
  • Get browser log and store in LogEntries returns treatable List of log messages and their level.
LogEntries logEntries = driver.manage().logs().get("browser");
  • Now iterate LogEntries over each item using for loop (or Iterator).Now using method getLevel you can access log level and message getMessage().
for (LogEntry entry : logEntries) {
System.out.println(new Date(entry.getTimestamp()) + " " + entry.getLevel() + " " + entry.getMessage());
String errorLogType= entry.getLevel().toString();
String errorLog= entry.getMessage().toString();
if(errorLog.contains("404")){
System.out.println("Error LogType: "+ errorLogType+" Error Log message: "+errorLog); 
} 
}

Complete Code: (Selenium code for automation testing of browser console error logs)

package stepDef; import io.github.bonigarcia.wdm.WebDriverManager;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.logging.LogEntries;
import org.openqa.selenium.logging.LogEntry;
import org.openqa.selenium.logging.LogType;
import org.testng.annotations.Test;
import java.util.Date;
public class consoleLogTest {
@Test
public void consoleMessageTest() throws InterruptedException {
WebDriverManager.chromedriver().version("79.0").setup();
WebDriver driver = new ChromeDriver();
driver.get("http://newtours.demoaut.com/");
Thread.sleep(7500);
LogEntries logEntries = driver.manage().logs().get("browser");
for (LogEntry entry : logEntries) {
System.out.println(new Date(entry.getTimestamp()) + " " + entry.getLevel() + " " + entry.getMessage());
String errorLogType= entry.getLevel().toString();
String errorLog= entry.getMessage().toString();
if(errorLog.contains("404")){
System.out.println("Error LogType: "+ errorLogType+" Error Log message: "+errorLog);
}
}
driver.close();
} 
}

Console Log:

Console log

Congratulation! we have completed automation script to test browser console log. Hope you like this article and able to follow it. Still if you have any issue then feel free to write at info@thoughtcoders.com or Contact Us.

To this end, for latest updates follow us on Facebook or LinkedIn!

Comments

Arun May 27, 2020

Very informative article. Thank you guys, keep up good work

Write A Comment