Search

Wednesday, February 18, 2015

Capturing Page JavaScript Errors Using Selenium WebDriver

It Is very Importnt to find Javascript errors on web page before any application's release. Generally Test engineer Goes to each and every page manually and check If there Is any java script error on page. It can take very much time. We can automate this task using selenium webdriver Instead of doing It manually.

How to Find JS errors using selenium webdriver
Selenium WebDriver do not have any built In method or function using which we can collect JS errors from page. For that you need to download "JSErrorCollector-0.5.jar" file from THIS PAGE and Include It In your project's build path. You can refer "Adding jar Files In Project's Build Path" section on THIS PAGE If you don't know how to add It.

Now you can create bellow given example In your eclipse. I have used page with javascript error.

package Testing_Pack;

import java.util.List;
import java.util.concurrent.TimeUnit;
import net.jsourcerer.webdriver.jserrorcollector.JavaScriptError;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxProfile;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

public class jsError {
WebDriver driver;

@BeforeTest
public void setup() throws Exception {
FirefoxProfile profile = new FirefoxProfile();
JavaScriptError.addExtension(profile);
driver = new FirefoxDriver(profile);
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
driver.get("http://only-testing-blog.blogspot.com/2015/01/table-with-checkbox.html");
}

@Test
public void printPageErrors() throws Exception {
//Capture all errors and store them In array.
List<JavaScriptError> Errors = JavaScriptError.readErrors(driver);
System.out.println("Total No Of JavaScript Errors : " + Errors.size());
//Print Javascript Errors one by one from array.
for (int i = 0; i < Errors.size(); i++) {
System.out.println("Error Message : "
+ Errors.get(i).getErrorMessage());
System.out.println("Error Line No : "
+ Errors.get(i).getLineNumber());
System.out.println(Errors.get(i).getSourceName());
System.out.println();
}
}
}

Check the console output on test completion. It will print JSError as bellow In console.

Error Message : ReferenceError: adddlert is not defined
Error Line No : 729
http://only-testing-blog.blogspot.in/2015/01/table-with-checkbox.html

No comments:

Post a Comment