Package uk.co.evoco.webdriver
Class WebDriverListener
java.lang.Object
org.openqa.selenium.support.events.AbstractWebDriverEventListener
uk.co.evoco.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 WebDriverListener() -
Method Summary
Modifier and Type Method Description voidbeforeClickOn(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.voidbeforeFindBy(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.voidonException(java.lang.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.voidsetScreenshotDirectory(java.io.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
-
Constructor Details
-
WebDriverListener
public WebDriverListener()
-
-
Method Details
-
setScreenshotDirectory
public void setScreenshotDirectory(java.io.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:
beforeFindByin interfaceorg.openqa.selenium.support.events.WebDriverEventListener- Overrides:
beforeFindByin classorg.openqa.selenium.support.events.AbstractWebDriverEventListener- Parameters:
by- locatorwebElement- active WebElement, already locatedwebDriver- 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:
beforeClickOnin interfaceorg.openqa.selenium.support.events.WebDriverEventListener- Overrides:
beforeClickOnin classorg.openqa.selenium.support.events.AbstractWebDriverEventListener- Parameters:
webElement- active WebElement, already locatedwebDriver- active WebDriver instance
-
onException
public void onException(java.lang.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:
onExceptionin interfaceorg.openqa.selenium.support.events.WebDriverEventListener- Overrides:
onExceptionin classorg.openqa.selenium.support.events.AbstractWebDriverEventListener- Parameters:
throwable- the thrown exception that we are holding herewebDriver- active WebDriver instance
-