EventFiringWebDriver for capturing Screenshots on failure.

666 views
Skip to first unread message

fgeorge

unread,
Aug 9, 2011, 7:00:58 PM8/9/11
to webdriver
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.

winfred zhu

unread,
Aug 24, 2011, 4:37:17 AM8/24/11
to webd...@googlegroups.com
I also did this using C# but it doesn't work, why


public static IWebDriver driver = null;
driver = new InternetExplorerDriver();
EventFiringWebDriver webDriver = new EventFiringWebDriver(driver);
webDriver.ExceptionThrown += new EventHandler<WebDriverExceptionEventArgs>(firingDriver_ExceptionThrown);

void firingDriver_ExceptionThrown(object sender, WebDriverExceptionEventArgs e)
        {
            log.Debug(e.ThrownException.StackTrace + e.ThrownException.Message);
            ITakesScreenshot shot = (ITakesScreenshot)e.Driver;
            shot.GetScreenshot().SaveAsFile(@"D:\acb.png", ImageFormat.Png);
        }


Reply all
Reply to author
Forward
0 new messages