Skip to content

Commit 8b4ac82

Browse files
committed
refactor(driverbuilder): pull up autoDriverDownload()
1 parent 35fd13f commit 8b4ac82

2 files changed

Lines changed: 45 additions & 35 deletions

File tree

src/main/java/io/github/seleniumquery/browser/driver/DriverBuilder.java

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,16 @@
1515
*/
1616
package io.github.seleniumquery.browser.driver;
1717

18+
import java.util.function.Consumer;
19+
20+
import org.apache.commons.logging.Log;
21+
import org.apache.commons.logging.LogFactory;
1822
import org.openqa.selenium.WebDriver;
1923
import org.openqa.selenium.remote.DesiredCapabilities;
2024

25+
import io.github.bonigarcia.wdm.BrowserManager;
26+
import io.github.bonigarcia.wdm.WebDriverManager;
27+
2128
/**
2229
* Builds {@link WebDriver} instances for SeleniumQueryDriver.
2330
*
@@ -26,10 +33,14 @@
2633
*/
2734
public abstract class DriverBuilder<T extends DriverBuilder<T>> {
2835

36+
private static final Log LOGGER = LogFactory.getLog(DriverBuilder.class);
37+
2938
private DesiredCapabilities desiredCapabilities;
3039

3140
private boolean capabilitiesManuallySet = false;
3241

42+
private Consumer<BrowserManager> autoDriverDownloadConfigurer;
43+
3344
@SuppressWarnings("unchecked")
3445
public T withCapabilities(DesiredCapabilities desiredCapabilities) {
3546
markCapabilitiesWereSet();
@@ -70,4 +81,38 @@ protected boolean isCapabilitiesManuallySet() {
7081
*/
7182
protected abstract WebDriver build();
7283

84+
protected void autoDownloadDriverIfAskedFor(Class<? extends WebDriver> driverClass) {
85+
if (this.autoDriverDownloadConfigurer != null) {
86+
BrowserManager browserManager = WebDriverManager.getInstance(driverClass);
87+
this.autoDriverDownloadConfigurer.accept(browserManager);
88+
browserManager.setup();
89+
}
90+
}
91+
92+
/**
93+
* Automatically downloads and configures the chromedriver.exe executable using
94+
* <a href="https://github.com/bonigarcia/webdrivermanager">webdrivermanager</a>.
95+
* @return A self reference, allowing further configuration of the driver builder.
96+
* @since 0.18.0
97+
*/
98+
public T autoDriverDownload() {
99+
return this.autoDriverDownload(x -> {});
100+
}
101+
102+
/**
103+
* Automatically downloads and configures the chromedriver.exe executable using
104+
* <a href="https://github.com/bonigarcia/webdrivermanager">webdrivermanager</a>.
105+
* @param configurer A function that allows <a href="https://github.com/bonigarcia/webdrivermanager#webdrivermanager-api">additional configuration</a> of the {@link BrowserManager}.
106+
* @return A self reference, allowing further configuration of the driver builder.
107+
* @since 0.18.0
108+
*/
109+
@SuppressWarnings("unchecked")
110+
public T autoDriverDownload(Consumer<BrowserManager> configurer) {
111+
if (this.autoDriverDownloadConfigurer != null) {
112+
LOGGER.warn(".autoDriverDownload() has already been called. Ignoring all calls but the last.");
113+
}
114+
this.autoDriverDownloadConfigurer = configurer;
115+
return (T) this;
116+
}
117+
73118
}

src/main/java/io/github/seleniumquery/browser/driver/builders/ChromeDriverBuilder.java

Lines changed: 0 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -151,14 +151,6 @@ protected WebDriver build() {
151151
}
152152
}
153153

154-
private void autoDownloadDriverIfAskedFor(Class<? extends WebDriver> driverClass) {
155-
if (this.autoDriverDownloadConfigurer != null) {
156-
BrowserManager browserManager = WebDriverManager.getInstance(driverClass);
157-
this.autoDriverDownloadConfigurer.accept(browserManager);
158-
browserManager.setup();
159-
}
160-
}
161-
162154
private WebDriver buildUsingCapabilities() {
163155
DesiredCapabilities capabilities = capabilities(DesiredCapabilities.chrome());
164156
overwriteCapabilityIfValueNotNull(capabilities, ChromeOptions.CAPABILITY, this.chromeOptions);
@@ -214,31 +206,4 @@ private void throwCustomExceptionIfExecutableWasNotFound(IllegalStateException e
214206
}
215207
}
216208

217-
private Consumer<BrowserManager> autoDriverDownloadConfigurer;
218-
219-
/**
220-
* Automatically downloads and configures the chromedriver.exe executable using
221-
* <a href="https://github.com/bonigarcia/webdrivermanager">webdrivermanager</a>.
222-
* @return A self reference, allowing further configuration of the driver builder.
223-
* @since 0.18.0
224-
*/
225-
public ChromeDriverBuilder autoDriverDownload() {
226-
return this.autoDriverDownload(x -> {});
227-
}
228-
229-
/**
230-
* Automatically downloads and configures the chromedriver.exe executable using
231-
* <a href="https://github.com/bonigarcia/webdrivermanager">webdrivermanager</a>.
232-
* @param configurer A function that allows <a href="https://github.com/bonigarcia/webdrivermanager#webdrivermanager-api">additional configuration</a> of the {@link BrowserManager}.
233-
* @return A self reference, allowing further configuration of the driver builder.
234-
* @since 0.18.0
235-
*/
236-
public ChromeDriverBuilder autoDriverDownload(Consumer<BrowserManager> configurer) {
237-
if (this.autoDriverDownloadConfigurer != null) {
238-
LOGGER.warn(".autoDriverDownload() has already been called. Ignoring all calls but the last.");
239-
}
240-
this.autoDriverDownloadConfigurer = configurer;
241-
return this;
242-
}
243-
244209
}

0 commit comments

Comments
 (0)