How to implement Event Listener Interface in Selenium WebDriver using java.

Webdriver Listeners: Event listener in WebDriver helps to track events those take place in WebDriver during script execution. It allows do something before or after an action. For example, if you want to wait for an ajax element before every click, or log that action into logger, it is possible to do with the event listener.
WebDriverEventListener: is a interface which have near about 15 methods
EventFiringWebDriver: This is an class that actually fire Webdriver event

Create a class called EventListerHandler to override events just by Implementing WebDriverEventListener interface. We can override to see after and before tracking of each events that we trigger through our scripts.
 import org.openqa.selenium.By;  
 import org.openqa.selenium.WebDriver;  
 import org.openqa.selenium.WebElement;  
 import org.openqa.selenium.support.events.WebDriverEventListener;  
 public class EventListerHandler implements WebDriverEventListener {  
 //Called after WebElement.clear(), WebElement.sendKeys(...).  
 @Override  
 public void afterChangeValueOf(WebElement element, WebDriver driver) {  
 // TODO Auto-generated method stub  
 }  
 //Called after WebElement.click()  
 @Override  
 public void afterClickOn(WebElement element, WebDriver driver) {  
 // TODO Auto-generated method stub  
 }  
 //Called after WebDriver.findElement(...), or WebDriver.findElements(...), or WebElement.findElement(...), or WebElement.findElements(...).  
 @Override  
 public void afterFindBy(By by, WebElement element, WebDriver driver) {  
 // TODO Auto-generated method stub  
 }  
 //Called after navigate().back().  
 @Override  
 public void afterNavigateBack(WebDriver driver) {  
 // TODO Auto-generated method stub  
 }  
 //Called after navigate().forward().  
 @Override  
 public void afterNavigateForward(WebDriver driver) {  
 // TODO Auto-generated method stub  
 }  
 //Called after get(String url) respectively navigate().to(String url).  
 @Override  
 public void afterNavigateTo(String url, WebDriver driver) {  
 // TODO Auto-generated method stub  
 }  
 //Called after RemoteWebDriver.executeScript(String, Object[]).  
 @Override  
 public void afterScript(String script, WebDriver driver) {  
 // TODO Auto-generated method stub  
 }  
 //Called before WebElement.clear(), WebElement.sendKeys(...)  
 @Override  
 public void beforeChangeValueOf(WebElement element, WebDriver driver) {  
 // TODO Auto-generated method stub  
 }  
 //Called before WebElement.click().    
 @Override  
 public void beforeClickOn(WebElement element, WebDriver driver) {  
 // TODO Auto-generated method stub  
 }  
 //Called before WebDriver.findElement(...), or WebDriver.findElements(...), or WebElement.findElement(...), or WebElement.findElements(...).  
 @Override  
 public void beforeFindBy(By by, WebElement element, WebDriver driver) {  
 }  
 //Called before navigate().back().  
 @Override  
 public void beforeNavigateBack(WebDriver driver) {  
 // TODO Auto-generated method stub  
 }  
 //Called before navigate().forward().  
 @Override  
 public void beforeNavigateForward(WebDriver driver) {  
 // TODO Auto-generated method stub  
 }  
 //Called before get(String url) respectively navigate().to(String url).  
 @Override  
 public void beforeNavigateTo(String url, WebDriver driver) {  
 // TODO Auto-generated method stub  
 }  
 //Called before RemoteWebDriver.executeScript(String, Object[])  
 @Override  
 public void beforeScript(String script, WebDriver driver) {  
 // TODO Auto-generated method stub  
 }  
 // Called whenever an exception would be thrown.  
 @Override  
 public void onException(Throwable throwable, WebDriver driver) {  
 // TODO Auto-generated method stub  
 }  
 }  



