EventFiringWebDriver & Listener
In this blog, I‘ll introduce how we use EventFiringWebDriver and listener to create an auto-logging system for the test framework. Why use EventFiringWebDriver and the event listener?
If we use it, we can easily know what a web driver instance will do with a small listener, and what operation it does when an event fires.
- We can use log4j to print log when the web driver performs an operation. That means when the test cases are executing, the logs will get printed automatically. What we need to do is to add only a few lines of code in the listener.
- We can add some functions to capture a screenshot in the listener, and the screenshot can be captured automatically when an event is firing. For example, it can capture the screen when you get an exception or capture the screen when selenium navigates to a new page.
- We can add some explicit waiting methods to slow down the test execution.
How to use EventFiringWebDriver and the event listener:
- We can create an EventFiringWebDriver as following, and it also registers the listener:
WebDriver driver = new EventFiringWebDriver(new FirefoxDriver()).register(new LogEventListener());
- We need to create the LogEventListener class which we used in the first step.
public class LogEventListener implements WebDriverEventListener {
private Log log = LogFactory.getLog(this.getClass());
private By lastFindBy;
private String originalValue;
public void beforeNavigateTo(String url, WebDriver selenium){
log.info(“WebDriver navigating to:'”+url+”‘”);
}
public void beforeChangeValueOf(WebElement element, WebDriver selenium){
originalValue = element.getAttribute(“value”);
}
public void afterChangeValueOf(WebElement element, WebDriver selenium){
log.info(“WebDriver changing value in element found “+lastFindBy+” from ‘”+originalValue+”‘ to ‘”+element.getAttribute(“value”)+”‘”);
}
public void beforeFindBy(By by, WebElement element, WebDriver selenium){
lastFindBy = by;
}
public void onException(Throwable error, WebDriver selenium){
if (error.getClass().equals(NoSuchElementException.class)){
log.error(“WebDriver error: Element not found “+lastFindBy);
} else {
log.error(“WebDriver error:”, error);
}
}
public void beforeNavigateBack(WebDriver selenium){}
public void beforeNavigateForward(WebDriver selenium){}
public void beforeClickOn(WebElement element, WebDriver selenium){}
public void beforeScript(String script, WebDriver selenium){}
public void afterClickOn(WebElement element, WebDriver selenium){
public void afterFindBy(By by, WebElement element, WebDriver selenium){}
public void afterNavigateBack(WebDriver selenium){}
public void afterNavigateForward(WebDriver selenium){}
public void afterNavigateTo(String url, WebDriver selenium){}
public void afterScript(String script, WebDriver selenium){}
}
- Here the “log.info” is from log4j, if you would like to use this code, you need to put log4j in your project and set the lo4j properties.
The full code of my small test framework build with selenium is here: https://github.com/zhangting85/simpleWebtest