• Skip to main content
  • Skip to primary sidebar
  • Skip to footer
  • Home
  • Programming Languages
    • Java Tutorials
    • Python Tutorials
    • JavaScript Tutorials
  • Automation Tools and Different Tools
    • Web Automation
      • Selenium with Java
        • Selenium Basic
        • Selenium Advance
        • Selenium Realtime
        • Framework
        • Selenium Interview
        • Selenium Videos
        • Selenium with Docker
      • Selenium with Python
      • WebdriverIO
        • Selenium Webdriver C# Tutorial
      • Cypress
      • Playwright
    • TestNG
    • Cucumber
    • Mobile Automation
      • Appium
    • API Testing
      • Postman
      • Rest Assured
      • SOAPUI
    • testRigor
    • Katalon
    • TestProject
    • Serenity BDD
    • Gradle- Build Tool
    • RPA-UiPath
    • Protractor
    • Windows Automation
  • Automation For Manual Testers
  • Services
  • Online Training
  • Contact us
  • About me
  • Follow us
    • Linkedin
    • Facebook Group
    • Facebook Page
    • Instagram

Automation

Selenium WebDriver tutorial Step by Step

You are here: Home / Advance Selenium / What is Listeners and EventFiringWebDriver in Selenium Webdriver

What is Listeners and EventFiringWebDriver in Selenium Webdriver

March 15, 2015 by Mukesh Otwani 42 Comments

what is listeners in selenium webdriver

What is Webdriver Listeners-

Hello Welcome to Selenium tutorial in this post we will talk about WebDriver Listener,EventFiringWebDriver and WebDriverEventListener  in detail.

I know all of you might have heard of Listeners but what exactly Listeners is let us discuss today.

In general, terms, Listeners are whom that listen to you and my favorite quotes is “Be a better listener”.

what is listeners in selenium webdriver
what is listeners in selenium webdriver

If you talk about Webdriver Listener so you should make a note of some classes and interfaces that we will use so will talk about it.

1- WebDriverEventListener – This is an interface, which have some predefined methods so we will implement all of these methods.

2-EventFiringWebDriver- This is an class that actually fire Webdriver event.

Why we are using Webdriver Listeners

If you talk about Webdriver we are doing some activity like type, click, navigate etc this is all your events which you are performing on your script so we should have activity which actually will keep track of it.

Take an example if you perform click then what should happen before click and after click.

To capture these events we will add listener that will perform this task for us.

How to implement Listener in our Script

Program for what is listeners in selenium webdriver

Step 1- Create a new Class that will implement WebDriverEventListener methods

package listerDemo;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.events.WebDriverEventListener;

public class ActivityCapture implements WebDriverEventListener {

@Override
public void afterChangeValueOf(WebElement arg0, WebDriver arg1) {

}

@Override
public void afterClickOn(WebElement arg0, WebDriver arg1) {

System.out.println("After click "+arg0.toString());

}

@Override
public void afterFindBy(By arg0, WebElement arg1, WebDriver arg2) {

System.out.println("After FindBy "+arg0.toString());
}

@Override
public void afterNavigateBack(WebDriver arg0) {

System.out.println("After navigating back "+arg0.toString());

}

@Override
public void afterNavigateForward(WebDriver arg0) {

System.out.println("After navigating forword "+arg0.toString());

}

@Override
public void afterNavigateTo(String arg0, WebDriver arg1) {

System.out.println("After navigating "+arg0.toString());

System.out.println("After navigating "+arg1.toString());

}

@Override
public void afterScript(String arg0, WebDriver arg1) {

}

@Override
public void beforeChangeValueOf(WebElement arg0, WebDriver arg1) {

}

@Override
public void beforeClickOn(WebElement arg0, WebDriver arg1) {

System.out.println("before click "+arg0.toString());

}

@Override
public void beforeFindBy(By arg0, WebElement arg1, WebDriver arg2) {

System.out.println("before FindBY "+arg0.toString());

}

@Override
public void beforeNavigateBack(WebDriver arg0) {

System.out.println("Before navigating back "+arg0.toString());
}

@Override
public void beforeNavigateForward(WebDriver arg0) {
System.out.println("Before navigating Forword "+arg0.toString());

}

@Override
public void beforeNavigateTo(String arg0, WebDriver arg1) {

System.out.println("Before navigating "+arg0.toString());
System.out.println("Before navigating "+arg1.toString());

}

@Override
public void beforeScript(String arg0, WebDriver arg1) {

}

@Override
public void onException(Throwable arg0, WebDriver arg1) {

System.out.println("Testcase done"+arg0.toString());
System.out.println("Testcase done"+arg1.toString());
}

}

 

