Using Selenium with Java and TestNG (3)

Using Selenium with Java and TestNG (3)

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.

  1. Using Selenium with Java and TestNG (3)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.
  1. 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.
  1. We can add some explicit waiting methods to slow down the test execution.

How to use EventFiringWebDriver and the event listener:

  1. We can create an EventFiringWebDriver as following, and it also registers the listener:

WebDriver driver = new EventFiringWebDriver(new FirefoxDriver()).register(new LogEventListener());

  1. 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){“WebDriver navigating to:'”+url+”‘”);


public void beforeChangeValueOf(WebElement element, WebDriver selenium){

originalValue = element.getAttribute(“value”);


public void afterChangeValueOf(WebElement element, WebDriver selenium){“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){}


  1. Here the “” 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:


Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Colin Zhang

More from this Author

Subscribe to the Weekly Blog Digest:

Sign Up
Follow Us