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;

 @BeforeTest
 public void setup() throws Exception {
  FirefoxProfile profile = new FirefoxProfile();
  JavaScriptError.addExtension(profile);
  driver = new FirefoxDriver(profile);
  driver.manage().window().maximize();
  driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
  driver.get("http://only-testing-blog.blogspot.com/2015/01/table-with-checkbox.html");
 }

 @Test
 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());
   System.out.println(Errors.get(i).getSourceName());
   System.out.println();
  }
 }
}

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
http://only-testing-blog.blogspot.com/2015/01/table-with-checkbox.html

12 comments:

  1. Hi Aravind,

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

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

      Delete
  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: '10.189.73.140', os.name: '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}]

    ReplyDelete
  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

    ReplyDelete
  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

    ReplyDelete
  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 : https://github.com/tporeba/JSErrorCollector/tree/c9884eb4c93d72505b66d9211dfba001644aea73

    ReplyDelete
  6. Hi Aravind,

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

    ReplyDelete
  7. Hi Aravind,

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

    ReplyDelete
  8. Hii all
    i have execute all program . But console showing the error Main() function can not define . how can i solve this error. plz. find the solution.

    Thank YOu

    ReplyDelete
    Replies
    1. Can you share your code. Are you using TestNG ? TestNG does not require main method

      Delete
  9. Hi All!
    Anybody know how to solve it ?
    org.openqa.selenium.JavascriptException: Error: Permission denied to access property "pump"

    ReplyDelete
  10. Hi All!
    Anybody know how to solve it ?
    org.openqa.selenium.JavascriptException: Error: Permission denied to access property "pump"

    ReplyDelete