How to extend Listener 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.
AbstractWebDriverEventListener: is a Abstract class
EventFiringWebDriver: This is an class that actually fire Webdriver event

Create a class called AbstractHandle to override events just by Extending AbstractWebDriverEventListener abstract class where AbstractWebDriverEventListener is a abstract which have near about 15 methods that we can override to see after and before tracking of each events that we trigger through our scripts. Creating one object of EventFiringWebDriver just by calling one of its constructor which takes instance of WebDriver.

 import org.openqa.selenium.By;  
 import org.openqa.selenium.WebDriver;  
 import org.openqa.selenium.WebElement;  
 import org.openqa.selenium.support.events.AbstractWebDriverEventListener;  
 public class AbstractHandle extends AbstractWebDriverEventListener {  
 //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.AbstractWebDriverEventListener;  
 public class AbstractHandle extends AbstractWebDriverEventListenerr {  
 //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 AbstractListernerDemo {  
 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   
 AbstractHandle handle = new AbstractHandle ();  
 System.out.println("===============================");  
 //register the object of this event listener class 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:
>



4 comments:

  1. I am implementing WebDriverEventListener interface using facebook/webdriver and php and defining certain useful Log statements in the methods
    which would be displayed/logged as the application under test is being run.
    but after run i am getting error:

    i am using facebook/webdriver dev-master version

    here is my code :
    public function beforeChangeValueOf(EventFiringWebElement $element) {

    var_dump($element);
    $var = settype($element, 'string');

    echo "Value of the: {$var} before any changes made".PHP_EOL;
    }

    Value of the: 1 before any changes made
    class EventFiringWebElement#30 (2) {
    protected $element =>
    class RemoteWebElement#29 (3) {
    protected $executor =>
    class RemoteExecuteMethod#28 (1) {
    private $driver =>
    class RemoteWebDriver#18 (6) {
    ...
    }
    }
    protected $id =>
    string(1) "2"
    protected $fileDetector =>
    class UselessFileDetector#26 (0) {
    }
    }
    protected $dispatcher =>
    class WebDriverDispatcher#21 (2) {
    protected $listeners =>
    array(1) {
    [0] =>
    class SeleniumEventListener#20 (0) {
    ...
    }
    }
    protected $driver =>
    class EventFiringWebDriver#17 (2) {
    protected $driver =>
    class RemoteWebDriver#18 (6) {
    ...
    }
    protected $dispatcher =>
    ...

    }
    }
    }

    Time: 41.36 seconds, Memory: 4.00MB

    There was 1 error:

    Object of class EventFiringWebElement could not be converted to string

    C:\xampp\htdocs\mhprof\tests\SeleniumEventListener.php:41
    C:\xampp\htdocs\mhprof\vendor\facebook\webdriver\lib\WebDriverDispatcher.php:75
    C:\xampp\htdocs\mhprof\vendor\facebook\webdriver\lib\support\events\EventFiringW
    ebElement.php:56
    C:\xampp\htdocs\mhprof\vendor\facebook\webdriver\lib\support\events\EventFiringW
    ebElement.php:81

    ReplyDelete
  2. "Great blog created by you. I read your blog, its best and useful information. You have done a great work. Super blogging and keep it up.php jobs in hyderabad.
    "

    ReplyDelete
  3. This is very informative blog for learners, Thanks for sharing this information Android Online Training Hyderabad

    ReplyDelete
  4. This One Post In this blog cleared All my doubts On Selenium Web Driver Integration With java

    ReplyDelete