Skip to content

Commit e3257d6

Browse files
committed
[java] implement builder() for all drivers to allow the correct default options instance
1 parent e50a1ab commit e3257d6

File tree

10 files changed

+122
-4
lines changed

10 files changed

+122
-4
lines changed

java/src/org/openqa/selenium/chrome/ChromeDriver.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,14 @@
1818
package org.openqa.selenium.chrome;
1919

2020
import com.google.common.collect.ImmutableMap;
21+
import org.openqa.selenium.Beta;
2122
import org.openqa.selenium.Capabilities;
2223
import org.openqa.selenium.WebDriver;
2324
import org.openqa.selenium.chromium.ChromiumDriver;
2425
import org.openqa.selenium.chromium.ChromiumDriverCommandExecutor;
2526
import org.openqa.selenium.remote.CommandInfo;
2627
import org.openqa.selenium.remote.RemoteWebDriver;
28+
import org.openqa.selenium.remote.RemoteWebDriverBuilder;
2729
import org.openqa.selenium.remote.service.DriverService;
2830

2931
import java.util.Map;
@@ -106,6 +108,11 @@ public ChromeDriver(ChromeDriverService service, Capabilities capabilities) {
106108
cdp = new AddHasCdp().getImplementation(getCapabilities(), getExecuteMethod());
107109
}
108110