Let’s Discuss one of these methods

@Override
public void afterClickOn(WebElement arg0, WebDriver arg1) {

System.out.println(“After click “+arg0.toString());

}

In above method we are simply printing on console and this method will automatically called once click events done. In same way you have to implement on methods.

Note- We generally use Listener to generate log events

Step 2- Now create your simple script, create EventFiringWebDriver object, and pass your driver object.

EventFiringWebDriver event1=new EventFiringWebDriver(driver);

Step 3- Create an object of the class who has implemented all the method of WebDriverEventListener so in our case ActivityCapture is a class who has implemented the same.

ActivityCapture handle=new ActivityCapture();

Step 4- Now register that event using register method and pass the object of ActivityCapture class

event1.register(handle);

We are done now using event1 object write your script so now let us implement the same

Implementation of Webdriver listener

 

package testcases;

import listerDemo.ActivityCapture;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.events.EventFiringWebDriver;

public class ListnerDemo {

public static void main(String []args){

System.out.println("Started");

WebDriver driver=new FirefoxDriver();

EventFiringWebDriver event1=new EventFiringWebDriver(driver);

ActivityCapture handle=new ActivityCapture();

event1.register(handle);

event1.navigate().to("http://www.facebook.com");

event1.findElement(By.id("email")).sendKeys("asdsadsa");

event1.findElement(By.id("loginbutton")).click();

event1.quit();

event1.unregister(handle);

System.out.println("End");
}

}

 

Output-

what is listeners in selenium webdriver
what is listeners in selenium webdriver

Please comment below if you have any issue in Selenium. Thanks for visiting my blog keep in touch.

Bye.

 

Filed Under: Advance Selenium Tagged With: Webdriver Listener

Reader Interactions

