Skip to content

Commit 83075bb

Browse files
committed
[java] Trying alternative way of configuring http client behind a RemoteWebDriver: implementing ability to pass ClientConfig to HttpCommandExecutor constructor
1 parent 02e3398 commit 83075bb

File tree

2 files changed

+68
-13
lines changed

2 files changed

+68
-13
lines changed

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

+34-13
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import org.openqa.selenium.logging.LogType;
2828
import org.openqa.selenium.logging.NeedsLocalLogs;
2929
import org.openqa.selenium.logging.profiler.HttpProfilerLogEntry;
30+
import org.openqa.selenium.remote.http.ClientConfig;
3031
import org.openqa.selenium.remote.http.HttpClient;
3132
import org.openqa.selenium.remote.http.HttpRequest;
3233
import org.openqa.selenium.remote.http.HttpResponse;
@@ -60,6 +61,10 @@ public HttpCommandExecutor(URL addressOfRemoteServer) {
6061
this(emptyMap(), addressOfRemoteServer);
6162
}
6263

64+
public HttpCommandExecutor(ClientConfig config) {
65+
this(emptyMap(), config, defaultClientFactory);
66+
}
67+
6368
/**
6469
* Creates an {@link HttpCommandExecutor} that supports non-standard
6570
* {@code additionalCommands} in addition to the standard.
@@ -68,26 +73,42 @@ public HttpCommandExecutor(URL addressOfRemoteServer) {
6873
* @param addressOfRemoteServer URL of remote end Selenium server
6974
*/
7075
public HttpCommandExecutor(
71-
Map<String, CommandInfo> additionalCommands,
72-
URL addressOfRemoteServer) {
76+
Map<String, CommandInfo> additionalCommands,
77+
URL addressOfRemoteServer)
78+
{
7379
this(additionalCommands, addressOfRemoteServer, defaultClientFactory);
7480
}
7581

7682
public HttpCommandExecutor(
77-
Map<String, CommandInfo> additionalCommands,
78-
URL addressOfRemoteServer,
79-
HttpClient.Factory httpClientFactory) {
83+
Map<String, CommandInfo> additionalCommands,
84+
URL addressOfRemoteServer,
85+
HttpClient.Factory httpClientFactory)
86+
{
87+
this(additionalCommands,
88+
ClientConfig.defaultConfig().baseUrl(addressOfRemoteServer),
89+
httpClientFactory);
90+
}
91+
92+
public HttpCommandExecutor(
93+
Map<String, CommandInfo> additionalCommands,
94+
ClientConfig config,
95+
HttpClient.Factory httpClientFactory)
96+
{
8097
try {
81-
remoteServer = addressOfRemoteServer == null
82-
? new URL(System.getProperty("webdriver.remote.server", "http://localhost:4444/"))
83-
: addressOfRemoteServer;
98+
if (config.baseUri() != null) {
99+
remoteServer = config.baseUrl();
100+
} else {
101+
remoteServer = new URL(
102+
System.getProperty("webdriver.remote.server", "http://localhost:4444/"));
103+
config = config.baseUrl(remoteServer);
104+
}
84105
} catch (MalformedURLException e) {
85106
throw new WebDriverException(e);
86107
}
87108

88109
this.additionalCommands = additionalCommands;
89110
this.httpClientFactory = httpClientFactory;
90-
this.client = httpClientFactory.createClient(remoteServer);
111+
this.client = httpClientFactory.createClient(config);
91112
}
92113

