Delivered by FeedBurner

Selenium : How To Capture JS Errors In Google Chrome

Today most of the applications are using Java Scripts for client side processing like client side validations, etc. So It Is very Important to that If any error related to Javascript Is not present any page of application which can disturb application functionality or user experience In any browser.
Doing this task will taking lots of efforts as you needs to go on every page one by one and check If there Is any JS error. So we need some automated solution which can help us to ease our task.

If you remember, Earlier we learnt how to capture page JavaScript error using JSErrorCollector In Firefox browser. Now let's try to do same task but little different way and using google chrome browser.

We will use logger Interface to collect error from page as described In bellow given example. It will collect Java Script Errors from web page and print errors In console. Execute bellow given example In eclipse and check result.

package Testing_Pack;

import java.util.logging.Level;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.logging.LogEntries;
import org.openqa.selenium.logging.LogEntry;
import org.openqa.selenium.logging.LogType;
import org.openqa.selenium.logging.LoggingPreferences;
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

public class LogErrorInChrome {

 WebDriver driver;

 public void setUp() {
  System.setProperty("", "D:\\chromedriver_win32\\chromedriver.exe");
  DesiredCapabilities cap =;

  // Set logging preference In Google Chrome browser capability to log
  // browser errors.
  LoggingPreferences pref = new LoggingPreferences();
  pref.enable(LogType.BROWSER, Level.ALL);
  cap.setCapability(CapabilityType.LOGGING_PREFS, pref);
  driver = new ChromeDriver(cap);

 // Function to capture JSError log.
 public void GetJSErrosLog() {
  // Capture all JSerrors and print In console.
  LogEntries jserrors = driver.manage().logs().get(LogType.BROWSER);
  for (LogEntry error : jserrors) {

 public void testMethod() {
  // Call GetJSErrosLog() to log and print JSErrors In console.

Console result of above example will looks like bellow.


No comments:

Post a Comment