Skip to content

Commit 9514499

Browse files
committed
[java] Adding more unit tests for RemoteWebDriver throwing an exception
1 parent 681059e commit 9514499

File tree

2 files changed

+82
-4
lines changed

2 files changed

+82
-4
lines changed

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

+1-3
Original file line numberDiff line numberDiff line change
@@ -533,9 +533,7 @@ protected Response execute(CommandPayload payload) {
533533
WebDriverException toThrow;
534534
if (command.getName().equals(DriverCommand.NEW_SESSION)) {
535535
toThrow = new SessionNotCreatedException(
536-
"Could not start a new session. Possible causes are invalid address"
537-
+ " of the remote server or browser start-up failure.",
538-
e);
536+
"Possible causes are invalid address of the remote server or browser start-up failure.", e);
539537
} else if (e instanceof WebDriverException) {
540538
toThrow = (WebDriverException) e;
541539
} else {

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

+81-1
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,10 @@ public void constructorShouldThrowIfExecutorIsNull() {
8585
public void constructorShouldThrowIfExecutorThrowsOnAnAttemptToStartASession() {
8686
CommandExecutor executor = prepareExecutorMock(exceptionResponder);
8787
assertThatExceptionOfType(SessionNotCreatedException.class)
88-
.isThrownBy(() -> new RemoteWebDriver(executor, new ImmutableCapabilities()));
88+
.isThrownBy(() -> new RemoteWebDriver(executor, new ImmutableCapabilities()))
89+
.withMessageContaining("Build info: ")
90+
.withMessageContaining("Driver info: org.openqa.selenium.remote.RemoteWebDriver")
91+
.withMessageContaining("Command: [null, newSession {desiredCapabilities=Capabilities {}}]");
8992

9093
verifyCommands(executor, null); // no commands
9194
}
@@ -978,6 +981,72 @@ public void canHandleGeneralExceptionThrownByCommandExecutor() {
978981
new CommandPayload(DriverCommand.CLICK_ELEMENT, ImmutableMap.of("id", element.getId())));
979982
}
980983

984+
@Test
985+
public void canHandleWebDriverExceptionReturnedByCommandExecutor() {
986+
CommandExecutor executor = prepareExecutorMock(
987+
echoCapabilities, errorResponder("element click intercepted", new WebDriverException("BOOM!!!")));
988+
989+
RemoteWebDriver driver = new RemoteWebDriver(executor, new ImmutableCapabilities(
990+
"browserName", "cheese"));
991+
RemoteWebElement element = new RemoteWebElement();
992+
element.setParent(driver);
993+
String elementId = UUID.randomUUID().toString();
994+
element.setId(elementId);
995+
element.setFoundBy(driver, "id", "test");
996+
997+
assertThatExceptionOfType(WebDriverException.class)
998+
.isThrownBy(element::click)
999+
.withMessageStartingWith("BOOM!!!")
1000+
.withMessageContaining("Build info: ")
1001+
.withMessageContaining(
1002+
"Driver info: org.openqa.selenium.remote.RemoteWebDriver")
1003+
.withMessageContaining(String.format(
1004+
"Session ID: %s", driver.getSessionId()))
1005+
.withMessageContaining(String.format(
1006+
"%s", driver.getCapabilities()))
1007+
.withMessageContaining(String.format(
1008+
"Command: [%s, clickElement {id=%s}]", driver.getSessionId(), elementId))
1009+
.withMessageContaining(String.format(
1010+
"Element: [[RemoteWebDriver: cheese on ANY (%s)] -> id: test]", driver.getSessionId()));
1011+
1012+
verifyCommands(
1013+
executor, driver.getSessionId(),
1014+
new CommandPayload(DriverCommand.CLICK_ELEMENT, ImmutableMap.of("id", element.getId())));
1015+
}
1016+
1017+
@Test
1018+
public void canHandleResponseWithErrorCodeButNoExceptionReturnedByCommandExecutor() {
1019+
CommandExecutor executor = prepareExecutorMock(
1020+
echoCapabilities, errorResponder("element click intercepted", "BOOM!!!"));
1021+
1022+
RemoteWebDriver driver = new RemoteWebDriver(executor, new ImmutableCapabilities(
1023+
"browserName", "cheese"));
1024+
RemoteWebElement element = new RemoteWebElement();
1025+
element.setParent(driver);
1026+
String elementId = UUID.randomUUID().toString();
1027+
element.setId(elementId);
1028+
element.setFoundBy(driver, "id", "test");
1029+
1030+
assertThatExceptionOfType(WebDriverException.class)
1031+
.isThrownBy(element::click)
1032+
.withMessageStartingWith("BOOM!!!")
1033+
.withMessageContaining("Build info: ")
1034+
.withMessageContaining(
1035+
"Driver info: org.openqa.selenium.remote.RemoteWebDriver")
1036+
.withMessageContaining(String.format(
1037+
"Session ID: %s", driver.getSessionId()))
1038+
.withMessageContaining(String.format(
1039+
"%s", driver.getCapabilities()))
1040+
.withMessageContaining(String.format(
1041+
"Command: [%s, clickElement {id=%s}]", driver.getSessionId(), elementId))
1042+
.withMessageContaining(String.format(
1043+
"Element: [[RemoteWebDriver: cheese on ANY (%s)] -> id: test]", driver.getSessionId()));
1044+
1045+
verifyCommands(
1046+
executor, driver.getSessionId(),
1047+
new CommandPayload(DriverCommand.CLICK_ELEMENT, ImmutableMap.of("id", element.getId())));
1048+
}
1049+
9811050
@Test
9821051
public void canHandleElementClearCommand() {
9831052
CommandExecutor executor = prepareExecutorMock(echoCapabilities, nullValueResponder);
@@ -1300,6 +1369,17 @@ private Function<Command, Response> valueResponder(Object value) {
13001369
};
13011370
}
13021371

1372+
private Function<Command, Response> errorResponder(String state, Object value) {
1373+
return cmd -> {
1374+
Response response = new Response();
1375+
response.setState(state);
1376+
response.setStatus(new ErrorCodes().toStatus(state, Optional.of(400)));
1377+
response.setValue(value);
1378+
response.setSessionId(cmd.getSessionId() != null ? cmd.getSessionId().toString() : null);
1379+
return response;
1380+
};
1381+
}
1382+
13031383
private final Function<Command, Response> echoCapabilities = cmd -> {
13041384
Response response = new Response();
13051385
response.setValue(

0 commit comments

Comments
 (0)