September 2, 2015

Class EventFiringWebDriver and Interface WebDriverEventListener

In this post i will try to explain what is the use of Class EventFiringWebDriver and Interface WebDriverEventListener in selenium webdriver.

EventFiringWebDriver is a class and is used to wrap your webdriver around to throw events.


WebDriverEventListner is an interface which you have to implement to catch the webdriver events. 


EventFiringWebDriver implements WebDriver interface, meaning you will get all regular methods like findElement,get,getText etc...apart from those methods you will also get register and unregister.

Register method is used to register your implementation of WebdriverEventListner to Webdriver events.


Below programs explains you how to implement Class EventFiringWebDriver and Interface WebDriverEventListener.

Here in this example i am trying to 

1. Highlight element before performing any action and 
2. Capture screenshot after every step.

First Lets implement WebDriverEventListener interface...MyListener is class Name which implemnts WebDriverEventListener  interface.

You can get this code from My Gitrepo:

https://github.com/mourya2175/RandD

Or

Google drive.

Experiment_WebDriverEventListener Project


package util;

import java.io.File;
import java.io.IOException;
import java.util.UUID;

import org.apache.commons.io.FileUtils;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.events.WebDriverEventListener;

import experiment.Facebook;

public class MyListener implements WebDriverEventListener{

 public void afterChangeValueOf(WebElement element, WebDriver driver) {
  // TODO Auto-generated method stub

 }

 public void afterClickOn(WebElement element, WebDriver driver) {
  // TODO Auto-generated method stub

 }


 public void afterFindBy(By by, WebElement element, WebDriver driver) {
  //this is class which will generate unique number for screenshot
  UUID sc = UUID.randomUUID();

  //Capture screenshot
  if(Facebook.screenshot)
  {
   File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
   // Now you can do whatever you need to do with it, for example copy somewhere
   try {

    FileUtils.copyFile(scrFile, new File("Screenshots\\"+Facebook.screenshotFolder+"\\"+sc+".jpg"));
   } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }
  }

 }


 public void afterNavigateBack(WebDriver driver) {
  // TODO Auto-generated method stub

 }


 public void afterNavigateForward(WebDriver driver) {
  // TODO Auto-generated method stub

 }


 public void afterNavigateTo(String url, WebDriver driver) {
  // TODO Auto-generated method stub

 }


 public void afterScript(String script, WebDriver driver) {
  // TODO Auto-generated method stub

 }


 public void beforeChangeValueOf(WebElement element, WebDriver driver) {
  // TODO Auto-generated method stub

 }


 public void beforeClickOn(WebElement element, WebDriver driver) {
  // TODO Auto-generated method stub

 }


 public void beforeFindBy(By by, WebElement element, WebDriver driver) {


  if(Facebook.highlight)
  {
   WebElement elem = driver.findElement(by);
   // draw a border around the found element
   if (driver instanceof JavascriptExecutor) {
    ((JavascriptExecutor)driver).executeScript("arguments[0].style.border='2px solid red'", elem);
    //((JavascriptExecutor)driver).executeScript("arguments[0].setAttribute('style', arguments[1]);", elem, "color: yellow; border: 2px solid yellow;"); 
   }
  }

 }




 public void beforeNavigateBack(WebDriver driver) {
  // TODO Auto-generated method stub

 }


 public void beforeNavigateForward(WebDriver driver) {
  // TODO Auto-generated method stub

 }


 public void beforeNavigateTo(String url, WebDriver driver) {
  // TODO Auto-generated method stub

 }


 public void beforeScript(String script, WebDriver driver) {
  // TODO Auto-generated method stub

 }

 public void onException(Throwable throwable, WebDriver driver) {
  // TODO Auto-generated method stub

 }

}

Here is my Class which uses EventFiringWebDriver. Class Name is Facebook.



package experiment;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.events.EventFiringWebDriver;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

import util.MyListener;

public class Facebook {
 public WebDriver wdriver;
 //create a variable for EventFiringWebDriver
 public EventFiringWebDriver driver;
 //boolean value for screenshot --if you set false it will highlight element
 public static boolean screenshot=true;
 //boolean value for highlight --if you set false it will not capture screenshot
 public static boolean highlight=true;
 //date --simple date format for screenshot folder name
 public static Date dNow = new Date( );
 public static SimpleDateFormat ft = new SimpleDateFormat("ddMMyy_HHmmss");
 public static String screenshotFolder=ft.format(dNow);

 @Test
 public void testFacebook() throws Exception {
  driver.get("https://www.facebook.com");
  driver.findElement(By.id("email")).sendKeys("WebDriverEventListener");
  driver.findElement(By.id("pass")).sendKeys("Password");
  driver.findElement(By.id("u_0_1")).sendKeys("FirstName");
  driver.findElement(By.id("u_0_3")).sendKeys("LastName");
  driver.findElement(By.id("u_0_5")).sendKeys("1234567890");
  driver.findElement(By.id("u_0_8")).sendKeys("1234567890");
  driver.findElement(By.id("u_0_a")).sendKeys("password");
  
 }

 @BeforeClass
 public void beforeClass() {
  //create an object for webdriver
  wdriver = new FirefoxDriver();
  //create an object for EventFiringWebDriver
  driver = new EventFiringWebDriver(wdriver);
  //this is class which implements WebDriverEventListener
  MyListener myListener = new MyListener();
  //registering EventFiringWebDriver to normal webdriver
  driver.register(myListener);
  driver.manage().window().maximize();
  driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
  /*System.setProperty("webdriver.chrome.driver", "D:\\Jar\\chromedriver.exe");
  driver = new ChromeDriver();*/
 }

 @AfterClass
 public void afterClass() throws Exception {
  driver.quit();
 }

}


1 comment: