Write Selenium Code To Test Browser Console Error Logs

While working on an enterprise-level application for automation testing, 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
  • Step 1: 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

  • Step 2: 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);
  • Step 3: Get browser log and store in LogEntries returns treatable List of log messages and their level.
LogEntries logEntries = driver.manage().logs().get("browser");
  • Step 4: 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

So you completed code for Console log testing. Please contact us on query@thoughtcoders.com if you need more information.

Subscribe to our Newsletter:

0 0 vote
Article Rating
close

Subscribe to our Newsletter:

Subscribe
Notify of
guest
1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Arun
Arun
11 months ago

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

1
0
Would love your thoughts, please comment.x
()
x
Open chat
Feel free to contact