How to Report Javascript Errors with Selenium WebDriver

Almost all websites use some level of Javascript to do some operations on the UI. During manual testing, when using the chrome browser, we can check for Javascript errors by inspecting the console of the developer tools.

To report Javascript errors with Selenium WebDriver, we need to add a JSERROR Collector jar to our project by adding the below dependency to the POM file:

<dependency>
    <groupId>net.jsourcerer.webdriver</groupId>
    <artifactId>JSErrorCollector</artifactId>
    <version>0.5-atlassian-2</version>
</dependency>

We also need to add a firefox addon to firefox profile in the setup of the automated tests to be able to catch Javascript errors in Selenium WebDriver. Click to Download the firefox addon.

Below is a sample WebDriver code which makes use of the above library and the firefox addon to report Javascript errors:

public class JSErrorTest {

    public static WebDriver wbdv = null;
    public static EventFiringWebDriver driver=null;

    @BeforeClass
    public static void setUp() throws IOException {
        System.setProperty("webdriver.firefox.bin","C:\\Program Files (x86)\\Mozilla Firefox\\firefox.exe");
        try {
            ProfilesIni allProfiles = new ProfilesIni();
            FirefoxProfile profile = allProfiles.getProfile("default");
            profile.setAcceptUntrustedCertificates(true);
            profile.setAssumeUntrustedCertificateIssuer(false);
            wbdv = new FirefoxDriver(profile);
            driver = new EventFiringWebDriver(wbdv);
        } catch(Throwable t) {
            System.out.println(t);
        }

        driver.get("http://www.example.com");
    }

    @AfterClass
    public static void tearDown() {
        List<JavaScriptError> jsErrors = JavaScriptError.readErrors(driver);
        System.out.println("###start displaying errors");
        for(int i = 0; i < jsErrors.size(); i++)
        {
            System.out.println(jsErrors.get(i).getErrorMessage());
            System.out.println(jsErrors.get(i).getLineNumber());
            System.out.println(jsErrors.get(i).getSourceName());
        }
        System.out.println("###start displaying errors");
        driver.close(); driver.quit();
    }

    @Test
    public void returnJavascriptErrors() throws InterruptedException {
    //do your steps
    }
}

 

Leave a Reply