Capture All The JavaScript Errors Using Selenium Webdriver


This post helps you to Capture all the JavaScript Errors

import java.util.List;
import net.jsourcerer.webdriver.jserrorcollector.JavaScriptError;
import org.openqa.selenium.firefox.FirefoxProfile;

@BeforeTest
public void setUp() throws Exception {
FirefoxProfile ffProfile = new FirefoxProfile();
JavaScriptError.addExtension(ffProfile);
driver = new FirefoxDriver(ffProfile);
baseUrl = “http://xyz.com”;
}

@Test
public void contentTitle() throws Exception {
driver.get(baseUrl + “/”);
Thread.sleep(5000);
}

@AfterTest
public void tearDown() throws Exception {
List jsErrors = JavaScriptError.readErrors(driver);
System.out.println(“——————START displaying JS errors——————”);
for (int i = 0; i < jsErrors.size(); i++) {
System.out.println(jsErrors.get(i).getErrorMessage());
System.out.println("Error in Line: "+ jsErrors.get(i).getLineNumber());
System.out.println(jsErrors.get(i).getSourceName());
System.out.println("\n");
}
System.out.println("——————STOP displaying JS errors———————");
driver.close();
driver.quit();
}

Advertisements

4 thoughts on “Capture All The JavaScript Errors Using Selenium Webdriver

  1. armatearjun June 15, 2015 / 2:13 pm

    is this comes with selenium server or is this an external package? import net.jsourcerer.webdriver.jserrorcollector.JavaScriptError;, if i try to add the same it says cannot be resolved…

    Like

    • charan163 June 15, 2015 / 2:50 pm

      Its Java Utils package

      Like

  2. autumnator December 8, 2016 / 2:24 am

    Unfortunately this is geared towards FF and not browser agnostic.

    Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s