111+
@Beta
112+
public static RemoteWebDriverBuilder builder() {
113+
return RemoteWebDriver.builder().oneOf(new ChromeOptions());
114+
}
115+
109116
private static class ChromeDriverCommandExecutor extends ChromiumDriverCommandExecutor {
110117
public ChromeDriverCommandExecutor(DriverService service) {
111118
super(service, getExtraCommands());

java/src/org/openqa/selenium/edge/EdgeDriver.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,15 @@
1717
package org.openqa.selenium.edge;
1818

1919
import com.google.common.collect.ImmutableMap;
20+
import org.openqa.selenium.Beta;
2021
import org.openqa.selenium.Capabilities;
2122
import org.openqa.selenium.WebDriver;
2223
import org.openqa.selenium.chromium.ChromiumDriver;
2324
import org.openqa.selenium.chromium.ChromiumDriverCommandExecutor;
2425
import org.openqa.selenium.internal.Require;
2526
import org.openqa.selenium.remote.CommandInfo;
27+
import org.openqa.selenium.remote.RemoteWebDriver;
28+
import org.openqa.selenium.remote.RemoteWebDriverBuilder;
2629
import org.openqa.selenium.remote.service.DriverService;
2730

2831
import java.util.Map;
@@ -56,6 +59,11 @@ public EdgeDriver(Capabilities capabilities) {
5659
this(new EdgeDriverService.Builder().build(), new EdgeOptions().merge(capabilities));
5760
}
5861

62+
@Beta
63+
public static RemoteWebDriverBuilder builder() {
64+
return RemoteWebDriver.builder().oneOf(new EdgeOptions());
65+
}
66+
5967
private static class EdgeDriverCommandExecutor extends ChromiumDriverCommandExecutor {
6068
public EdgeDriverCommandExecutor(DriverService service) {
6169
super(service, getExtraCommands());

java/src/org/openqa/selenium/firefox/FirefoxDriver.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import com.google.common.collect.ImmutableMap;
2121
import com.google.common.collect.Maps;
2222
import com.google.common.collect.Sets;
23+
import org.openqa.selenium.Beta;
2324
import org.openqa.selenium.Capabilities;
2425
import org.openqa.selenium.ImmutableCapabilities;
2526
import org.openqa.selenium.MutableCapabilities;
@@ -42,6 +43,7 @@
4243
import org.openqa.selenium.remote.CommandInfo;
4344
import org.openqa.selenium.remote.FileDetector;
4445
import org.openqa.selenium.remote.RemoteWebDriver;
46+
import org.openqa.selenium.remote.RemoteWebDriverBuilder;
4547
import org.openqa.selenium.remote.html5.RemoteWebStorage;
4648
import org.openqa.selenium.remote.http.ClientConfig;
4749
import org.openqa.selenium.remote.http.HttpClient;
@@ -137,6 +139,11 @@ public static final class Capability {
137139
public static final String MARIONETTE = "marionette";
138140
}
139141

142+
@Beta
143+
public static RemoteWebDriverBuilder builder() {
144+
return RemoteWebDriver.builder().oneOf(new FirefoxOptions());
145+
}
146+
140147
private static class FirefoxDriverCommandExecutor extends DriverCommandExecutor {
141148
public FirefoxDriverCommandExecutor(DriverService service) {
142149
super(service, getExtraCommands());

java/src/org/openqa/selenium/ie/InternetExplorerDriver.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,14 @@
1717

1818
package org.openqa.selenium.ie;
1919

20+
import org.openqa.selenium.Beta;
2021
import org.openqa.selenium.Capabilities;
2122
import org.openqa.selenium.Platform;
2223
import org.openqa.selenium.WebDriverException;
2324
import org.openqa.selenium.remote.CapabilityType;
2425
import org.openqa.selenium.remote.FileDetector;
2526
import org.openqa.selenium.remote.RemoteWebDriver;
27+
import org.openqa.selenium.remote.RemoteWebDriverBuilder;
2628
import org.openqa.selenium.remote.service.DriverCommandExecutor;
2729

2830
import java.io.File;
@@ -193,6 +195,11 @@ public void setFileDetector(FileDetector detector) {
193195
"via RemoteWebDriver");
194196
}
195197

198+
@Beta
199+
public static RemoteWebDriverBuilder builder() {
200+
return RemoteWebDriver.builder().oneOf(new InternetExplorerOptions());
201+
}
202+
196203
protected void assertOnWindows() {
197204
Platform current = Platform.getCurrent();
198205
if (!current.is(Platform.WINDOWS)) {

java/src/org/openqa/selenium/safari/SafariDriver.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,14 @@
1818
package org.openqa.selenium.safari;
1919

2020
import com.google.common.collect.ImmutableMap;
21+
import org.openqa.selenium.Beta;
2122
import org.openqa.selenium.Capabilities;
2223
import org.openqa.selenium.WebDriverException;
2324
import org.openqa.selenium.internal.Require;
2425
import org.openqa.selenium.remote.CommandInfo;
2526
import org.openqa.selenium.remote.FileDetector;
2627
import org.openqa.selenium.remote.RemoteWebDriver;
28+
import org.openqa.selenium.remote.RemoteWebDriverBuilder;
2729
import org.openqa.selenium.remote.service.DriverCommandExecutor;
2830
import org.openqa.selenium.remote.service.DriverService;
2931

@@ -126,4 +128,9 @@ public void setFileDetector(FileDetector detector) {
126128
"Setting the file detector only works on remote webdriver instances obtained " +
127129
"via RemoteWebDriver");
128130
}
131+
132+
@Beta
133+
public static RemoteWebDriverBuilder builder() {
134+
return RemoteWebDriver.builder().oneOf(new SafariOptions());
135+
}
129136
}

java/test/org/openqa/selenium/chrome/ChromeDriverFunctionalTest.java

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,6 @@
1717

1818
package org.openqa.selenium.chrome;
1919

20-
import static org.assertj.core.api.Assertions.assertThat;
21-
import static org.assertj.core.api.Assertions.fail;
22-
import static org.assertj.core.api.Assumptions.assumeThat;
23-
2420
import org.junit.Test;
2521
import org.openqa.selenium.JavascriptExecutor;
2622
import org.openqa.selenium.WebDriver;
@@ -38,11 +34,31 @@
3834
import java.util.List;
3935
import java.util.Map;
4036

37+
import static org.assertj.core.api.Assertions.assertThat;
38+
import static org.assertj.core.api.Assertions.fail;
39+
import static org.assertj.core.api.Assumptions.assumeThat;
40+
4141
public class ChromeDriverFunctionalTest extends JUnit4TestBase {
4242

4343
private final String CLIPBOARD_READ = "clipboard-read";
4444
private final String CLIPBOARD_WRITE = "clipboard-write";
4545

46+
@Test
47+
public void builderGeneratesDefaultChromeOptions() {
48+
WebDriver driver = ChromeDriver.builder().build();
49+
driver.quit();
50+
}
51+
52+
@Test
53+
public void builderOverridesDefaultChromeOptions() {
54+
ChromeOptions options = new ChromeOptions();
55+
options.setImplicitWaitTimeout(Duration.ofMillis(1));
56+
WebDriver driver = ChromeDriver.builder().oneOf(options).build();
57+
assertThat(driver.manage().timeouts().getImplicitWaitTimeout()).isEqualTo(Duration.ofMillis(1));
58+
59+
driver.quit();
60+
}
61+
4662
@Test
4763
public void canSetPermission() {
4864
HasPermissions permissions = (HasPermissions) driver;

java/test/org/openqa/selenium/edge/EdgeDriverFunctionalTest.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,22 @@ public class EdgeDriverFunctionalTest extends JUnit4TestBase {
4242
private final String CLIPBOARD_READ = "clipboard-read";
4343
private final String CLIPBOARD_WRITE = "clipboard-write";
4444

45+
@Test
46+
public void builderGeneratesDefaultChromeOptions() {
47+
WebDriver driver = EdgeDriver.builder().build();
48+
driver.quit();
49+
}
50+
51+
@Test
52+
public void builderOverridesDefaultChromeOptions() {
53+
EdgeOptions options = new EdgeOptions();
54+
options.setImplicitWaitTimeout(Duration.ofMillis(1));
55+
WebDriver driver = EdgeDriver.builder().oneOf(options).build();
56+
assertThat(driver.manage().timeouts().getImplicitWaitTimeout()).isEqualTo(Duration.ofMillis(1));
57+
58+
driver.quit();
59+
}
60+
4561
@Test
4662
public void canSetPermission() {
4763
HasPermissions permissions = (HasPermissions) driver;

java/test/org/openqa/selenium/firefox/FirefoxDriverTest.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,22 @@ public void quitDriver() {
9696
}
9797
}
9898

99+
@Test
100+
public void builderGeneratesDefaultChromeOptions() {
101+
WebDriver driver = FirefoxDriver.builder().build();
102+
driver.quit();
103+
}
104+
105+
@Test
106+
public void builderOverridesDefaultChromeOptions() {
107+
FirefoxOptions options = new FirefoxOptions();
108+
options.setImplicitWaitTimeout(Duration.ofMillis(1));
109+
WebDriver driver = FirefoxDriver.builder().oneOf(options).build();
110+
assertThat(driver.manage().timeouts().getImplicitWaitTimeout()).isEqualTo(Duration.ofMillis(1));
111+
112+
driver.quit();
113+
}
114+
99115
@Test
100116
public void canStartDriverWithNoParameters() {
101117
localDriver = new WebDriverBuilder().get();

java/test/org/openqa/selenium/ie/InternetExplorerDriverTest.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,29 @@
3030
import org.openqa.selenium.testing.drivers.WebDriverBuilder;
3131

3232
import java.awt.*;
33+
import java.time.Duration;
3334

3435
import static org.assertj.core.api.Assertions.assertThat;
3536
import static org.openqa.selenium.ie.InternetExplorerDriver.ENABLE_PERSISTENT_HOVERING;
3637

3738
public class InternetExplorerDriverTest extends JUnit4TestBase {
3839

40+
@Test
41+
public void builderGeneratesDefaultChromeOptions() {
42+
WebDriver driver = InternetExplorerDriver.builder().build();
43+
driver.quit();
44+
}
45+
46+
@Test
47+
public void builderOverridesDefaultChromeOptions() {
48+
InternetExplorerOptions options = new InternetExplorerOptions();
49+
options.setImplicitWaitTimeout(Duration.ofMillis(1));
50+
WebDriver driver = InternetExplorerDriver.builder().oneOf(options).build();
51+
assertThat(driver.manage().timeouts().getImplicitWaitTimeout()).isEqualTo(Duration.ofMillis(1));
52+
53+
driver.quit();
54+
}
55+
3956
@Test
4057
@NoDriverBeforeTest
4158
public void canRestartTheIeDriverInATightLoop() {

java/test/org/openqa/selenium/safari/SafariDriverTest.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import java.nio.file.Files;
2929
import java.nio.file.Path;
3030
import java.nio.file.Paths;
31+
import java.time.Duration;
3132

3233
import static org.assertj.core.api.Assertions.assertThat;
3334
import static org.junit.Assume.assumeTrue;
@@ -53,6 +54,22 @@ public void stopAll() {
5354
}
5455
}
5556

57+
@Test
58+
public void builderGeneratesDefaultChromeOptions() {
59+
WebDriver driver = SafariDriver.builder().build();
60+
driver.quit();
61+
}
62+
63+
@Test
64+
public void builderOverridesDefaultChromeOptions() {
65+
SafariOptions options = new SafariOptions();
66+
options.setImplicitWaitTimeout(Duration.ofMillis(1));
67+
WebDriver driver = SafariDriver.builder().oneOf(options).build();
68+
assertThat(driver.manage().timeouts().getImplicitWaitTimeout()).isEqualTo(Duration.ofMillis(1));
69+
70+
driver.quit();
71+
}
72+
5673
@Test
5774
public void canStartADriverUsingAService() throws IOException {
5875
removeDriver();

0 commit comments

Comments
 (0)