Generate Logs with Event Firing WebDriver

Hello Friends
While coding, it is very tedious task to create Log for each statement, although it is very helpful while digging the failures.
Logs are also very helpful for detailed investigation of failures and Exceptions. Recently, someone asked how to have Selenium Web-driver create Log if an exception is thrown. Here is how you do it
Normally you use following code to initializing Web-Driver

WebDriver driver;
driver= new InternetExplorerDriver();

You need to Slightly Modify your Code as below

WebDriver driver;
WebDriverEventListner eventListner = new MyEventListner();
driver = new EventFiringWebDriver(new InternetExplorerDriver()).register(eventListner);

And you need to add MyEventListener Class in your project and below is the the code of MyEventListener.java

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

public class MyEventListener implements WebDriverEventListener {
  public void beforeNavigateTo(String url, WebDriver driver) {
     // TODO Auto-generated method stub
  }
  public void afterNavigateTo(String url, WebDriver driver) {
    // TODO Auto-generated method stub
  }
  public void beforeNavigateBack(WebDriver driver) {
       // TODO Auto-generated method stub
  }
  public void afterNavigateBack(WebDriver driver) {
       // TODO Auto-generated method stub
  }
  public void beforeNavigateForward(WebDriver driver) {
       // TODO Auto-generated method stub
  }
  public void afterNavigateForward(WebDriver driver) {
       // TODO Auto-generated method stub
  }
  public void beforeFindBy(By by, WebElement element, WebDriver driver) {
       // TODO Auto-generated method stub
  }
  public void afterFindBy(By by, WebElement element, WebDriver driver) {
       // TODO Auto-generated method stub
  }
  public void beforeClickOn(WebElement element, WebDriver driver) {
       // TODO Auto-generated method stub
  }
  public void afterClickOn(WebElement element, WebDriver driver) {
       // TODO Auto-generated method stub
  }
  public void beforeChangeValueOf(WebElement element, WebDriver driver) {
       // TODO Auto-generated method stub
  }
  public void afterChangeValueOf(WebElement element, WebDriver driver) {
       // TODO Auto-generated method stub
  }
  public void beforeScript(String script, WebDriver driver) {
       // TODO Auto-generated method stub
  }
  public void afterScript(String script, WebDriver driver) {
       // TODO Auto-generated method stub
  }
  public void onException(Throwable throwable, WebDriver driver) {
       // TODO Auto-generated method stub
  }
}

This is it….. now just add the code for logging for events which you want to…. and will create the log automatically

And to take Screen shot on Each Exception you can add your screen shot code in Method onException();

Be happy… Keep Automating….

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s