Package uk.co.evoco.webdriver
Class WebDriverListener
- java.lang.Object
-
- uk.co.evoco.webdriver.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.
-
-
Constructor Summary
Constructors Constructor Description WebDriverListener()
-
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()
-
-
-
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 interfaceorg.openqa.selenium.support.events.WebDriverEventListener
- Parameters:
webDriver
- active WebDriver instance
-
afterAlertAccept
public void afterAlertAccept(org.openqa.selenium.WebDriver webDriver)
- Specified by:
afterAlertAccept
in interfaceorg.openqa.selenium.support.events.WebDriverEventListener
- Parameters:
webDriver
- active WebDriver instance
-
afterAlertDismiss
public void afterAlertDismiss(org.openqa.selenium.WebDriver webDriver)
- Specified by:
afterAlertDismiss
in interfaceorg.openqa.selenium.support.events.WebDriverEventListener
- Parameters:
webDriver
- active WebDriver instance
-
beforeAlertDismiss
public void beforeAlertDismiss(org.openqa.selenium.WebDriver webDriver)
- Specified by:
beforeAlertDismiss
in interfaceorg.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 interfaceorg.openqa.selenium.support.events.WebDriverEventListener
- Parameters:
s
- StringwebDriver
- active WebDriver instance
-
afterNavigateTo
public void afterNavigateTo(String s, org.openqa.selenium.WebDriver webDriver)
- Specified by:
afterNavigateTo
in interfaceorg.openqa.selenium.support.events.WebDriverEventListener
- Parameters:
s
- StringwebDriver
- active WebDriver instance
-
beforeNavigateBack
public void beforeNavigateBack(org.openqa.selenium.WebDriver webDriver)
- Specified by:
beforeNavigateBack
in interfaceorg.openqa.selenium.support.events.WebDriverEventListener
- Parameters:
webDriver
- active WebDriver instance
-
afterNavigateBack
public void afterNavigateBack(org.openqa.selenium.WebDriver webDriver)
- Specified by:
afterNavigateBack
in interfaceorg.openqa.selenium.support.events.WebDriverEventListener
- Parameters:
webDriver
- active WebDriver instance
-
beforeNavigateForward
public void beforeNavigateForward(org.openqa.selenium.WebDriver webDriver)
- Specified by:
beforeNavigateForward
in interfaceorg.openqa.selenium.support.events.WebDriverEventListener
- Parameters:
webDriver
- active WebDriver instance
-
afterNavigateForward
public void afterNavigateForward(org.openqa.selenium.WebDriver webDriver)
- Specified by:
afterNavigateForward
in interfaceorg.openqa.selenium.support.events.WebDriverEventListener
- Parameters:
webDriver
- active WebDriver instance
-
beforeNavigateRefresh
public void beforeNavigateRefresh(org.openqa.selenium.WebDriver webDriver)
- Specified by:
beforeNavigateRefresh
in interfaceorg.openqa.selenium.support.events.WebDriverEventListener
- Parameters:
webDriver
- active WebDriver instance
-
afterNavigateRefresh
public void afterNavigateRefresh(org.openqa.selenium.WebDriver webDriver)
- Specified by:
afterNavigateRefresh
in interfaceorg.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 interfaceorg.openqa.selenium.support.events.WebDriverEventListener
- Parameters:
by
- locatorwebElement
- active WebElement, already locatedwebDriver
- 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 interfaceorg.openqa.selenium.support.events.WebDriverEventListener
- 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:
beforeClickOn
in interfaceorg.openqa.selenium.support.events.WebDriverEventListener
- Parameters:
webElement
- active WebElement, already locatedwebDriver
- active WebDriver instance
-
afterClickOn
public void afterClickOn(org.openqa.selenium.WebElement webElement, org.openqa.selenium.WebDriver webDriver)
- Specified by:
afterClickOn
in interfaceorg.openqa.selenium.support.events.WebDriverEventListener
- Parameters:
webElement
- active WebElement, already locatedwebDriver
- active WebDriver instance
-
beforeChangeValueOf
public void beforeChangeValueOf(org.openqa.selenium.WebElement webElement, org.openqa.selenium.WebDriver webDriver, CharSequence[] charSequences)
- Specified by:
beforeChangeValueOf
in interfaceorg.openqa.selenium.support.events.WebDriverEventListener
- Parameters:
webElement
- active WebElement, already locatedwebDriver
- active WebDriver instancecharSequences
- character sequence
-
afterChangeValueOf
public void afterChangeValueOf(org.openqa.selenium.WebElement webElement, org.openqa.selenium.WebDriver webDriver, CharSequence[] charSequences)
- Specified by:
afterChangeValueOf
in interfaceorg.openqa.selenium.support.events.WebDriverEventListener
- Parameters:
webElement
- active WebElement, already locatedwebDriver
- active WebDriver instancecharSequences
- character sequence
-
beforeScript
public void beforeScript(String s, org.openqa.selenium.WebDriver webDriver)
- Specified by:
beforeScript
in interfaceorg.openqa.selenium.support.events.WebDriverEventListener
- Parameters:
s
- StringwebDriver
- active WebDriver instance
-
afterScript
public void afterScript(String s, org.openqa.selenium.WebDriver webDriver)
- Specified by:
afterScript
in interfaceorg.openqa.selenium.support.events.WebDriverEventListener
- Parameters:
s
- StringwebDriver
- active WebDriver instance
-
beforeSwitchToWindow
public void beforeSwitchToWindow(String s, org.openqa.selenium.WebDriver webDriver)
- Specified by:
beforeSwitchToWindow
in interfaceorg.openqa.selenium.support.events.WebDriverEventListener
- Parameters:
s
- StringwebDriver
- active WebDriver instance
-
afterSwitchToWindow
public void afterSwitchToWindow(String s, org.openqa.selenium.WebDriver webDriver)
- Specified by:
afterSwitchToWindow
in interfaceorg.openqa.selenium.support.events.WebDriverEventListener
- Parameters:
s
- StringwebDriver
- 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 interfaceorg.openqa.selenium.support.events.WebDriverEventListener
- Parameters:
throwable
- the thrown exception that we are holding herewebDriver
- active WebDriver instance
-
beforeGetScreenshotAs
public <X> void beforeGetScreenshotAs(org.openqa.selenium.OutputType<X> outputType)
- Specified by:
beforeGetScreenshotAs
in interfaceorg.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 interfaceorg.openqa.selenium.support.events.WebDriverEventListener
- Type Parameters:
X
- x- Parameters:
outputType
- output typex
- x
-
beforeGetText
public void beforeGetText(org.openqa.selenium.WebElement webElement, org.openqa.selenium.WebDriver webDriver)
- Specified by:
beforeGetText
in interfaceorg.openqa.selenium.support.events.WebDriverEventListener
- Parameters:
webElement
- active WebElement, already locatedwebDriver
- active WebDriver instance
-
afterGetText
public void afterGetText(org.openqa.selenium.WebElement webElement, org.openqa.selenium.WebDriver webDriver, String s)
- Specified by:
afterGetText
in interfaceorg.openqa.selenium.support.events.WebDriverEventListener
- Parameters:
webElement
- active WebElement, already locatedwebDriver
- active WebDriver instances
- String
-
-