Inspired by
http://darrellgrainger.blogspot.com/2011/02/generating-screen-capture-on-exception.html
I wrote the following piece of code for generating a screenshot on
webdriver exceptions.
//starting the driver
wdriver = new FirefoxDriver();
driver = new EventFiringWebDriver(wdriver);
driver.register(new AbstractWebDriverEventListener() {
public void onException(Throwable cause, WebDriver d) {
errorHandler = new ErrorHandler(driver);
errorHandler.captureScreenShot(cause.getMessage());
}
});
//and the ErrorHandler class has the following code
private String getUniqueFilename(String fileName) {
Calendar c = Calendar.getInstance();
int i = fileName.indexOf('\n');
fileName = fileName.substring(0, i).replaceAll("\\s",
"_").replaceAll(":", "") + ".png";
fileName = "" + c.get(Calendar.YEAR) + "-" + c.get(Calendar.MONTH) +
"-" + c.get(Calendar.DAY_OF_MONTH) + "-"
+ c.get(Calendar.HOUR_OF_DAY) + "-" + c.get(Calendar.MINUTE) + "-"
+ c.get(Calendar.SECOND) + "-"
+ fileName;
return fileName;
}
public void captureScreenShot(String string) {
File tmpScreenShot = ((TakesScreenshot)
this.driver).getScreenshotAs(OutputType.FILE);
try {
FileUtils.copyFile(tmpScreenShot, new File("//Users//fgeorge//
Documents//testrunner-log//",
getUniqueFilename(string)));
} catch (IOException e) {
e.printStackTrace();
}
}
1. Is there any cons in using "EventFiringWebDriver" instead of
"WebDriver" in my selenium code?
2. The screenshot gets generated( i.e. onException gets called ) if an
element is not found (driver.findElement(by) fails)
but not when a drop down value selection by selectByVisibleText(val)
fails (org.openqa.selenium.NoSuchElementException).
I am wondering whether I am taking a totally wrong way to implement
this since my objective is to capture all failures/exceptions from
WebDriver events.