Class WebDriverListener

  • All Implemented Interfaces:
    org.openqa.selenium.support.events.WebDriverEventListener

    public class WebDriverListener
    extends Object
    implements org.openqa.selenium.support.events.WebDriverEventListener
    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. We're implementing a core WebDriver interface in WebDriverEventListener here, so the declarations of methods that have no contents is unfortunately necessary. It just means in our world that these "empty" methods do get called, but nothing happens, they just exit.
    • Method Summary

      Modifier and Type Method Description
      void afterAlertAccept​(org.openqa.selenium.WebDriver webDriver)  
      void afterAlertDismiss​(org.openqa.selenium.WebDriver webDriver)  
      void afterChangeValueOf​(org.openqa.selenium.WebElement webElement, org.openqa.selenium.WebDriver webDriver, CharSequence[] charSequences)  
      void afterClickOn​(org.openqa.selenium.WebElement webElement, org.openqa.selenium.WebDriver webDriver)  
      void afterFindBy​(org.openqa.selenium.By by, org.openqa.selenium.WebElement webElement, org.openqa.selenium.WebDriver webDriver)  
      <X> void afterGetScreenshotAs​(org.openqa.selenium.OutputType<X> outputType, X x)  
      void afterGetText​(org.openqa.selenium.WebElement webElement, org.openqa.selenium.WebDriver webDriver, String s)  
      void afterNavigateBack​(org.openqa.selenium.WebDriver webDriver)  
      void afterNavigateForward​(org.openqa.selenium.WebDriver webDriver)  
      void afterNavigateRefresh​(org.openqa.selenium.WebDriver webDriver)  
      void afterNavigateTo​(String s, org.openqa.selenium.WebDriver webDriver)  
      void afterScript​(String s, org.openqa.selenium.WebDriver webDriver)  
      void afterSwitchToWindow​(String s, org.openqa.selenium.WebDriver webDriver)  
      void beforeAlertAccept​(org.openqa.selenium.WebDriver webDriver)  
      void beforeAlertDismiss​(org.openqa.selenium.WebDriver webDriver)  
      void beforeChangeValueOf​(org.openqa.selenium.WebElement webElement, org.openqa.selenium.WebDriver webDriver, CharSequence[] charSequences)  
      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.
      <X> void beforeGetScreenshotAs​(org.openqa.selenium.OutputType<X> outputType)  
      void beforeGetText​(org.openqa.selenium.WebElement webElement, org.openqa.selenium.WebDriver webDriver)  
      void beforeNavigateBack​(org.openqa.selenium.WebDriver webDriver)  
      void beforeNavigateForward​(org.openqa.selenium.WebDriver webDriver)  
      void beforeNavigateRefresh​(org.openqa.selenium.WebDriver webDriver)  
      void beforeNavigateTo​(String s, org.openqa.selenium.WebDriver webDriver)  
      void beforeScript​(String s, org.openqa.selenium.WebDriver webDriver)  
      void beforeSwitchToWindow​(String s, org.openqa.selenium.WebDriver webDriver)  
      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()
    • Constructor Detail

      • WebDriverListener

        public WebDriverListener()
    • Method Detail

      • 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
      • beforeAlertAccept

        public void beforeAlertAccept​(org.openqa.selenium.WebDriver webDriver)
        Specified by:
        beforeAlertAccept in interface org.openqa.selenium.support.events.WebDriverEventListener
        Parameters:
        webDriver - active WebDriver instance
      • afterAlertAccept

        public void afterAlertAccept​(org.openqa.selenium.WebDriver webDriver)
        Specified by:
        afterAlertAccept in interface org.openqa.selenium.support.events.WebDriverEventListener
        Parameters:
        webDriver - active WebDriver instance
      • afterAlertDismiss

        public void afterAlertDismiss​(org.openqa.selenium.WebDriver webDriver)
        Specified by:
        afterAlertDismiss in interface org.openqa.selenium.support.events.WebDriverEventListener
        Parameters:
        webDriver - active WebDriver instance
      • beforeAlertDismiss

        public void beforeAlertDismiss​(org.openqa.selenium.WebDriver webDriver)
        Specified by:
        beforeAlertDismiss in interface org.openqa.selenium.support.events.WebDriverEventListener
        Parameters:
        webDriver - active WebDriver instance
      • beforeNavigateTo

        public void beforeNavigateTo​(String s,
                                     org.openqa.selenium.WebDriver webDriver)
        Specified by:
        beforeNavigateTo in interface org.openqa.selenium.support.events.WebDriverEventListener
        Parameters:
        s - String
        webDriver - active WebDriver instance
      • afterNavigateTo

        public void afterNavigateTo​(String s,
                                    org.openqa.selenium.WebDriver webDriver)
        Specified by:
        afterNavigateTo in interface org.openqa.selenium.support.events.WebDriverEventListener
        Parameters:
        s - String
        webDriver - active WebDriver instance
      • beforeNavigateBack

        public void beforeNavigateBack​(org.openqa.selenium.WebDriver webDriver)
        Specified by:
        beforeNavigateBack in interface org.openqa.selenium.support.events.WebDriverEventListener
        Parameters:
        webDriver - active WebDriver instance
      • afterNavigateBack

        public void afterNavigateBack​(org.openqa.selenium.WebDriver webDriver)
        Specified by:
        afterNavigateBack in interface org.openqa.selenium.support.events.WebDriverEventListener
        Parameters:
        webDriver - active WebDriver instance
      • beforeNavigateForward

        public void beforeNavigateForward​(org.openqa.selenium.WebDriver webDriver)
        Specified by:
        beforeNavigateForward in interface org.openqa.selenium.support.events.WebDriverEventListener
        Parameters:
        webDriver - active WebDriver instance
      • afterNavigateForward

        public void afterNavigateForward​(org.openqa.selenium.WebDriver webDriver)
        Specified by:
        afterNavigateForward in interface org.openqa.selenium.support.events.WebDriverEventListener
        Parameters:
        webDriver - active WebDriver instance
      • beforeNavigateRefresh

        public void beforeNavigateRefresh​(org.openqa.selenium.WebDriver webDriver)
        Specified by:
        beforeNavigateRefresh in interface org.openqa.selenium.support.events.WebDriverEventListener
        Parameters:
        webDriver - active WebDriver instance
      • afterNavigateRefresh

        public void afterNavigateRefresh​(org.openqa.selenium.WebDriver webDriver)
        Specified by:
        afterNavigateRefresh in interface org.openqa.selenium.support.events.WebDriverEventListener
        Parameters:
        webDriver - active WebDriver instance
      • 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
        Parameters:
        by - locator
        webElement - active WebElement, already located
        webDriver - active WebDriver instance
      • afterFindBy

        public void afterFindBy​(org.openqa.selenium.By by,
                                org.openqa.selenium.WebElement webElement,
                                org.openqa.selenium.WebDriver webDriver)
        Specified by:
        afterFindBy in interface org.openqa.selenium.support.events.WebDriverEventListener
        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
        Parameters:
        webElement - active WebElement, already located
        webDriver - active WebDriver instance
      • afterClickOn

        public void afterClickOn​(org.openqa.selenium.WebElement webElement,
                                 org.openqa.selenium.WebDriver webDriver)
        Specified by:
        afterClickOn in interface org.openqa.selenium.support.events.WebDriverEventListener
        Parameters:
        webElement - active WebElement, already located
        webDriver - active WebDriver instance
      • beforeChangeValueOf

        public void beforeChangeValueOf​(org.openqa.selenium.WebElement webElement,
                                        org.openqa.selenium.WebDriver webDriver,
                                        CharSequence[] charSequences)
        Specified by:
        beforeChangeValueOf in interface org.openqa.selenium.support.events.WebDriverEventListener
        Parameters:
        webElement - active WebElement, already located
        webDriver - active WebDriver instance
        charSequences - character sequence
      • afterChangeValueOf

        public void afterChangeValueOf​(org.openqa.selenium.WebElement webElement,
                                       org.openqa.selenium.WebDriver webDriver,
                                       CharSequence[] charSequences)
        Specified by:
        afterChangeValueOf in interface org.openqa.selenium.support.events.WebDriverEventListener
        Parameters:
        webElement - active WebElement, already located
        webDriver - active WebDriver instance
        charSequences - character sequence
      • beforeScript

        public void beforeScript​(String s,
                                 org.openqa.selenium.WebDriver webDriver)
        Specified by:
        beforeScript in interface org.openqa.selenium.support.events.WebDriverEventListener
        Parameters:
        s - String
        webDriver - active WebDriver instance
      • afterScript

        public void afterScript​(String s,
                                org.openqa.selenium.WebDriver webDriver)
        Specified by:
        afterScript in interface org.openqa.selenium.support.events.WebDriverEventListener
        Parameters:
        s - String
        webDriver - active WebDriver instance
      • beforeSwitchToWindow

        public void beforeSwitchToWindow​(String s,
                                         org.openqa.selenium.WebDriver webDriver)
        Specified by:
        beforeSwitchToWindow in interface org.openqa.selenium.support.events.WebDriverEventListener
        Parameters:
        s - String
        webDriver - active WebDriver instance
      • afterSwitchToWindow

        public void afterSwitchToWindow​(String s,
                                        org.openqa.selenium.WebDriver webDriver)
        Specified by:
        afterSwitchToWindow in interface org.openqa.selenium.support.events.WebDriverEventListener
        Parameters:
        s - String
        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
        Parameters:
        throwable - the thrown exception that we are holding here
        webDriver - active WebDriver instance
      • beforeGetScreenshotAs

        public <X> void beforeGetScreenshotAs​(org.openqa.selenium.OutputType<X> outputType)
        Specified by:
        beforeGetScreenshotAs in interface org.openqa.selenium.support.events.WebDriverEventListener
        Type Parameters:
        X - x
        Parameters:
        outputType - output type
      • afterGetScreenshotAs

        public <X> void afterGetScreenshotAs​(org.openqa.selenium.OutputType<X> outputType,
                                             X x)
        Specified by:
        afterGetScreenshotAs in interface org.openqa.selenium.support.events.WebDriverEventListener
        Type Parameters:
        X - x
        Parameters:
        outputType - output type
        x - x
      • beforeGetText

        public void beforeGetText​(org.openqa.selenium.WebElement webElement,
                                  org.openqa.selenium.WebDriver webDriver)
        Specified by:
        beforeGetText in interface org.openqa.selenium.support.events.WebDriverEventListener
        Parameters:
        webElement - active WebElement, already located
        webDriver - active WebDriver instance
      • afterGetText

        public void afterGetText​(org.openqa.selenium.WebElement webElement,
                                 org.openqa.selenium.WebDriver webDriver,
                                 String s)
        Specified by:
        afterGetText in interface org.openqa.selenium.support.events.WebDriverEventListener
        Parameters:
        webElement - active WebElement, already located
        webDriver - active WebDriver instance
        s - String