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





8 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. Is this usable only for selenium JAVA and not for Python?

    ReplyDelete
  4. if try to use provided .xpi file as extension ,it says corrupted

    ReplyDelete
  5. It’s great to come across a blog every once in a while that isn’t the same out of date rehashed material. Fantastic read.
    Machine Learning Training in Chennai |Machine Learning Training Institute in Chennai

    ReplyDelete