Create a new Class that implement WebDriverEventListener methods
 import org.openqa.selenium.By;  
 import org.openqa.selenium.WebDriver;  
 import org.openqa.selenium.WebElement;  
 import org.openqa.selenium.support.events.WebDriverEventListener;  
 public class EventListerHandler implements WebDriverEventListener {  
 //Called after WebElement.clear(), WebElement.sendKeys(...).  
 @Override  
 public void afterChangeValueOf(WebElement element, WebDriver driver) {  
 // TODO Auto-generated method stub  
 System.out.println("inside method afterChangeValueOf on =: " + element.toString());  
 }  
 //Called after WebElement.click()  
 @Override  
 public void afterClickOn(WebElement element, WebDriver driver) {  
 // TODO Auto-generated method stub  
  System.out.println("inside method afterClickOn on =: " + element.toString());  
 }  
 //Called after WebDriver.findElement(...), or WebDriver.findElements(...), or WebElement.findElement(...), or WebElement.findElements(...).  
 @Override  
 public void afterFindBy(By by, WebElement element, WebDriver driver) {  
 // TODO Auto-generated method stub  
  System.out.println("Find happened on =: " + by.toString());  
 }  
 //Called after navigate().back().  
 @Override  
 public void afterNavigateBack(WebDriver driver) {  
 // TODO Auto-generated method stub  
 System.out.println("Inside the after navigateback to =: " + driver.getCurrentUrl());  
 }  
 //Called after navigate().forward().  
 @Override  
 public void afterNavigateForward(WebDriver driver) {  
 // TODO Auto-generated method stub  
 System.out.println("Inside the afterNavigateForward to =: " + driver.getCurrentUrl());  
 }  
 //Called after get(String url) respectively navigate().to(String url).  
 @Override  
 public void afterNavigateTo(String url, WebDriver driver) {  
 // TODO Auto-generated method stub  
 System.out.println("Inside the afterNavigateTo to =: " + url);  
 }  
 //Called after RemoteWebDriver.executeScript(String, Object[]).  
 @Override  
 public void afterScript(String script, WebDriver driver) {  
 // TODO Auto-generated method stub  
 System.out.println("Inside the afterScript to, Script is =: " + script);  
 }  
 //Called before WebElement.clear(), WebElement.sendKeys(...)  
 @Override  
 public void beforeChangeValueOf(WebElement element, WebDriver driver) {  
 // TODO Auto-generated method stub  
 System.out.println("Inside the beforeChangeValueOf method =; "+element.toString());  
 }  
 //Called before WebElement.click().    
 @Override  
 public void beforeClickOn(WebElement element, WebDriver driver) {  
 // TODO Auto-generated method stub  
 System.out.println("About to click on the " + element.toString());  
 }  
 //Called before WebDriver.findElement(...), or WebDriver.findElements(...), or WebElement.findElement(...), or WebElement.findElements(...).  
 @Override  
 public void beforeFindBy(By by, WebElement element, WebDriver driver) {  
   System.out.println("before FindBY =: "+by.toString());  
 }  
 //Called before navigate().back().  
 @Override  
 public void beforeNavigateBack(WebDriver driver) {  
 // TODO Auto-generated method stub  
 System.out.println("Just before beforeNavigateBack =: " + driver.getCurrentUrl());  
 }  
 //Called before navigate().forward().  
 @Override  
 public void beforeNavigateForward(WebDriver driver) {  
 // TODO Auto-generated method stub  
 System.out.println("Just before beforeNavigateForward =: " + driver.getCurrentUrl());  
 }  
 //Called before get(String url) respectively navigate().to(String url).  
 @Override  
 public void beforeNavigateTo(String url, WebDriver driver) {  
 // TODO Auto-generated method stub  
 System.out.println("Just before beforeNavigateTo =: " + url);  
 }  
 //Called before RemoteWebDriver.executeScript(String, Object[])  
 @Override  
 public void beforeScript(String script, WebDriver driver) {  
 // TODO Auto-generated method stub  
 System.out.println("Just before beforeScript =: " + script);  
 }  
 // Called whenever an exception would be thrown.  
 @Override  
 public void onException(Throwable throwable, WebDriver driver) {  
 // TODO Auto-generated method stub  
 System.out.println("Exception occured at =: " + throwable.getMessage());  
 }  
 }  


Create Event Throwing WebDriver
 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 EventListerDemo {  
 public static void main(String[] args) {  
 //Initializing instance of Webdriver  
 WebDriver wDriver = new FirefoxDriver();  

 //Initializing instance of EventFiringWebDriver  
 EventFiringWebDriver driver = new EventFiringWebDriver(wDriver);  
 //Now create object of EventListerHandler to register it with EventFiringWebDriver   
 EventListerHandler handle = new EventListerHandler();  

 System.out.println("===============================");  
 //Registering it with EventFiringWebDriver   
 driver.register(handle);  
 driver.get("http://www.facebook.com");  
 driver.findElement(By.id("email")).sendKeys("abcde@gmail.com");  
 driver.findElement(By.id("pass")).sendKeys("aaaaaaaa");  
 driver.findElement(By.id("loginbutton")).click();  
 driver.get("http://www.gmail.com");  
 driver.get("http://www.outlook.com");  
 driver.navigate().back();  
 driver.navigate().forward();  
 driver.quit();  
 driver.unregister(handle);  
 System.out.println("===============================");  
   }  
 }  

Output:
>

3 comments: