Tuesday, 28 January 2014

Capture all the JavaScript Errors | Selenium

Capture js using Firefox profile

1| Download the library file, JSErrorCollector
2| Add to build path.
3| Make use of the library in your code using Firefox profile.

Snippet

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<JavaScriptError> 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();
}





5 comments:

  1. Exposing privileged or cross-origin callable is prohibited error when doing it

    ReplyDelete
  2. This comment has been removed by the author.

    ReplyDelete
  3. Thanks for the great information in your blog Selenium Training in Chennai

    ReplyDelete
  4. It was very nice blog to learn about Selenium.Thanks for sharing new things.selenium training in chennai

    ReplyDelete