public class SharedWebDriver extends AbstractDelegatingWebDriver
This solution represents a compromise between test performances and reliability: we are not in a perfectly clean environment at the beginning of every test, but we are not paying the price of creating a new browser instance.
This implementation supports two types of test: without any dependency injection mechanism and with dependency injection, like Spring Test or Cucumber JVM.
No injection
A classic singleton pattern is implemented and the browser will be destroyed upon JVM destruction.
New browser instances should be obtained by using one of the
open static methods. The no argument version uses the
selenium.driver system property to specify the delegate implementation
web driver class.
In case a browser shutdown is required, the static destroy
method must be used as both close and quit
WebDriver methods implementations prevents the browser process to be closed
by leaving at least one browser window open.
With injection
The instance creation must occur throughout one of the open
static methods.
Browser shutdown upon test suite completion is still supported through JVM shutdown hook.
The following is an example of Spring configuration:
<bean id="driver" class="org.agileware.test.web.SharedWebDriver" factory-method="open">
<constructor-arg ref="delegate" />
</bean>
<bean id="delegate" class="org.openqa.selenium.chrome.ChromeDriver">
<!-- capabilities can be set here -->
</bean>
AbstractDelegatingWebDriver.DelegatingWebElementorg.openqa.selenium.WebDriver.ImeHandler, org.openqa.selenium.WebDriver.Navigation, org.openqa.selenium.WebDriver.Options, org.openqa.selenium.WebDriver.TargetLocator, org.openqa.selenium.WebDriver.Timeouts, org.openqa.selenium.WebDriver.Window| Modifier and Type | Field and Description |
|---|---|
static String |
SELENIUM_DRIVER_PROPERTY |
delegate| Modifier | Constructor and Description |
|---|---|
protected |
SharedWebDriver(org.openqa.selenium.WebDriver delegate)
Creates a new instance wrapping the provided delegate WebDriver
implementation.
|
| Modifier and Type | Method and Description |
|---|---|
void |
close()
Closes the current browser window only if it is not the last open one.
|
static void |
destroy()
Destroys the current shared instance closing all the browser windows.
|
static SharedWebDriver |
open()
Returns the current shared instance or creates a new one using the
selenium.driver system property to determine the delegate
implementation.
|
static SharedWebDriver |
open(Class<? extends org.openqa.selenium.WebDriver> delegate)
Returns the current shared instance or creates a new one using the
provided WebDriver implementation type to create the delegate instance.
|
static SharedWebDriver |
open(org.openqa.selenium.WebDriver delegate)
Returns the current shared instance or creates a new one using the
provided delegate implementation.
|
void |
quit()
Closes all browser windows but the currently selected one which is
reused.
|
executeAsyncScript, executeScript, findElement, findElements, get, getCurrentUrl, getDelegate, getPageSource, getScreenshotAs, getTitle, getWindowHandle, getWindowHandles, manage, navigate, switchTopublic static final String SELENIUM_DRIVER_PROPERTY
protected SharedWebDriver(org.openqa.selenium.WebDriver delegate)
delegate - the delegate WebDriver instancepublic static SharedWebDriver open(org.openqa.selenium.WebDriver delegate)
delegate - the delegate WebDriver instance to use if no shared instance
is availablepublic static SharedWebDriver open() throws org.openqa.selenium.WebDriverException
org.openqa.selenium.WebDriverException - if the delegate implementation cannot be createdpublic static SharedWebDriver open(Class<? extends org.openqa.selenium.WebDriver> delegate) throws org.openqa.selenium.WebDriverException
delegate - the WebDriver implementation typeorg.openqa.selenium.WebDriverException - if the delegate instance cannot be createdpublic void close()
close in interface org.openqa.selenium.WebDriverclose in class AbstractDelegatingWebDriverWebDriver.close()public void quit()
quit in interface org.openqa.selenium.WebDriverquit in class AbstractDelegatingWebDriverWebDriver.quit()public static void destroy()
Copyright © 2015. All rights reserved.