Skip to content

Commit 30ae31c

Browse files
code-with-abdullahtitusfortnerBilalKamranBilal Kamrandiemol
authored
[java] have overloaded constructors to have configurable connect and read timeouts while instantiating WebDriver object (#11532)
* re #11158 - Added overriden constructors to have configurable connect and read timeouts. * Added support of specifying read and connect timeout for IE and Edge drivers. * Added support to specify client config at the time of WebDriver's object instantiation. * Updated PR as requested by diemol & krmahadevan * Reverted formatting changes from RemoteWebDriver Constructors * Reverted changed from RemoteWebDriver. Refactored all constructors in InternetExplorerDriver. Removed invalid throws clause from ChromiumDriverCommandExecutor. * Removed remaining throws clauses from constructors. * reverted all the changes from RemoteWebDriver class * Optimized unused imports * Resolved build errors in PR * Updated DriverCommandExecutor to have 2 args constructor that enables us to pass client config * Added test cases for passing client config in webdriver constructors * Refactor test cases --------- Co-authored-by: Titus Fortner <[email protected]> Co-authored-by: Bilal Kamran <[email protected]> Co-authored-by: Bilal Kamran <“[email protected]”> Co-authored-by: Diego Molina <[email protected]>
1 parent 4fb0b0a commit 30ae31c

File tree

13 files changed

+171
-46
lines changed

13 files changed

+171
-46
lines changed

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

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import org.openqa.selenium.chromium.ChromiumDriverCommandExecutor;
2727
import org.openqa.selenium.internal.Require;
2828
import org.openqa.selenium.remote.CommandInfo;
29+
import org.openqa.selenium.remote.http.ClientConfig;
2930
import org.openqa.selenium.remote.RemoteWebDriver;
3031
import org.openqa.selenium.remote.RemoteWebDriverBuilder;
3132
import org.openqa.selenium.remote.service.DriverFinder;
@@ -80,19 +81,24 @@ public ChromeDriver(ChromeOptions options) {
8081
* @param options The options required from ChromeDriver.
8182
*/
8283
public ChromeDriver(ChromeDriverService service, ChromeOptions options) {
83-
super(generateExecutor(service, options), options, ChromeOptions.CAPABILITY);
84+
this(service, options, ClientConfig.defaultConfig());
85+
}
86+
87+
public ChromeDriver(ChromeDriverService service, ChromeOptions options, ClientConfig clientConfig) {
88+
super(generateExecutor(service, options, clientConfig), options, ChromeOptions.CAPABILITY);
8489
casting = new AddHasCasting().getImplementation(getCapabilities(), getExecuteMethod());
8590
cdp = new AddHasCdp().getImplementation(getCapabilities(), getExecuteMethod());
8691
}
8792

88-
private static ChromeDriverCommandExecutor generateExecutor(ChromeDriverService service, ChromeOptions options) {
93+
private static ChromeDriverCommandExecutor generateExecutor(ChromeDriverService service, ChromeOptions options, ClientConfig clientConfig) {
8994
Require.nonNull("Driver service", service);
9095
Require.nonNull("Driver options", options);
96+
Require.nonNull("Driver clientConfig", clientConfig);
9197
if (service.getExecutable() == null) {
9298
String path = DriverFinder.getPath(service, options);
9399
service.setExecutable(path);
94100
}
95-
return new ChromeDriverCommandExecutor(service);
101+
return new ChromeDriverCommandExecutor(service, clientConfig);
96102
}
97103

98104
@Beta
@@ -101,8 +107,8 @@ public static RemoteWebDriverBuilder builder() {
101107
}
102108

103109
private static class ChromeDriverCommandExecutor extends ChromiumDriverCommandExecutor {
104-
public ChromeDriverCommandExecutor(DriverService service) {
105-
super(service, getExtraCommands());
110+
public ChromeDriverCommandExecutor(DriverService service, ClientConfig clientConfig) {
111+
super(service, getExtraCommands(), clientConfig);
106112
}
107113

108114
private static Map<String, CommandInfo> getExtraCommands() {

java/src/org/openqa/selenium/chromium/ChromiumDriverCommandExecutor.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
import com.google.common.collect.ImmutableMap;
2121
import org.openqa.selenium.remote.CommandInfo;
22+
import org.openqa.selenium.remote.http.ClientConfig;
2223
import org.openqa.selenium.remote.service.DriverCommandExecutor;
2324
import org.openqa.selenium.remote.service.DriverService;
2425

@@ -32,7 +33,11 @@
3233
public class ChromiumDriverCommandExecutor extends DriverCommandExecutor {
3334

3435
public ChromiumDriverCommandExecutor(DriverService service, Map<String, CommandInfo> extraCommands) {
35-
super(service, getExtraCommands(extraCommands));
36+
this(service, extraCommands, ClientConfig.defaultConfig());
37+
}
38+
39+
public ChromiumDriverCommandExecutor(DriverService service, Map<String, CommandInfo> extraCommands, ClientConfig clientConfig) {
40+
super(service, getExtraCommands(extraCommands), clientConfig);
3641
}
3742

3843
private static Map<String, CommandInfo> getExtraCommands(Map<String, CommandInfo> commands) {

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

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import org.openqa.selenium.remote.CommandInfo;
2727
import org.openqa.selenium.remote.RemoteWebDriver;
2828
import org.openqa.selenium.remote.RemoteWebDriverBuilder;
29+
import org.openqa.selenium.remote.http.ClientConfig;
2930
import org.openqa.selenium.remote.service.DriverFinder;
3031
import org.openqa.selenium.remote.service.DriverService;
3132

@@ -52,19 +53,24 @@ public EdgeDriver(EdgeDriverService service) {
5253
}
5354

5455
public EdgeDriver(EdgeDriverService service, EdgeOptions options) {
55-
super(generateExecutor(service, options), options, EdgeOptions.CAPABILITY);
56+
this(service, options, ClientConfig.defaultConfig());
57+
}
58+
59+
public EdgeDriver(EdgeDriverService service, EdgeOptions options, ClientConfig clientConfig) {
60+
super(generateExecutor(service, options, clientConfig), options, EdgeOptions.CAPABILITY);
5661
casting = new AddHasCasting().getImplementation(getCapabilities(), getExecuteMethod());
5762
cdp = new AddHasCdp().getImplementation(getCapabilities(), getExecuteMethod());
5863
}
5964

60-
private static EdgeDriverCommandExecutor generateExecutor(EdgeDriverService service, EdgeOptions options) {
65+
private static EdgeDriverCommandExecutor generateExecutor(EdgeDriverService service, EdgeOptions options, ClientConfig clientConfig) {
6166
Require.nonNull("Driver service", service);
6267
Require.nonNull("Driver options", options);
68+
Require.nonNull("Driver clientConfig", clientConfig);
6369
if (service.getExecutable() == null) {
6470
String path = DriverFinder.getPath(service, options);
6571
service.setExecutable(path);
6672
}
67-
return new EdgeDriverCommandExecutor(service);
73+
return new EdgeDriverCommandExecutor(service, clientConfig);
6874
}
6975

7076
@Beta
@@ -73,8 +79,8 @@ public static RemoteWebDriverBuilder builder() {
7379
}
7480

7581
private static class EdgeDriverCommandExecutor extends ChromiumDriverCommandExecutor {
76-
public EdgeDriverCommandExecutor(DriverService service) {
77-
super(service, getExtraCommands());
82+
public EdgeDriverCommandExecutor(DriverService service, ClientConfig clientConfig) {
83+
super(service, getExtraCommands(), clientConfig);
7884
}
7985

8086
private static Map<String, CommandInfo> getExtraCommands() {

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

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -124,20 +124,29 @@ public FirefoxDriver(FirefoxDriverService service) {
124124
}
125125

126126
public FirefoxDriver(FirefoxDriverService service, FirefoxOptions options) {
127-
this(generateExecutor(service, options), options);
127+
this(service, options, ClientConfig.defaultConfig());
128128
}
129129

130-
private static FirefoxDriverCommandExecutor generateExecutor(FirefoxDriverService service, FirefoxOptions options) {
130+
public FirefoxDriver(FirefoxDriverService service, FirefoxOptions options, ClientConfig clientConfig) {
131+
this(generateExecutor(service, options, clientConfig), options);
132+
}
133+
134+
private static FirefoxDriverCommandExecutor generateExecutor(FirefoxDriverService service, FirefoxOptions options, ClientConfig clientConfig) {
131135
Require.nonNull("Driver service", service);
132136
Require.nonNull("Driver options", options);
137+
Require.nonNull("Driver clientConfig", clientConfig);
133138
if (service.getExecutable() == null) {
134139
String path = DriverFinder.getPath(service, options);
135140
service.setExecutable(path);
136141
}
137-
return new FirefoxDriverCommandExecutor(service);
142+
return new FirefoxDriverCommandExecutor(service, clientConfig);
138143
}
139144

140145
private FirefoxDriver(FirefoxDriverCommandExecutor executor, FirefoxOptions options) {
146+
this(executor, options, ClientConfig.defaultConfig());
147+
}
148+
149+
private FirefoxDriver(FirefoxDriverCommandExecutor executor, FirefoxOptions options, ClientConfig clientConfig) {
141150
super(executor, checkCapabilitiesAndProxy(options));
142151
webStorage = new RemoteWebStorage(getExecuteMethod());
143152
extensions = new AddHasExtensions().getImplementation(getCapabilities(), getExecuteMethod());
@@ -162,10 +171,10 @@ private FirefoxDriver(FirefoxDriverCommandExecutor executor, FirefoxOptions opti
162171

163172
this.cdpUri = cdpUri;
164173
this.capabilities = cdpUri.map(uri ->
165-
new ImmutableCapabilities(
166-
new PersistentCapabilities(capabilities)
167-
.setCapability("se:cdp", uri.toString())
168-
.setCapability("se:cdpVersion", "85.0")))
174+
new ImmutableCapabilities(
175+
new PersistentCapabilities(capabilities)
176+
.setCapability("se:cdp", uri.toString())
177+
.setCapability("se:cdpVersion", "85.0")))
169178
.orElse(new ImmutableCapabilities(capabilities));
170179
}
171180

@@ -202,7 +211,7 @@ public Capabilities getCapabilities() {
202211
public void setFileDetector(FileDetector detector) {
203212
throw new WebDriverException(
204213
"Setting the file detector only works on remote webdriver instances obtained " +
205-
"via RemoteWebDriver");
214+
"via RemoteWebDriver");
206215
}
207216

208217
@Override
@@ -365,7 +374,11 @@ public static final class SystemProperty {
365374
private static class FirefoxDriverCommandExecutor extends DriverCommandExecutor {
366375

367376
public FirefoxDriverCommandExecutor(DriverService service) {
368-
super(service, getExtraCommands());
377+
this(service, ClientConfig.defaultConfig());
378+
}
379+
380+
public FirefoxDriverCommandExecutor(DriverService service, ClientConfig clientConfig) {
381+
super(service, getExtraCommands(), clientConfig);
369382
}
370383

371384
private static Map<String, CommandInfo> getExtraCommands() {

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

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import org.openqa.selenium.remote.FileDetector;
2626
import org.openqa.selenium.remote.RemoteWebDriver;
2727
import org.openqa.selenium.remote.RemoteWebDriverBuilder;
28+
import org.openqa.selenium.remote.http.ClientConfig;
2829
import org.openqa.selenium.remote.service.DriverCommandExecutor;
2930
import org.openqa.selenium.remote.service.DriverFinder;
3031

@@ -77,7 +78,7 @@ public class InternetExplorerDriver extends RemoteWebDriver {
7778
* Setting this capability will make your tests unstable and hard to debug.
7879
*/
7980
public static final String INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS =
80-
"ignoreProtectedModeSettings";
81+
"ignoreProtectedModeSettings";
8182

8283
/**
8384
* Capability that defines to use persistent hovering or not.
@@ -111,15 +112,19 @@ public class InternetExplorerDriver extends RemoteWebDriver {
111112
public static final String IE_SWITCHES = "ie.browserCommandLineSwitches";
112113

113114
public InternetExplorerDriver() {
114-
this(null, null);
115+
this(InternetExplorerDriverService.createDefaultService(), new InternetExplorerOptions(), ClientConfig.defaultConfig());
115116
}
116117

117118
public InternetExplorerDriver(InternetExplorerOptions options) {
118-
this(null, options);
119+
this(InternetExplorerDriverService.createDefaultService(), options, ClientConfig.defaultConfig());
119120
}
120121

121122
public InternetExplorerDriver(InternetExplorerDriverService service) {
122-
this(service, null);
123+
this(service, new InternetExplorerOptions(), ClientConfig.defaultConfig());
124+
}
125+
126+
public InternetExplorerDriver(InternetExplorerDriverService service, InternetExplorerOptions options) {
127+
this(service, options, ClientConfig.defaultConfig());
123128
}
124129

125130
/**
@@ -131,48 +136,51 @@ public InternetExplorerDriver(InternetExplorerDriverService service) {
131136
* @param options The options required from InternetExplorerDriver.
132137
*/
133138
public InternetExplorerDriver(InternetExplorerDriverService service,
134-
InternetExplorerOptions options) {
139+
InternetExplorerOptions options,
140+
ClientConfig clientConfig) {
135141
if (options == null) {
136142
options = new InternetExplorerOptions();
137143
}
138144
if (service == null) {
139145
service = InternetExplorerDriverService.createDefaultService();
140146
}
141-
142147
if (service.getExecutable() == null) {
143148
String path = DriverFinder.getPath(service, options);
144149
service.setExecutable(path);
145150
}
151+
if (clientConfig == null){
152+
clientConfig = ClientConfig.defaultConfig();
153+
}
146154

147-
run(service, options);
155+
run(service, options, clientConfig);
148156
}
149157

150158
@Beta
151159
public static RemoteWebDriverBuilder builder() {
152160
return RemoteWebDriver.builder().oneOf(new InternetExplorerOptions());
153161
}
154162

155-
private void run(InternetExplorerDriverService service, Capabilities capabilities) {
163+
private void run(InternetExplorerDriverService service, Capabilities capabilities, ClientConfig clientConfig) {
156164
assertOnWindows();
157165

158-
setCommandExecutor(new DriverCommandExecutor(service));
166+
setCommandExecutor(new DriverCommandExecutor(service, clientConfig));
159167

160168
startSession(capabilities);
161169
}
162170

163171
@Override
164172
public void setFileDetector(FileDetector detector) {
165173
throw new WebDriverException(
166-
"Setting the file detector only works on remote webdriver instances obtained " +
174+
"Setting the file detector only works on remote webdriver instances obtained " +
167175
"via RemoteWebDriver");
168176
}
169177

170178
protected void assertOnWindows() {
171179
Platform current = Platform.getCurrent();
172180
if (!current.is(Platform.WINDOWS)) {
173181
throw new WebDriverException(
174-
String.format(
175-
"You appear to be running %s. The IE driver only runs on Windows.", current));
182+
String.format(
183+
"You appear to be running %s. The IE driver only runs on Windows.", current));
176184
}
177185
}
178186
}

java/src/org/openqa/selenium/remote/HttpCommandExecutor.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,15 @@ public HttpCommandExecutor(
8787
getDefaultClientFactory());
8888
}
8989

90+
public HttpCommandExecutor(
91+
Map<String, CommandInfo> additionalCommands,
92+
URL addressOfRemoteServer,
93+
ClientConfig config) {
94+
this(additionalCommands,
95+
config.baseUrl(Require.nonNull("Server URL", addressOfRemoteServer)),
96+
getDefaultClientFactory());
97+
}
98+
9099
public HttpCommandExecutor(
91100
Map<String, CommandInfo> additionalCommands,
92101
URL addressOfRemoteServer,

java/src/org/openqa/selenium/remote/service/DriverCommandExecutor.java

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,13 @@
2929
import org.openqa.selenium.remote.DriverCommand;
3030
import org.openqa.selenium.remote.HttpCommandExecutor;
3131
import org.openqa.selenium.remote.Response;
32+
import org.openqa.selenium.remote.http.ClientConfig;
3233

3334
import java.io.Closeable;
3435
import java.io.IOException;
3536
import java.net.ConnectException;
37+
import java.net.URISyntaxException;
38+
import java.util.Collections;
3639
import java.util.Map;
3740
import java.util.concurrent.CompletableFuture;
3841
import java.util.concurrent.ExecutionException;
@@ -64,8 +67,11 @@ public class DriverCommandExecutor extends HttpCommandExecutor implements Closea
6467
* @param service The DriverService to send commands to.
6568
*/
6669
public DriverCommandExecutor(DriverService service) {
67-
super(Require.nonNull("DriverService", service.getUrl()));
68-
this.service = service;
70+
this(service, ClientConfig.defaultConfig());
71+
}
72+
73+
public DriverCommandExecutor(DriverService service, ClientConfig clientConfig) {
74+
this(service, Collections.emptyMap(), clientConfig);
6975
}
7076

7177
/**
@@ -74,13 +80,23 @@ public DriverCommandExecutor(DriverService service) {
7480
*
7581
* @param service driver server
7682
* @param additionalCommands additional commands the remote end can process
83+
* @param clientConfig
7784
*/
78-
protected DriverCommandExecutor(
79-
DriverService service, Map<String, CommandInfo> additionalCommands) {
80-
super(additionalCommands, service.getUrl());
85+
86+
public DriverCommandExecutor(
87+
DriverService service, Map<String, CommandInfo> additionalCommands, ClientConfig clientConfig) {
88+
super(additionalCommands, service.getUrl(), computeClientConfigWithBaseURI(clientConfig, service));
8189
this.service = service;
8290
}
8391

92+
private static ClientConfig computeClientConfigWithBaseURI(ClientConfig clientConfig, DriverService service) {
93+
try {
94+
return clientConfig.baseUri(service.getUrl().toURI());
95+
} catch (URISyntaxException e) {
96+
return clientConfig;
97+
}
98+
}
99+
84100
/**
85101
* Sends the {@code command} to the driver server for execution. The server will be started
86102
* if requesting a new session. Likewise, if terminating a session, the server will be shutdown

0 commit comments

Comments
 (0)