How to use Selenium WebDriver to report Javascript errors
Almost all websites use some level of Javascript to perform certain operations on UI. During manual testing, when using the chrome browser, we can check for Javascript errors by checking the console of the developer tool.
To report Javascript errors using Selenium WebDriver, we need to add JSERROR Collector jar to the project by adding the following dependencies to the POM file:
Net.jsourcerer.webdriver
JSErrorCollector
0.5-atlassian-2
We also need to add a firefox plug-in to the firefox configuration file in the settings of the automated test so that we can catch Javascript errors in Selenium WebDriver. Click to download the firefox plug-in.
Here is a sample WebDriver code that uses the above library and firefox plug-in to report Javascript errors:
Public class JSErrorTest {
Public static WebDriver wbdv = null;public static EventFiringWebDriver driver=null;@BeforeClasspublic 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");}@AfterClasspublic static void tearDown () {List jsErrors = JavaScriptError.readErrors (driver); System.out.println (" # start displaying errors "); for (int I = 0; I < jsErrors.size () ) {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 ();} @ Testpublic void returnJavascriptErrors () throws InterruptedException {/ / do your steps}
}