93114
/**
@@ -126,7 +147,7 @@ public Response execute(Command command) throws IOException {
126147
if (!GET_ALL_SESSIONS.equals(command.getName())
127148
&& !NEW_SESSION.equals(command.getName())) {
128149
throw new NoSuchSessionException(
129-
"Session ID is null. Using WebDriver after calling quit()?");
150+
"Session ID is null. Using WebDriver after calling quit()?");
130151
}
131152
}
132153

@@ -149,7 +170,7 @@ public Response execute(Command command) throws IOException {
149170

150171
if (commandCodec == null || responseCodec == null) {
151172
throw new WebDriverException(
152-
"No command or response codec has been defined. Unable to proceed");
173+
"No command or response codec has been defined. Unable to proceed");
153174
}
154175

155176
HttpRequest httpRequest = commandCodec.encode(command);
@@ -180,8 +201,8 @@ public Response execute(Command command) throws IOException {
180201
} catch (UnsupportedCommandException e) {
181202
if (e.getMessage() == null || "".equals(e.getMessage())) {
182203
throw new UnsupportedOperationException(
183-
"No information from server. Command name was: " + command.getName(),
184-
e.getCause());
204+
"No information from server. Command name was: " + command.getName(),
205+
e.getCause());
185206
}
186207
throw e;
187208
}

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

+34
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717

1818
package org.openqa.selenium.remote;
1919

20+
import static java.util.Collections.emptyMap;
21+
import static java.util.Collections.singletonMap;
2022
import static org.assertj.core.api.Assertions.assertThat;
2123
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
2224
import static org.mockito.ArgumentMatchers.any;
@@ -35,14 +37,22 @@
3537

3638
import org.junit.Test;
3739
import org.junit.experimental.categories.Category;
40+
import org.mockito.ArgumentCaptor;
3841
import org.openqa.selenium.Capabilities;
3942
import org.openqa.selenium.ImmutableCapabilities;
4043
import org.openqa.selenium.Platform;
4144
import org.openqa.selenium.SessionNotCreatedException;
45+
import org.openqa.selenium.remote.http.ClientConfig;
46+
import org.openqa.selenium.remote.http.Contents;
47+
import org.openqa.selenium.remote.http.HttpClient;
48+
import org.openqa.selenium.remote.http.HttpResponse;
4249
import org.openqa.selenium.testing.UnitTests;
4350

4451
import java.io.IOException;
4552
import java.io.UncheckedIOException;
53+
import java.net.MalformedURLException;
54+
import java.net.URL;
55+
import java.time.Duration;
4656
import java.util.UUID;
4757

4858
@Category(UnitTests.class)
@@ -182,6 +192,30 @@ public void quit() {
182192
}
183193
}
184194

195+
@Test
196+
public void canPassClientConfig() throws MalformedURLException {
197+
HttpClient client = mock(HttpClient.class);
198+
when(client.execute(any())).thenReturn(new HttpResponse().setStatus(200).setContent(
199+
Contents.asJson(singletonMap("value", ImmutableMap.of(
200+
"sessionId", UUID.randomUUID().toString(),
201+
"capabilities", new ImmutableCapabilities().asMap())))));
202+
203+
HttpClient.Factory factory = mock(HttpClient.Factory.class);
204+
ArgumentCaptor<ClientConfig > config = ArgumentCaptor.forClass(ClientConfig.class);
205+
when(factory.createClient(config.capture())).thenReturn(client);
206+
207+
CommandExecutor executor = new HttpCommandExecutor(
208+
emptyMap(),
209+
ClientConfig.defaultConfig().readTimeout(Duration.ofSeconds(1)),
210+
factory);
211+
212+
RemoteWebDriver driver = new RemoteWebDriver(executor, new ImmutableCapabilities());
213+
214+
ClientConfig usedConfig = config.getValue();
215+
assertThat(usedConfig.baseUrl()).isEqualTo(new URL("http://localhost:4444/"));
216+
assertThat(usedConfig.readTimeout()).isEqualTo(Duration.ofSeconds(1));
217+
}
218+
185219
public void verifyNoCommands(CommandExecutor executor) {
186220
try {
187221
verify(executor).execute(argThat(cmd -> cmd.getName().equals(DriverCommand.NEW_SESSION)));

0 commit comments

Comments
 (0)