Delivered by FeedBurner

Capturing Page JavaScript Errors Using Selenium WebDriver

It Is very Importnt to find Javascript errors on web page before any application's release. Generally Test engineer Goes to each and every page manually and check If there Is any java script error on page. It can take very much time. We can automate this task using selenium webdriver Instead of doing It manually.

How to Find JS errors using selenium webdriver
Selenium WebDriver do not have any built In method or function using which we can collect JS errors from page. For that you need to download "JSErrorCollector-0.5.jar" file from THIS PAGE and Include It In your project's build path. You can refer "Adding jar Files In Project's Build Path" section on THIS PAGE If you don't know how to add It.

Now you can create bellow given example In your eclipse. I have used page with javascript error.

package Testing_Pack;

import java.util.List;
import java.util.concurrent.TimeUnit;
import net.jsourcerer.webdriver.jserrorcollector.JavaScriptError;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxProfile;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

public class jsError {
 WebDriver driver;

 public void setup() throws Exception {
  FirefoxProfile profile = new FirefoxProfile();
  driver = new FirefoxDriver(profile);
  driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);

 public void printPageErrors() throws Exception {
  //Capture all errors and store them In array.
  List<JavaScriptError> Errors = JavaScriptError.readErrors(driver);
  System.out.println("Total No Of JavaScript Errors : " + Errors.size());
  //Print Javascript Errors one by one from array.
  for (int i = 0; i < Errors.size(); i++) {
   System.out.println("Error Message : "
     + Errors.get(i).getErrorMessage());
   System.out.println("Error Line No : "
     + Errors.get(i).getLineNumber());

Check the console output on test completion. It will print JSError as bellow In console.

Error Message : ReferenceError: adddlert is not defined
Error Line No : 729


  1. Hi Aravind,

    By providing base URL whether it will give all pages JSerrors?

    1. No, It will give you only selected open page URL. You need to write logic to open all pages seperatly from above example

  2. Can you explain what JS errors are?
    I manually view the page and doesn't see any errors appear.

    I also encountered error during run:
    org.openqa.selenium.WebDriverException: Exposing privileged or cross-origin callable is prohibited
    Command duration or timeout: 50 milliseconds
    Build info: version: '2.47.1', revision: '411b314', time: '2015-07-30 02:56:46'
    System info: host: 'MKT-101-092-08', ip: '', 'Windows 7', os.arch: 'x86', os.version: '6.1', java.version: '1.7.0_79'
    Session ID: 442b6095-1d3d-48c1-999b-fcf58e4d61fc
    Driver info: org.openqa.selenium.firefox.FirefoxDriver
    Capabilities [{platform=WINDOWS, acceptSslCerts=true, javascriptEnabled=true, cssSelectorsEnabled=true, databaseEnabled=true, browserName=firefox, handlesAlerts=true, nativeEvents=false, webStorageEnabled=true, rotatable=false, locationContextEnabled=true, applicationCacheEnabled=true, takesScreenshot=true, version=40.0.3}]

  3. i got an error "org.openqa.selenium.WebDriverException: Exposing privileged or cross-origin callable is prohibited
    Command duration or timeout: 36 milliseconds"
    pls give a solution

  4. error "org.openqa.selenium.WebDriverException: Exposing privileged or cross-origin callable is prohibited => I tried with JSErrorCollector-0.6.jar and this error is fixed

  5. Dear @Mark, @Minh and @Anoop

    Just replace your JSErrorCollector.JAR file with Latest one and compile and run the test case.

    it will work.

    DownLoad Link :

  6. Hi Aravind,

    How can i write a logic to open all the pages seperatly from above example.

  7. Hi Aravind,

    How can i write a logic to open all pages seperatly from above example.