Skip to content

Do not persist a driver instance in the configuration object but in the browser #266

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ class DriverConfigSpec extends Specification implements InlineConfigurationLoade
"""

then:
config.driver instanceof FirefoxDriver
config.createDriver() instanceof FirefoxDriver

cleanup:
CachingDriverFactory.clearCacheAndQuitDriver()
Expand All @@ -72,7 +72,7 @@ class DriverConfigSpec extends Specification implements InlineConfigurationLoade
"""

then:
config.driver instanceof FirefoxDriver
config.createDriver() instanceof FirefoxDriver

cleanup:
CachingDriverFactory.clearCacheAndQuitDriver()
Expand All @@ -90,7 +90,7 @@ class DriverConfigSpec extends Specification implements InlineConfigurationLoade
"""

then:
config.driver instanceof FirefoxDriver
config.createDriver() instanceof FirefoxDriver

cleanup:
CachingDriverFactory.clearCacheAndQuitDriver()
Expand Down Expand Up @@ -130,7 +130,7 @@ class DriverConfigSpec extends Specification implements InlineConfigurationLoade
""")

then:
config.driver in driverClass
config.createDriver() in driverClass

cleanup:
CachingDriverFactory.clearCacheAndQuitDriver()
Expand All @@ -140,4 +140,4 @@ class DriverConfigSpec extends Specification implements InlineConfigurationLoade
null | HtmlUnitDriver
"remote" | RemoteWebDriver
}
}
}
4 changes: 2 additions & 2 deletions gradle/codenarc/rulesets.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ ruleset {
doNotApplyToFileNames = 'GebTest.groovy, GebReportingTest.groovy'
}
EmptyMethod {
doNotApplyToClassNames = 'UsingRuleWithoutTestManagerTest, UsingReportingRuleWithoutTestManagerTest'
doNotApplyToClassNames = 'UsingRuleWithoutTestManagerTest, UsingReportingRuleWithoutTestManagerTest, Configuration'
}
}
ruleset('rulesets/braces.xml')
Expand Down Expand Up @@ -190,4 +190,4 @@ ruleset {
}
}
ruleset('rulesets/unused.xml')
}
}
52 changes: 32 additions & 20 deletions module/geb-core/src/main/groovy/geb/Browser.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ class Browser {

private Page page
private final Configuration config
private WebDriver driver
private final RemoteDriverOperations remoteDriverOperations = new RemoteDriverOperations(this.class.classLoader)
private String reportGroup = null
private NavigatorFactory navigatorFactory = null
Expand All @@ -68,7 +69,7 @@ class Browser {
* If the driver is remote, this object allows access to its capabilities (users of Geb should not access this object, it is used internally).
*/
@Lazy
WebDriver augmentedDriver = remoteDriverOperations.getAugmentedDriver(driver)
WebDriver augmentedDriver = remoteDriverOperations.getAugmentedDriver(getDriver())

/**
* Create a new browser with a default configuration loader, loading the default configuration file.
Expand Down Expand Up @@ -164,10 +165,14 @@ class Browser {
* <p>
* The driver implementation to use is determined by the configuration.
*
* @see geb.Configuration#getDriver()
* @see geb.Configuration#createDriver()
*/
WebDriver getDriver() {
config.driver
if (driver == null) {
driver = createDriver()
}

driver
}

/**
Expand All @@ -189,11 +194,9 @@ class Browser {
* This should only be called before making any requests as a means to override the driver instance
* that would be created from the configuration. Where possible, prefer using the configuration mechanism
* to specify the driver implementation to use.
* <p>
* This method delegates to {@link geb.Configuration#setDriver(org.openqa.selenium.WebDriver)}.
*/
void setDriver(WebDriver driver) {
config.driver = driver
this.driver = driver
}

/**
Expand Down Expand Up @@ -223,7 +226,7 @@ class Browser {
* @see org.openqa.selenium.WebDriver#getCurrentUrl()
*/
String getCurrentUrl() {
driver.currentUrl
getDriver().currentUrl
}

/**
Expand Down Expand Up @@ -509,9 +512,9 @@ class Browser {
void go(Map params = [:], String url = null, UrlFragment fragment = null) {
def newUri = calculateUri(url, params, fragment)
def currentUri = retrieveCurrentUri()
driver.get(newUri.toString())
getDriver().get(newUri.toString())
if (sameUrlWithDifferentFragment(currentUri, newUri)) {
driver.navigate().refresh()
getDriver().navigate().refresh()
}
if (!page) {
page(Page)
Expand Down Expand Up @@ -630,7 +633,7 @@ class Browser {
* @see org.openqa.selenium.WebDriver.Options#deleteAllCookies()
*/
void clearCookies() {
driver?.manage()?.deleteAllCookies()
getDriver()?.manage()?.deleteAllCookies()
}

/**
Expand Down Expand Up @@ -669,7 +672,7 @@ class Browser {
* @see org.openqa.selenium.WebDriver#quit()
*/
void quit() {
driver.quit()
getDriver().quit()
}

/**
Expand All @@ -678,7 +681,7 @@ class Browser {
* @see org.openqa.selenium.WebDriver#close()
*/
void close() {
driver.close()
getDriver().close()
}

/**
Expand All @@ -687,7 +690,7 @@ class Browser {
* @see org.openqa.selenium.WebDriver#getWindowHandle()
*/
String getCurrentWindow() {
driver.windowHandle
getDriver().windowHandle
}

/**
Expand All @@ -696,7 +699,7 @@ class Browser {
* @see org.openqa.selenium.WebDriver#getWindowHandles()
*/
Set<String> getAvailableWindows() {
driver.windowHandles
getDriver().windowHandles
}

/**
Expand Down Expand Up @@ -812,7 +815,7 @@ class Browser {
cloned.call()
} finally {
if ((!options.containsKey(CLOSE_OPTION) && config.withNewWindowConfig.close.orElse(true)) || options.close) {
driver.close()
getDriver().close()
}
switchToWindow(originalWindow)
page originalPage
Expand Down Expand Up @@ -996,8 +999,8 @@ class Browser {
}

public <T> Optional<T> driverAs(Class<T> castType) {
if (castType.isInstance(driver)) {
Optional.of(driver as T)
if (castType.isInstance(getDriver())) {
Optional.of(getDriver() as T)
} else if (castType.isInstance(augmentedDriver)) {
Optional.of(augmentedDriver as T)
} else {
Expand All @@ -1006,7 +1009,7 @@ class Browser {
}

protected switchToWindow(String window) {
driver.switchTo().window(window)
getDriver().switchTo().window(window)
}

protected <T> T doWithWindow(Map options, Closure<T> block) {
Expand All @@ -1021,7 +1024,7 @@ class Browser {
cloned.call()
} finally {
if (options.close || (!options.containsKey(CLOSE_OPTION) && config.withWindowConfig.close)) {
driver.close()
getDriver().close()
}
}
}
Expand All @@ -1035,6 +1038,15 @@ class Browser {
config.createNavigatorFactory(this)
}

/**
* Called to create the driver, the first time it is requested.
*
* @return The driver
*/
protected WebDriver createDriver() {
config.createDriver()
}

private WebDriver getDriverWithNetworkConditions() {
optionalHasNetworkConditionsClass.map { hasNetworkConditionsClass ->
driverAs(hasNetworkConditionsClass).orElse(null)
Expand Down Expand Up @@ -1230,7 +1242,7 @@ class Browser {
private URI retrieveCurrentUri() {
def currentUri = null
try {
def currentUrl = driver.currentUrl
def currentUrl = getDriver().currentUrl
currentUri = currentUrl ? new URI(currentUrl) : null
} catch (NullPointerException npe) {
} catch (URISyntaxException use) {
Expand Down
35 changes: 20 additions & 15 deletions module/geb-core/src/main/groovy/geb/Configuration.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,6 @@ class Configuration {
final Properties properties
final BuildAdapter buildAdapter

private WebDriver driver

Configuration(Map rawConfig) {
this(toConfigObject(rawConfig), null, null, null)
}
Expand Down Expand Up @@ -256,7 +254,7 @@ class Configuration {
* This may be the class name of a driver implementation, a driver short name or a closure
* that when invoked with no arguments returns a driver implementation.
*
* @see #getDriver()
* @see #createDriver()
*/
void setDriverConf(value) {
rawConfig.driver = value
Expand All @@ -268,7 +266,7 @@ class Configuration {
* This may be the class name of a driver implementation, a short name, or a closure
* that when invoked returns an actual driver.
*
* @see #getDriver()
* @see #createDriver()
*/
def getDriverConf() {
def value = properties.getProperty("geb.driver") ?: readValue("driver", null)
Expand Down Expand Up @@ -376,16 +374,27 @@ class Configuration {
rawConfig.pageEventListener = pageEventListener
}

WebDriver getDriver() {
if (driver == null) {
driver = createDriver()
}
WebDriver createDriver() {
wrapDriverFactoryInCachingIfNeeded(getDriverFactory(getDriverConf())).driver
}

driver
/**
* @deprecated As of 8.0, replaced by {@link #createDriver()}, the configuration does
* no longer carry a driver instance.
*/
@Deprecated
WebDriver getDriver() {
createDriver()
}

void setDriver(WebDriver driver) {
this.driver = driver
/**
* @deprecated As of 8.0, the configuration does no longer carry a driver
* instance, but only create new driver instances, the driver
* instance used is stored in the browser instance.
*/
@Deprecated
void setDriver(WebDriver ignored) {
// does nothing anymore
}

/**
Expand Down Expand Up @@ -700,10 +709,6 @@ class Configuration {
throw new InvalidGebConfiguration("Configuration for bounds and 'required' template options is conflicting")
}

protected WebDriver createDriver() {
wrapDriverFactoryInCachingIfNeeded(getDriverFactory(getDriverConf())).driver
}

protected DriverFactory getDriverFactory(driverValue) {
switch (driverValue) {
case CharSequence:
Expand Down
2 changes: 1 addition & 1 deletion module/geb-core/src/test/groovy/geb/PauseSpec.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ class PauseSpec extends GebSpecWithCallbackServer {
@InheritConstructors
private static class ThreadSafeScriptExecutionDriverConfiguration extends Configuration {
@Override
protected WebDriver createDriver() {
WebDriver createDriver() {
def driver = super.createDriver()
driver.javascriptEnabled = true
ThreadSafeScriptExecutionDriver.of(driver)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ class ConfigurationDriverCreationSpec extends Specification {
def config = new ConfigObject()
config.cacheDriver = false
config.driver = { new HtmlUnitDriver() }
d = new Configuration(config, p()).driver
d = new Configuration(config, p()).createDriver()

then:
d instanceof HtmlUnitDriver
Expand All @@ -182,7 +182,7 @@ class ConfigurationDriverCreationSpec extends Specification {
config.driver = { 'not a driver' }

when:
new Configuration(config).driver
new Configuration(config).createDriver()

then:
thrown(DriverCreationException)
Expand Down
Loading
Loading