Skip to content

Commit 37e98d2

Browse files
committed
[java] Fix setting default server URL if it is not passed to RemoteWebDriver constructor
1 parent 8f19b1a commit 37e98d2

File tree

3 files changed

+28
-30
lines changed

3 files changed

+28
-30
lines changed

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

Lines changed: 11 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,8 @@
3333
import org.openqa.selenium.remote.http.HttpResponse;
3434

3535
import java.io.IOException;
36-
import java.net.MalformedURLException;
3736
import java.net.URL;
3837
import java.util.Map;
39-
import java.util.Optional;
4038

4139
import static java.util.Collections.emptyMap;
4240
import static org.openqa.selenium.json.Json.JSON_UTF_8;
@@ -58,20 +56,14 @@ public class HttpCommandExecutor implements CommandExecutor, NeedsLocalLogs {
5856

5957
private LocalLogs logs = LocalLogs.getNullLogger();
6058

61-
private static URL getDefaultServerURL() {
62-
try {
63-
return new URL(System.getProperty("webdriver.remote.server", "http://localhost:4444/"));
64-
} catch (MalformedURLException e) {
65-
throw new WebDriverException(e);
66-
}
67-
}
68-
6959
public HttpCommandExecutor(URL addressOfRemoteServer) {
70-
this(emptyMap(), addressOfRemoteServer);
60+
this(emptyMap(), Require.nonNull("Server URL", addressOfRemoteServer));
7161
}
7262

7363
public HttpCommandExecutor(ClientConfig config) {
74-
this(emptyMap(), config, defaultClientFactory);
64+
this(emptyMap(),
65+
Require.nonNull("HTTP client configuration", config),
66+
defaultClientFactory);
7567
}
7668

7769
/**
@@ -85,7 +77,9 @@ public HttpCommandExecutor(
8577
Map<String, CommandInfo> additionalCommands,
8678
URL addressOfRemoteServer)
8779
{
88-
this(additionalCommands, addressOfRemoteServer, defaultClientFactory);
80+
this(Require.nonNull("Additional commands", additionalCommands),
81+
Require.nonNull("Server URL", addressOfRemoteServer),
82+
defaultClientFactory);
8983
}
9084

9185
public HttpCommandExecutor(
@@ -95,8 +89,7 @@ public HttpCommandExecutor(
9589
{
9690
this(additionalCommands,
9791
ClientConfig.defaultConfig()
98-
.baseUrl(Optional.ofNullable(addressOfRemoteServer)
99-
.orElseGet(HttpCommandExecutor::getDefaultServerURL)),
92+
.baseUrl(Require.nonNull("Server URL", addressOfRemoteServer)),
10093
httpClientFactory);
10194
}
10295

@@ -105,12 +98,9 @@ public HttpCommandExecutor(
10598
ClientConfig config,
10699
HttpClient.Factory httpClientFactory)
107100
{
108-
if (config.baseUri() == null) {
109-
config = config.baseUrl(getDefaultServerURL());
110-
}
111-
remoteServer = config.baseUrl();
112-
this.additionalCommands = additionalCommands;
113-
this.httpClientFactory = httpClientFactory;
101+
remoteServer = Require.nonNull("HTTP client configuration", config).baseUrl();
102+
this.additionalCommands = Require.nonNull("Additional commands", additionalCommands);
103+
this.httpClientFactory = Require.nonNull("HTTP client factory", httpClientFactory);
114104
this.client = this.httpClientFactory.createClient(config);
115105
}
116106

java/client/src/org/openqa/selenium/remote/RemoteWebDriver.java

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@
6969
import org.openqa.selenium.virtualauthenticator.VirtualAuthenticator;
7070
import org.openqa.selenium.virtualauthenticator.VirtualAuthenticatorOptions;
7171

72+
import java.net.MalformedURLException;
7273
import java.net.URL;
7374
import java.time.Duration;
7475
import java.util.Base64;
@@ -134,12 +135,22 @@ protected RemoteWebDriver() {
134135
this.capabilities = init(new ImmutableCapabilities());
135136
}
136137

138+
private static URL getDefaultServerURL() {
139+
try {
140+
return new URL(System.getProperty("webdriver.remote.server", "http://localhost:4444/"));
141+
} catch (MalformedURLException e) {
142+
throw new WebDriverException(e);
143+
}
144+
}
145+
137146
public RemoteWebDriver(Capabilities capabilities) {
138-
this((URL) null, capabilities);
147+
this(getDefaultServerURL(),
148+
Require.nonNull("Capabilities", capabilities));
139149
}
140150

141151
public RemoteWebDriver(URL remoteAddress, Capabilities capabilities) {
142-
this(createTracedExecutorWithTracedHttpClient(remoteAddress), capabilities);
152+
this(createTracedExecutorWithTracedHttpClient(Require.nonNull("Server URL", remoteAddress)),
153+
Require.nonNull("Capabilities", capabilities));
143154
}
144155

145156
private static CommandExecutor createTracedExecutorWithTracedHttpClient(URL remoteAddress) {
@@ -152,11 +163,7 @@ private static CommandExecutor createTracedExecutorWithTracedHttpClient(URL remo
152163
}
153164

154165
public RemoteWebDriver(CommandExecutor executor, Capabilities capabilities) {
155-
if (executor == null) {
156-
throw new IllegalArgumentException("RemoteWebDriver cannot work without a command executor");
157-
}
158-
this.executor = executor;
159-
166+
this.executor = Require.nonNull("Command executor", executor);
160167
capabilities = init(capabilities);
161168

162169
if (executor instanceof NeedsLocalLogs) {

java/client/test/org/openqa/selenium/remote/RemoteWebDriverInitializationTest.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ public void testQuitsIfStartSessionFails() {
7272
public void constructorShouldThrowIfExecutorIsNull() {
7373
assertThatExceptionOfType(IllegalArgumentException.class)
7474
.isThrownBy(() -> new RemoteWebDriver((CommandExecutor) null, new ImmutableCapabilities()))
75-
.withMessage("RemoteWebDriver cannot work without a command executor");
75+
.withMessage("Command executor must be set");
7676
}
7777

7878
@Test
@@ -206,7 +206,8 @@ public void canPassClientConfig() throws MalformedURLException {
206206

207207
CommandExecutor executor = new HttpCommandExecutor(
208208
emptyMap(),
209-
ClientConfig.defaultConfig().readTimeout(Duration.ofSeconds(1)),
209+
ClientConfig.defaultConfig()
210+
.baseUrl(new URL("http://localhost:4444/")).readTimeout(Duration.ofSeconds(1)),
210211
factory);
211212

212213
RemoteWebDriver driver = new RemoteWebDriver(executor, new ImmutableCapabilities());

0 commit comments

Comments
 (0)