Class WebDriverListener

java.lang.Object
org.openqa.selenium.support.events.AbstractWebDriverEventListener
com.version1.webdriver.WebDriverListener
All Implemented Interfaces:
org.openqa.selenium.support.events.WebDriverEventListener

public class WebDriverListener extends org.openqa.selenium.support.events.AbstractWebDriverEventListener
This class allows us hooks into before and afters of a lot of WebDriver internals This is a great way to capture screenshots and add conditional waits to actions without making tests and page objects have superfluous code to do these things.
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    beforeClickOn(org.openqa.selenium.WebElement webElement, org.openqa.selenium.WebDriver webDriver)
    Before we interact with any methods (which is anytime we click() on anything) we want to do a state check that the element is actually clickable.
    void
    beforeFindBy(org.openqa.selenium.By by, org.openqa.selenium.WebElement webElement, org.openqa.selenium.WebDriver webDriver)
    Before each webDriver.findBy or @FindBy we want to make sure that we are supply elements that are present in the DOM.
    void
    onException(Throwable throwable, org.openqa.selenium.WebDriver webDriver)
    If we have an exception, lets create a screenshot so we can see the page as it happened.
    void
    setScreenshotDirectory(File screenshotDirectory)
    Sets the screenshot target directory that will be used for screenshots generated inside onException()

    Methods inherited from class org.openqa.selenium.support.events.AbstractWebDriverEventListener

    afterAlertAccept, afterAlertDismiss, afterChangeValueOf, afterClickOn, afterFindBy, afterGetScreenshotAs, afterGetText, afterNavigateBack, afterNavigateForward, afterNavigateRefresh, afterNavigateTo, afterScript, afterSwitchToWindow, beforeAlertAccept, beforeAlertDismiss, beforeChangeValueOf, beforeGetScreenshotAs, beforeGetText, beforeNavigateBack, beforeNavigateForward, beforeNavigateRefresh, beforeNavigateTo, beforeScript, beforeSwitchToWindow

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • WebDriverListener

      public WebDriverListener()
  • Method Details

    • setScreenshotDirectory

      public void setScreenshotDirectory(File screenshotDirectory)
      Sets the screenshot target directory that will be used for screenshots generated inside onException()
      Parameters:
      screenshotDirectory - the path to the screenshot directory used in onException
    • beforeFindBy

      public void beforeFindBy(org.openqa.selenium.By by, org.openqa.selenium.WebElement webElement, org.openqa.selenium.WebDriver webDriver)
      Before each webDriver.findBy or @FindBy we want to make sure that we are supply elements that are present in the DOM. This ensures that we avoid, to some degree, StaleElementExceptions (although this is generally not the best way to avoid those types of exceptions).
      Specified by:
      beforeFindBy in interface org.openqa.selenium.support.events.WebDriverEventListener
      Overrides:
      beforeFindBy in class org.openqa.selenium.support.events.AbstractWebDriverEventListener
      Parameters:
      by - locator
      webElement - active WebElement, already located
      webDriver - active WebDriver instance
    • beforeClickOn

      public void beforeClickOn(org.openqa.selenium.WebElement webElement, org.openqa.selenium.WebDriver webDriver)
      Before we interact with any methods (which is anytime we click() on anything) we want to do a state check that the element is actually clickable. This ensures that elements that are disabled or not visible are given time (as little as they need) to be ready to be interacted with. A valid case for this is where a button may be disabled until form fields are automatically valid.
      Specified by:
      beforeClickOn in interface org.openqa.selenium.support.events.WebDriverEventListener
      Overrides:
      beforeClickOn in class org.openqa.selenium.support.events.AbstractWebDriverEventListener
      Parameters:
      webElement - active WebElement, already located
      webDriver - active WebDriver instance
    • onException

      public void onException(Throwable throwable, org.openqa.selenium.WebDriver webDriver)
      If we have an exception, lets create a screenshot so we can see the page as it happened.
      Specified by:
      onException in interface org.openqa.selenium.support.events.WebDriverEventListener
      Overrides:
      onException in class org.openqa.selenium.support.events.AbstractWebDriverEventListener
      Parameters:
      throwable - the thrown exception that we are holding here
      webDriver - active WebDriver instance