Skip to content

Commit bcdbb1f

Browse files
committed
[java] RemoteWebDriver should fail fast if there is no command executor provided
1 parent 19548ca commit bcdbb1f

File tree

2 files changed

+23
-18
lines changed

2 files changed

+23
-18
lines changed

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

+13-10
Original file line numberDiff line numberDiff line change
@@ -83,9 +83,9 @@
8383
import org.openqa.selenium.virtualauthenticator.VirtualAuthenticatorOptions;
8484

8585
@Augmentable
86-
public class RemoteWebDriver implements WebDriver, JavascriptExecutor,
87-
HasInputDevices, HasCapabilities, Interactive, TakesScreenshot,
88-
HasVirtualAuthenticator {
86+
public class RemoteWebDriver implements WebDriver, JavascriptExecutor, HasInputDevices,
87+
HasCapabilities, Interactive, TakesScreenshot,
88+
HasVirtualAuthenticator {
8989

9090
// TODO(dawagner): This static logger should be unified with the per-instance localLogs
9191
private static final Logger logger = Logger.getLogger(RemoteWebDriver.class.getName());
@@ -114,13 +114,20 @@ public RemoteWebDriver(Capabilities capabilities) {
114114
this((URL) null, capabilities);
115115
}
116116

117+
public RemoteWebDriver(URL remoteAddress, Capabilities capabilities) {
118+
this(new HttpCommandExecutor(remoteAddress), capabilities);
119+
}
120+
117121
public RemoteWebDriver(CommandExecutor executor, Capabilities capabilities) {
122+
if (executor == null) {
123+
throw new IllegalArgumentException("RemoteWebDriver cannot work without a command executor");
124+
}
118125
this.executor = executor;
119126

120127
init(capabilities);
121128

122129
if (executor instanceof NeedsLocalLogs) {
123-
((NeedsLocalLogs)executor).setLocalLogs(localLogs);
130+
((NeedsLocalLogs) executor).setLocalLogs(localLogs);
124131
}
125132

126133
try {
@@ -136,10 +143,6 @@ public RemoteWebDriver(CommandExecutor executor, Capabilities capabilities) {
136143
}
137144
}
138145

139-
public RemoteWebDriver(URL remoteAddress, Capabilities capabilities) {
140-
this(new HttpCommandExecutor(remoteAddress), capabilities);
141-
}
142-
143146
@Beta
144147
public static RemoteWebDriverBuilder builder() {
145148
return new RemoteWebDriverBuilder();
@@ -173,8 +176,8 @@ private void init(Capabilities capabilities) {
173176
Set<String> logTypesToInclude = builder.build();
174177

175178
LocalLogs performanceLogger = LocalLogs.getStoringLoggerInstance(logTypesToInclude);
176-
LocalLogs clientLogs = LocalLogs.getHandlerBasedLoggerInstance(LoggingHandler.getInstance(),
177-
logTypesToInclude);
179+
LocalLogs clientLogs = LocalLogs.getHandlerBasedLoggerInstance(
180+
LoggingHandler.getInstance(), logTypesToInclude);
178181
localLogs = LocalLogs.getCombinedLogsHolder(clientLogs, performanceLogger);
179182
remoteLogs = new RemoteLogs(executeMethod, localLogs);
180183
}

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

+10-8
Original file line numberDiff line numberDiff line change
@@ -71,19 +71,20 @@ public class RemoteWebDriverUnitTest {
7171
private static final String ELEMENT_KEY = "element-6066-11e4-a52e-4f735466cecf";
7272

7373
@Test
74-
public void whatIfExecutorIsNull() {
75-
assertThatExceptionOfType(UnreachableBrowserException.class)
76-
.isThrownBy(() -> new RemoteWebDriver((CommandExecutor) null, new ImmutableCapabilities()));
74+
public void constructorShouldThrowIfExecutorIsNull() {
75+
assertThatExceptionOfType(IllegalArgumentException.class)
76+
.isThrownBy(() -> new RemoteWebDriver((CommandExecutor) null, new ImmutableCapabilities()))
77+
.withMessage("RemoteWebDriver cannot work without a command executor");
7778
}
7879

7980
@Test
80-
public void whatIfExecutorCannotStartASession() throws IOException {
81+
public void constructorShouldThrowIfExecutorCannotStartASession() throws IOException {
8182
CommandExecutor executor = prepareExecutorMock(nullResponder, nullResponder);
8283
assertThatExceptionOfType(UnreachableBrowserException.class)
83-
.isThrownBy(() -> new RemoteWebDriver(executor, new ImmutableCapabilities()));
84+
.isThrownBy(() -> new RemoteWebDriver(executor, new ImmutableCapabilities()));
8485

8586
verify(executor).execute(argThat(
86-
command -> command.getName().equals(DriverCommand.NEW_SESSION)));
87+
command -> command.getName().equals(DriverCommand.NEW_SESSION)));
8788
verifyNoMoreInteractions(executor);
8889
}
8990

@@ -1166,8 +1167,9 @@ private Function<Command, Response> valueResponder(Object value) {
11661167
}
11671168

11681169
@SafeVarargs
1169-
private final CommandExecutor prepareExecutorMock(
1170-
Function<Command, Response>... handlers) throws IOException {
1170+
private final CommandExecutor prepareExecutorMock(Function<Command, Response>... handlers)
1171+
throws IOException
1172+
{
11711173
CommandExecutor executor = mock(CommandExecutor.class);
11721174
OngoingStubbing<Response> callChain = when(executor.execute(any()));
11731175
for (Function<Command, Response> handler : handlers) {

0 commit comments

Comments
 (0)