Comments

  1. Navneet Singh says

    November 19, 2019 at 5:22 PM

    Hi Mukesh,

    It would be very helpful if you post a video as well on this topic. I think there is no video uploaded on this topic. If it’s there could you please provide the link.

    Thanks

    Reply
    • Mukesh Otwani says

      November 19, 2019 at 10:42 PM

      Hi Navneet,

      Thanks for your reminder…:)
      I’ll post it soon

      Reply
  2. soni says

    June 19, 2019 at 2:47 PM

    Hi Mukesh,
    Your videos are helping us so much….

    Reply
    • Mukesh Otwani says

      June 19, 2019 at 4:31 PM

      You’re welcome…:)

      Reply
  3. viki says

    May 25, 2019 at 8:26 PM

    do you have c# code of the same ?

    Reply
    • Mukesh Otwani says

      May 26, 2019 at 2:05 AM

      Hi Viki,

      May be this link will help you…

      Reply
  4. Gaurav Khurana says

    January 15, 2017 at 4:17 PM

    Firstly i felt lazy to type the first program but finally i started writing it.
    Best part was as soon as you type implements WebDriverEventListner, eclipse give you the option to implement all the unimplemented methods of the interface

    I clicked on it and it has written all the fucntions fo that class.
    Thanks for such facilities and making the logic so simple.

    Coudl you share some shortcoming of these classes WebDriverEventListener and EventFiringWebdriver , So that we can think of why they should not be used in certain situation..

    Till now as per my observation

    1) reporter.log is simplest to implement
    2) This one. WebDriverEventListener
    3) Log4j the most complicated

    Reply
    • Mukesh Otwani says

      January 24, 2017 at 4:11 PM

      Hi Gaurav,

      They mainly used for reporting and some special event as well. Let’s say after every click I want to generate log in that case we use this.

      Reply
  5. Nikhil says

    January 9, 2017 at 12:48 PM

    Mukesh where eaxctly are listeners used?

    Reply
    • Lajis says

      January 9, 2017 at 4:56 PM

      Hi Nikhil,

      It is mainly used for reports and logs.

      Reply
  6. hemant says

    December 27, 2016 at 12:52 AM

    Thanks Mukesh youe site is really helpful..

    Reply
    • Mukesh Otwani says

      December 28, 2016 at 11:38 AM

      Thanks Hemant

      Reply
  7. Vishwaa says

    December 15, 2016 at 2:34 PM

    Hi Mukesh,

    Will you please let me know how to implement Reflection in selenium WebDriver.

    Reply
    • Mukesh Otwani says

      December 15, 2016 at 5:19 PM

      Hi Vishwaa,

      I will post article on this soon.

      Reply
  8. Lokesh Sharma says

    November 30, 2016 at 9:32 AM

    Hi Mukesh, I found your videos and article very helpful.

    Reply
    • Mukesh Otwani says

      November 30, 2016 at 5:08 PM

      Thanks Lokesh. Keep in touch.

      Reply
  9. Priyanka says

    November 2, 2016 at 12:07 PM

    Hi Mukesh….Its a wonderful article. I m getting a lil confused. Can you plz explain me step by step i.e how are the two codes are interconnected . event1.navigate().to(“http://www.facebook.com”)

    Its opening facebook page but how exactly it is workingm not geeting. Pls help

    Reply
    • Mukesh Otwani says

      November 5, 2016 at 10:46 PM

      Hi Priyanka,

      In this case we are attaching listerner with our code.

      Reply
    • Mithilesh Singh says

      January 15, 2020 at 11:23 PM

      Hi Priyanka,

      This communication is possible just because of register() method which we are calling using the object of EventFiringWebDriver class and passing object of Listener class.
      Since We have already overridden those methods which we are using in current class so as soon as we call that method it shows implemented log message.

      Reply
  10. Priyaranjan says

    September 24, 2016 at 8:01 PM

    Every time I get an issue, I visit your site or mail you. That’s really great of you to answer them all promptly. Your videos really works in understanding. Thanks a lot 🙂

    Reply
    • Mukesh Otwani says

      September 29, 2016 at 11:13 AM

      Thanks mate 🙂 Keep visiting.

      Reply
  11. Aruna says

    July 19, 2016 at 6:35 AM

    Hi Sir,
    Your vedios and tutorials are awesome. Everything is in detailed. Its very very useful for me….great job
    Thankyou.

    Reply
    • Mukesh Otwani says

      July 20, 2016 at 1:55 PM

      Hey Aruna,

      Welcome and thanks for nice feedback 🙂 Let me know if any help from my side.

      Reply
    • yes aruna says

      August 10, 2016 at 8:50 AM

      Nice comment with nice picture

      Reply
      • Mukesh Otwani says

        August 18, 2016 at 10:02 AM

        Thanks Aruna 🙂

        Reply
  12. Nawaz says

    June 24, 2016 at 4:29 PM

    Could you plz add a video on the same WebDriver Listners.

    Thanks in Advance.

    Reply
    • Mukesh Otwani says

      July 2, 2016 at 3:15 PM

      Yes Nawaz will upload soon.

      Reply
  13. Mahesh says

    June 12, 2016 at 11:50 PM

    How webdriver listeners are different from TestNG listeners and when to use what and which is best ?

    Reply
    • Mukesh Otwani says

      June 14, 2016 at 9:14 PM

      Hey Mahesh,

      TestNG listener for test related events.

      WebDriver listener for driver events and we should use both for maximum output.

      Reply
  14. yogi says

    June 6, 2016 at 3:13 PM

    Hi Mukesh Otwani,

    good article, it helps a lot, Please upload how to handle javascript alert dynamically and automatically for entire web application using listeners in selenium webdriver.

    Reply
    • Mukesh Otwani says

      June 9, 2016 at 2:02 AM

      Hey Yogi yes will try to upload soon.

      Reply
  15. sujay kumar says

    May 21, 2016 at 1:38 PM

    Hi Mukesh,

    Very good explanation. Your articles help us to learn advanced topics in easy way. Keep your good work.Many thanks.

    Reply
    • Mukesh Otwani says

      May 24, 2016 at 1:37 AM

      Hi Sujay,

      Welcome mate 🙂 keep visiting.

      Reply
  16. Reshma Sultana says

    May 20, 2016 at 7:51 PM

    Hi Mukesh,

    Could you please upload a video tutorial of Webdriver Listener as you have done for TestNG Listener? Then it would be easy for me to understand properly. Thank you.

    Reply
    • Mukesh Otwani says

      May 21, 2016 at 1:15 AM

      Hi Reshma,

      Yes sure will try to upload video on the same.

      Reply
  17. saikiran says

    February 4, 2016 at 10:53 PM

    Hi Mukesh,

    I am getting error at line(20) ActivityCapture handle=new ActivityCapture();

    when i mouse hover it doesn’t show any options. Is there any jar we need to download and add that to our project.

    Reply
    • Mukesh Otwani says

      March 3, 2016 at 5:01 PM

      Hi Sai,

      ActivityCapture is the class that we have created which implements the listener.

      Reply
    • Gaurav Khurana says

      January 15, 2017 at 3:28 PM

      @Saikiran you need to create 2 programs as mentioned above. 1st program has the ActivityCapture class whose object we are creating in the second program. SO check if you miss to create the first program or forgot the import statement in the second class

      Reply
  18. Ram Narayan says

    December 2, 2015 at 3:56 PM

    Thanks for detailed explanation … Mean it ….

    Reply
    • Mukesh Otwani says

      December 2, 2015 at 5:37 PM

      Thanks Ram

      Reply
  19. Ramesh says

    November 9, 2015 at 9:40 PM

    Thank you so much. Clearly mentioned each and every thing about listeners and it implementation in Framework.:)_

    Reply
    • Mukesh Otwani says

      November 9, 2015 at 9:45 PM

      Hi Ramesh,

      Thank you. Glad to know it helped 🙂 Keep visiting.

      Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

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

Primary Sidebar

Free Selenium Videos

https://www.youtube.com/watch?v=w_iPCT1ETO4

Search topic

Top Posts & Pages

  • Selenium Webdriver tutorial for beginners
  • How To Fix Eclipse Autocomplete Or Code Suggestion In Eclipse
  • Selenium Webdriver C# Tutorial
  • WHAT ARE YOUR EXPECTATIONS FROM US?

Stay connected via Facebook

Stay connected via Facebook

Archives

Footer

Categories

Recent Post

  • API Testing Using Postman And RestAssured
  • Disable Personalise Your Web Experience Microsoft Edge Prompt In Selenium
  • How To Fix Error: No tests found In Playwright
  • How To Fix Eclipse Autocomplete Or Code Suggestion In Eclipse
  • Best and easy way to Group test cases in selenium

Top Posts & Pages

  • Selenium Webdriver tutorial for beginners
  • How To Fix Eclipse Autocomplete Or Code Suggestion In Eclipse
  • Selenium Webdriver C# Tutorial
  • WHAT ARE YOUR EXPECTATIONS FROM US?