WebDriver Event Listener for effective Logging and Reporting

We discussed a lot more things about Selenium WebDriver. Today we are going to discuss Selenium WebDriver Event Listener. A framework without the implementation of the listener is incomplete because listeners help in many ways for advancement in the Automation framework.

Before we jump to the topic on WebDriver Event Listener, I would like to recommend you some of the awesome articles posted on inviul.

What is WebDriver Event Listener?

WebDriver Event Listener is fundamentally an interface, which is implemented by the class to override the methods written inside it. In general, listener’s work is to listen to the events and behave according to the definition given by the defined methods.

Listeners are mainly used to create effective logging and reporting in Selenium. They basically modify the default behavior of the driver. We can implement WebDriver Event listener along with log4j logging.

Webdriver event listener image

How to set up WebDriver Event Listener?

You need to implement the WebDriverEventListener interface to your class. Once implemented you will get some methods defined within it without the statement written inside the method. See below the default implementation.

Once you have implemented the WebDriver Event Listener then go to that class or TestNG file where you have instantiated WebDriver. In this class, create an instance of EventFiringWebDriver and pass the instance of WebDriver as an argument in it.

If the above steps are done, then create the instance of that class where you have implemented the WebDriver event listener. Now you have to register the instance of the implemented class with the EventFiringWebDriver.

Let’s look at the detailed steps below.

Steps to implement WebDriver Event Listener

Step# 1: Implement WebDriverEventListener

At first, you need to create a class (let’s say, EventActivityCapturer.java). Now you must implement this class with WebDriver Event Listener.

I have edited some of the methods which I am going to use in my project. So, you can do the same as per your convenience.

Step# 2: Register with EventFiringWebDriver class

Next, create the instance of WebDriver and pass it to the EventFiringWebDriver.

Now we need to register the implemented class with EventFiringWebDriver.

Here is the full program.

Below is the console output.

WebDriver Event Listener Console output

Hope you enjoyed the article. Please comment for any queries. Don’t miss our latest announcement, join our Facebook group.

Join Inviul fb group

Leave a Reply