|
15 | 15 | */ |
16 | 16 | package io.github.seleniumquery.browser.driver; |
17 | 17 |
|
| 18 | +import java.util.function.Consumer; |
| 19 | + |
| 20 | +import org.apache.commons.logging.Log; |
| 21 | +import org.apache.commons.logging.LogFactory; |
18 | 22 | import org.openqa.selenium.WebDriver; |
19 | 23 | import org.openqa.selenium.remote.DesiredCapabilities; |
20 | 24 |
|
| 25 | +import io.github.bonigarcia.wdm.BrowserManager; |
| 26 | +import io.github.bonigarcia.wdm.WebDriverManager; |
| 27 | + |
21 | 28 | /** |
22 | 29 | * Builds {@link WebDriver} instances for SeleniumQueryDriver. |
23 | 30 | * |
|
26 | 33 | */ |
27 | 34 | public abstract class DriverBuilder<T extends DriverBuilder<T>> { |
28 | 35 |
|
| 36 | + private static final Log LOGGER = LogFactory.getLog(DriverBuilder.class); |
| 37 | + |
29 | 38 | private DesiredCapabilities desiredCapabilities; |
30 | 39 |
|
31 | 40 | private boolean capabilitiesManuallySet = false; |
32 | 41 |
|
| 42 | + private Consumer<BrowserManager> autoDriverDownloadConfigurer; |
| 43 | + |
33 | 44 | @SuppressWarnings("unchecked") |
34 | 45 | public T withCapabilities(DesiredCapabilities desiredCapabilities) { |
35 | 46 | markCapabilitiesWereSet(); |
@@ -70,4 +81,38 @@ protected boolean isCapabilitiesManuallySet() { |
70 | 81 | */ |
71 | 82 | protected abstract WebDriver build(); |
72 | 83 |
|
| 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 | + |
73 | 118 | } |
0 commit comments