Skip to content

Commit d5695d3

Browse files
committed
[java] Throw SessionNotCreatedException instead of UnreachableBrowserException if a session cannot be started
1 parent bcdbb1f commit d5695d3

File tree

2 files changed

+32
-24
lines changed

2 files changed

+32
-24
lines changed

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

+17-14
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@
5959
import org.openqa.selenium.Platform;
6060
import org.openqa.selenium.Point;
6161
import org.openqa.selenium.SearchContext;
62+
import org.openqa.selenium.SessionNotCreatedException;
6263
import org.openqa.selenium.TakesScreenshot;
6364
import org.openqa.selenium.WebDriver;
6465
import org.openqa.selenium.WebDriverException;
@@ -498,7 +499,7 @@ protected Response execute(CommandPayload payload) {
498499
long start = System.currentTimeMillis();
499500
String currentName = Thread.currentThread().getName();
500501
Thread.currentThread().setName(
501-
String.format("Forwarding %s on session %s to remote", command.getName(), sessionId));
502+
String.format("Forwarding %s on session %s to remote", command.getName(), sessionId));
502503
try {
503504
log(sessionId, command.getName(), command, When.BEFORE);
504505
response = executor.execute(command);
@@ -518,22 +519,24 @@ protected Response execute(CommandPayload payload) {
518519
} catch (WebDriverException e) {
519520
e.addInfo("Command", command.toString());
520521
throw e;
521-
} catch (Exception e) {
522+
} catch (Throwable e) {
522523
log(sessionId, command.getName(), command, When.EXCEPTION);
523-
String errorMessage = "Error communicating with the remote browser. " +
524-
"It may have died.";
525524
if (command.getName().equals(DriverCommand.NEW_SESSION)) {
526-
errorMessage = "Could not start a new session. Possible causes are " +
527-
"invalid address of the remote server or browser start-up failure.";
528-
}
529-
UnreachableBrowserException ube = new UnreachableBrowserException(errorMessage, e);
530-
if (getSessionId() != null) {
531-
ube.addInfo(WebDriverException.SESSION_ID, getSessionId().toString());
532-
}
533-
if (getCapabilities() != null) {
534-
ube.addInfo("Capabilities", getCapabilities().toString());
525+
throw new SessionNotCreatedException(
526+
"Could not start a new session. Possible causes are invalid address of the remote server or browser start-up failure.",
527+
e);
528+
} else {
529+
WebDriverException toThrow = new UnreachableBrowserException(
530+
"Error communicating with the remote browser. It may have died.",
531+
e);
532+
if (getSessionId() != null) {
533+
toThrow.addInfo(WebDriverException.SESSION_ID, getSessionId().toString());
534+
}
535+
if (getCapabilities() != null) {
536+
toThrow.addInfo("Capabilities", getCapabilities().toString());
537+
}
538+
throw toThrow;
535539
}
536-
throw ube;
537540
} finally {
538541
Thread.currentThread().setName(currentName);
539542
}

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

+15-10
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
import org.openqa.selenium.Platform;
4848
import org.openqa.selenium.Point;
4949
import org.openqa.selenium.Rectangle;
50+
import org.openqa.selenium.SessionNotCreatedException;
5051
import org.openqa.selenium.WebDriver;
5152
import org.openqa.selenium.WebElement;
5253
import org.openqa.selenium.WindowType;
@@ -78,9 +79,9 @@ public void constructorShouldThrowIfExecutorIsNull() {
7879
}
7980

8081
@Test
81-
public void constructorShouldThrowIfExecutorCannotStartASession() throws IOException {
82-
CommandExecutor executor = prepareExecutorMock(nullResponder, nullResponder);
83-
assertThatExceptionOfType(UnreachableBrowserException.class)
82+
public void constructorShouldThrowIfExecutorThrowsOnAnAttemptToStartASession() throws IOException {
83+
CommandExecutor executor = prepareExecutorMock(exceptionalResponder);
84+
assertThatExceptionOfType(SessionNotCreatedException.class)
8485
.isThrownBy(() -> new RemoteWebDriver(executor, new ImmutableCapabilities()));
8586

8687
verify(executor).execute(argThat(
@@ -1140,28 +1141,32 @@ private boolean isWebElement(Object value) {
11401141
.isPresent();
11411142
}
11421143

1143-
private Function<Command, Response> echoCapabilities = cmd -> {
1144+
private final Function<Command, Response> echoCapabilities = cmd -> {
11441145
Response nullResponse = new Response();
11451146
nullResponse.setValue(
1146-
((Capabilities) cmd.getParameters().get("desiredCapabilities")).asMap()
1147-
.entrySet().stream()
1148-
.collect(Collectors.toMap(e -> e.getKey(), e -> e.getValue().toString())));
1147+
((Capabilities) cmd.getParameters().get("desiredCapabilities")).asMap()
1148+
.entrySet().stream()
1149+
.collect(Collectors.toMap(e -> e.getKey(), e -> e.getValue().toString())));
11491150
nullResponse.setSessionId(UUID.randomUUID().toString());
11501151
return nullResponse;
11511152
};
11521153

1153-
private Function<Command, Response> nullResponder = cmd -> {
1154+
private final Function<Command, Response> nullResponder = cmd -> {
11541155
Response nullResponse = new Response();
11551156
nullResponse.setValue(null);
1156-
nullResponse.setSessionId(cmd.getSessionId().toString());
1157+
nullResponse.setSessionId(cmd.getSessionId() != null ? cmd.getSessionId().toString() : null);
11571158
return nullResponse;
11581159
};
11591160

1161+
private final Function<Command, Response> exceptionalResponder = cmd -> {
1162+
throw new InternalError("BOOM!!!");
1163+
};
1164+
11601165
private Function<Command, Response> valueResponder(Object value) {
11611166
return cmd -> {
11621167
Response nullResponse = new Response();
11631168
nullResponse.setValue(value);
1164-
nullResponse.setSessionId(cmd.getSessionId().toString());
1169+
nullResponse.setSessionId(cmd.getSessionId() != null ? cmd.getSessionId().toString() : null);
11651170
return nullResponse;
11661171
};
11671172
}

0 commit comments

Comments
 (0)