@@ -60,6 +60,7 @@ class Browser {
60
60
61
61
private Page page
62
62
private final Configuration config
63
+ private WebDriver driver
63
64
private final RemoteDriverOperations remoteDriverOperations = new RemoteDriverOperations (this . class. classLoader)
64
65
private String reportGroup = null
65
66
private NavigatorFactory navigatorFactory = null
@@ -68,7 +69,7 @@ class Browser {
68
69
* If the driver is remote, this object allows access to its capabilities (users of Geb should not access this object, it is used internally).
69
70
*/
70
71
@Lazy
71
- WebDriver augmentedDriver = remoteDriverOperations. getAugmentedDriver(driver )
72
+ WebDriver augmentedDriver = remoteDriverOperations. getAugmentedDriver(getDriver() )
72
73
73
74
/**
74
75
* Create a new browser with a default configuration loader, loading the default configuration file.
@@ -164,10 +165,14 @@ class Browser {
164
165
* <p >
165
166
* The driver implementation to use is determined by the configuration.
166
167
*
167
- * @see geb.Configuration#getDriver ()
168
+ * @see geb.Configuration#createDriver ()
168
169
*/
169
170
WebDriver getDriver () {
170
- config. driver
171
+ if (driver == null ) {
172
+ driver = createDriver()
173
+ }
174
+
175
+ driver
171
176
}
172
177
173
178
/**
@@ -189,11 +194,9 @@ class Browser {
189
194
* This should only be called before making any requests as a means to override the driver instance
190
195
* that would be created from the configuration. Where possible, prefer using the configuration mechanism
191
196
* to specify the driver implementation to use.
192
- * <p >
193
- * This method delegates to {@link geb.Configuration#setDriver(org.openqa.selenium.WebDriver)}.
194
197
*/
195
198
void setDriver (WebDriver driver ) {
196
- config . driver = driver
199
+ this . driver = driver
197
200
}
198
201
199
202
/**
@@ -223,7 +226,7 @@ class Browser {
223
226
* @see org.openqa.selenium.WebDriver#getCurrentUrl()
224
227
*/
225
228
String getCurrentUrl () {
226
- driver . currentUrl
229
+ getDriver() . currentUrl
227
230
}
228
231
229
232
/**
@@ -509,9 +512,9 @@ class Browser {
509
512
void go (Map params = [:], String url = null , UrlFragment fragment = null ) {
510
513
def newUri = calculateUri(url, params, fragment)
511
514
def currentUri = retrieveCurrentUri()
512
- driver . get(newUri. toString())
515
+ getDriver() . get(newUri. toString())
513
516
if (sameUrlWithDifferentFragment(currentUri, newUri)) {
514
- driver . navigate(). refresh()
517
+ getDriver() . navigate(). refresh()
515
518
}
516
519
if (! page) {
517
520
page(Page )
@@ -630,7 +633,7 @@ class Browser {
630
633
* @see org.openqa.selenium.WebDriver.Options#deleteAllCookies()
631
634
*/
632
635
void clearCookies () {
633
- driver ?. manage()?. deleteAllCookies()
636
+ getDriver() ?. manage()?. deleteAllCookies()
634
637
}
635
638
636
639
/**
@@ -669,7 +672,7 @@ class Browser {
669
672
* @see org.openqa.selenium.WebDriver#quit()
670
673
*/
671
674
void quit () {
672
- driver . quit()
675
+ getDriver() . quit()
673
676
}
674
677
675
678
/**
@@ -678,7 +681,7 @@ class Browser {
678
681
* @see org.openqa.selenium.WebDriver#close()
679
682
*/
680
683
void close () {
681
- driver . close()
684
+ getDriver() . close()
682
685
}
683
686
684
687
/**
@@ -687,7 +690,7 @@ class Browser {
687
690
* @see org.openqa.selenium.WebDriver#getWindowHandle()
688
691
*/
689
692
String getCurrentWindow () {
690
- driver . windowHandle
693
+ getDriver() . windowHandle
691
694
}
692
695
693
696
/**
@@ -696,7 +699,7 @@ class Browser {
696
699
* @see org.openqa.selenium.WebDriver#getWindowHandles()
697
700
*/
698
701
Set<String > getAvailableWindows () {
699
- driver . windowHandles
702
+ getDriver() . windowHandles
700
703
}
701
704
702
705
/**
@@ -812,7 +815,7 @@ class Browser {
812
815
cloned. call()
813
816
} finally {
814
817
if ((! options. containsKey(CLOSE_OPTION ) && config. withNewWindowConfig. close. orElse(true )) || options. close) {
815
- driver . close()
818
+ getDriver() . close()
816
819
}
817
820
switchToWindow(originalWindow)
818
821
page originalPage
@@ -996,8 +999,8 @@ class Browser {
996
999
}
997
1000
998
1001
public <T> Optional<T> driverAs (Class<T> castType ) {
999
- if (castType. isInstance(driver )) {
1000
- Optional . of(driver as T)
1002
+ if (castType. isInstance(getDriver() )) {
1003
+ Optional . of(getDriver() as T)
1001
1004
} else if (castType. isInstance(augmentedDriver)) {
1002
1005
Optional . of(augmentedDriver as T)
1003
1006
} else {
@@ -1006,7 +1009,7 @@ class Browser {
1006
1009
}
1007
1010
1008
1011
protected switchToWindow (String window ) {
1009
- driver . switchTo(). window(window)
1012
+ getDriver() . switchTo(). window(window)
1010
1013
}
1011
1014
1012
1015
protected <T> T doWithWindow (Map options , Closure<T> block ) {
@@ -1021,7 +1024,7 @@ class Browser {
1021
1024
cloned. call()
1022
1025
} finally {
1023
1026
if (options. close || (! options. containsKey(CLOSE_OPTION ) && config. withWindowConfig. close)) {
1024
- driver . close()
1027
+ getDriver() . close()
1025
1028
}
1026
1029
}
1027
1030
}
@@ -1035,6 +1038,15 @@ class Browser {
1035
1038
config. createNavigatorFactory(this )
1036
1039
}
1037
1040
1041
+ /**
1042
+ * Called to create the driver, the first time it is requested.
1043
+ *
1044
+ * @return The driver
1045
+ */
1046
+ protected WebDriver createDriver () {
1047
+ config. createDriver()
1048
+ }
1049
+
1038
1050
private WebDriver getDriverWithNetworkConditions () {
1039
1051
optionalHasNetworkConditionsClass. map { hasNetworkConditionsClass ->
1040
1052
driverAs(hasNetworkConditionsClass). orElse(null )
@@ -1230,7 +1242,7 @@ class Browser {
1230
1242
private URI retrieveCurrentUri () {
1231
1243
def currentUri = null
1232
1244
try {
1233
- def currentUrl = driver . currentUrl
1245
+ def currentUrl = getDriver() . currentUrl
1234
1246
currentUri = currentUrl ? new URI (currentUrl) : null
1235
1247
} catch (NullPointerException npe) {
1236
1248
} catch (URISyntaxException use) {
0 commit comments