Skip to content

Commit 3828661

Browse files
committed
UnreachableBrowserException logs the command parameter details only in debug mode
1 parent 388f530 commit 3828661

8 files changed

Lines changed: 116 additions & 27 deletions

File tree

.idea/aws.xml

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

java/maven_deps.bzl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ def selenium_java_deps():
9595
"org.hamcrest:hamcrest:2.2",
9696
"org.hsqldb:hsqldb:2.7.1",
9797
"org.mockito:mockito-core:4.11.0",
98+
"org.mockito:mockito-inline:4.11.0",
9899
"org.slf4j:slf4j-api:2.0.7",
99100
"org.slf4j:slf4j-jdk14:2.0.7",
100101
"org.apache.logging.log4j:log4j-core:2.20.0",

java/maven_install.json

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"__AUTOGENERATED_FILE_DO_NOT_MODIFY_THIS_FILE_MANUALLY": "THERE_IS_NO_DATA_ONLY_ZUUL",
3-
"__INPUT_ARTIFACTS_HASH": 1835346973,
4-
"__RESOLVED_ARTIFACTS_HASH": 831155937,
3+
"__INPUT_ARTIFACTS_HASH": 1647168181,
4+
"__RESOLVED_ARTIFACTS_HASH": -409306628,
55
"artifacts": {
66
"com.beust:jcommander": {
77
"shasums": {
@@ -902,6 +902,13 @@
902902
},
903903
"version": "4.11.0"
904904
},
905+
"org.mockito:mockito-inline": {
906+
"shasums": {
907+
"jar": "ee52e1c299a632184fba274a9370993e09140429f5e516e6c5570fd6574b297f",
908+
"sources": "ee52e1c299a632184fba274a9370993e09140429f5e516e6c5570fd6574b297f"
909+
},
910+
"version": "4.11.0"
911+
},
905912
"org.objenesis:objenesis": {
906913
"shasums": {
907914
"jar": "02dfd0b0439a5591e35b708ed2f5474eb0948f53abf74637e959b8e4ef69bfeb",
@@ -1475,6 +1482,9 @@
14751482
"net.bytebuddy:byte-buddy-agent",
14761483
"org.objenesis:objenesis"
14771484
],
1485+
"org.mockito:mockito-inline": [
1486+
"org.mockito:mockito-core"
1487+
],
14781488
"org.ow2.asm:asm-analysis": [
14791489
"org.ow2.asm:asm-tree"
14801490
],
@@ -3764,6 +3774,8 @@
37643774
"org.junit.platform:junit-platform-suite-engine:jar:sources",
37653775
"org.mockito:mockito-core",
37663776
"org.mockito:mockito-core:jar:sources",
3777+
"org.mockito:mockito-inline",
3778+
"org.mockito:mockito-inline:jar:sources",
37673779
"org.objenesis:objenesis",
37683780
"org.objenesis:objenesis:jar:sources",
37693781
"org.opentest4j:opentest4j",
@@ -4071,6 +4083,8 @@
40714083
"org.junit.platform:junit-platform-suite-engine:jar:sources",
40724084
"org.mockito:mockito-core",
40734085
"org.mockito:mockito-core:jar:sources",
4086+
"org.mockito:mockito-inline",
4087+
"org.mockito:mockito-inline:jar:sources",
40744088
"org.objenesis:objenesis",
40754089
"org.objenesis:objenesis:jar:sources",
40764090
"org.opentest4j:opentest4j",

java/src/org/openqa/selenium/internal/Debug.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,7 @@
2323
/** Used to provide information about whether Selenium is running under debug mode. */
2424
public class Debug {
2525

26-
private static boolean IS_DEBUG;
27-
26+
private static final boolean IS_DEBUG;
2827
static {
2928
boolean debugFlag =
3029
ManagementFactory.getRuntimeMXBean().getInputArguments().stream()

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

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@
7474
import org.openqa.selenium.devtools.HasDevTools;
7575
import org.openqa.selenium.interactions.Interactive;
7676
import org.openqa.selenium.interactions.Sequence;
77+
import org.openqa.selenium.internal.Debug;
7778
import org.openqa.selenium.internal.Require;
7879
import org.openqa.selenium.logging.LocalLogs;
7980
import org.openqa.selenium.logging.LoggingHandler;
@@ -541,8 +542,7 @@ protected Response execute(CommandPayload payload) {
541542
new UnreachableBrowserException(
542543
"Error communicating with the remote browser. It may have died.", e);
543544
}
544-
populateWebDriverException(toThrow);
545-
toThrow.addInfo("Command", command.toString());
545+
populateWebDriverException(toThrow, command);
546546
throw toThrow;
547547
} finally {
548548
Thread.currentThread().setName(currentName);
@@ -551,21 +551,25 @@ protected Response execute(CommandPayload payload) {
551551
try {
552552
errorHandler.throwIfResponseFailed(response, System.currentTimeMillis() - start);
553553
} catch (WebDriverException ex) {
554-
populateWebDriverException(ex);
555-
ex.addInfo("Command", command.toString());
554+
populateWebDriverException(ex, command);
556555
throw ex;
557556
}
558557
return response;
559558
}
560559

561-
private void populateWebDriverException(WebDriverException ex) {
560+
private void populateWebDriverException(WebDriverException ex, Command command) {
562561
ex.addInfo(WebDriverException.DRIVER_INFO, this.getClass().getName());
563562
if (getSessionId() != null) {
564563
ex.addInfo(WebDriverException.SESSION_ID, getSessionId().toString());
565564
}
566565
if (getCapabilities() != null) {
567566
ex.addInfo("Capabilities", getCapabilities().toString());
568567
}
568+
if (ex instanceof UnreachableBrowserException && !Debug.isDebugging()) {
569+
ex.addInfo("Command", "[" + command.getSessionId() + ", " + command.getName() + " " + command.getParameters().keySet() + "]");
570+
} else {
571+
ex.addInfo("Command", command.toString());
572+
}
569573
}
570574

571575
protected Response execute(String driverCommand, Map<String, ?> parameters) {

java/test/org/openqa/selenium/remote/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ java_test_suite(
3333
artifact("com.google.guava:guava"),
3434
artifact("org.junit.jupiter:junit-jupiter-api"),
3535
artifact("org.mockito:mockito-core"),
36+
artifact("org.mockito:mockito-inline")
3637
] + JUNIT5_DEPS,
3738
)
3839

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

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@
4545
import java.util.logging.Level;
4646
import org.junit.jupiter.api.Tag;
4747
import org.junit.jupiter.api.Test;
48+
import org.mockito.MockedStatic;
49+
import org.mockito.Mockito;
4850
import org.openqa.selenium.Alert;
4951
import org.openqa.selenium.By;
5052
import org.openqa.selenium.Cookie;
@@ -58,6 +60,7 @@
5860
import org.openqa.selenium.WebDriverException;
5961
import org.openqa.selenium.WebElement;
6062
import org.openqa.selenium.WindowType;
63+
import org.openqa.selenium.internal.Debug;
6164
import org.openqa.selenium.virtualauthenticator.VirtualAuthenticator;
6265
import org.openqa.selenium.virtualauthenticator.VirtualAuthenticatorOptions;
6366

@@ -705,6 +708,62 @@ void canHandleGeneralExceptionThrownByCommandExecutor() {
705708
fixture.verifyCommands(new CommandPayload(DriverCommand.GET_CURRENT_URL, emptyMap()));
706709
}
707710

711+
@Test
712+
void canHandleGeneralExceptionInNonDebugModeThrownByCommandExecutor() {
713+
try (MockedStatic<Debug> debugMock = Mockito.mockStatic(Debug.class)) {
714+
final ImmutableMap<String, String> parameters = ImmutableMap.of(
715+
"url", "https://user:[email protected]", "token", "12345Secret");
716+
final CommandPayload commandPayload = new CommandPayload(DriverCommand.GET, parameters);
717+
debugMock.when(Debug::isDebugging).thenReturn(false);
718+
WebDriverFixture fixture = new WebDriverFixture(
719+
new ImmutableCapabilities(
720+
"browserName", "cheese", "platformName", "WINDOWS"),
721+
echoCapabilities, exceptionResponder);
722+
assertThatExceptionOfType(WebDriverException.class)
723+
.isThrownBy(() -> fixture.driver.execute(commandPayload))
724+
.withMessageStartingWith("Error communicating with the remote browser. It may have died.")
725+
.withMessageContaining("Build info: ")
726+
.withMessageContaining(
727+
"Driver info: org.openqa.selenium.remote.RemoteWebDriver")
728+
.withMessageContaining(String.format(
729+
"Session ID: %s", fixture.driver.getSessionId()))
730+
.withMessageContaining(String.format(
731+
"%s", fixture.driver.getCapabilities()))
732+
.withMessageContaining(String.format(
733+
"Command: [%s, get [url, token]]", fixture.driver.getSessionId()))
734+
.havingCause()
735+
.withMessage("BOOM!!!");
736+
}
737+
}
738+
739+
@Test
740+
void canHandleGeneralExceptionInDebugModeThrownByCommandExecutor() {
741+
try (MockedStatic<Debug> debugMock = Mockito.mockStatic(Debug.class)) {
742+
final ImmutableMap<String, String> parameters = ImmutableMap.of(
743+
"url", "https://user:[email protected]", "token", "12345Secret");
744+
final CommandPayload commandPayload = new CommandPayload(DriverCommand.GET, parameters);
745+
debugMock.when(Debug::isDebugging).thenReturn(true);
746+
WebDriverFixture fixture = new WebDriverFixture(
747+
new ImmutableCapabilities(
748+
"browserName", "cheese", "platformName", "WINDOWS"),
749+
echoCapabilities, exceptionResponder);
750+
assertThatExceptionOfType(WebDriverException.class)
751+
.isThrownBy(() -> fixture.driver.execute(commandPayload))
752+
.withMessageStartingWith("Error communicating with the remote browser. It may have died.")
753+
.withMessageContaining("Build info: ")
754+
.withMessageContaining(
755+
"Driver info: org.openqa.selenium.remote.RemoteWebDriver")
756+
.withMessageContaining(String.format(
757+
"Session ID: %s", fixture.driver.getSessionId()))
758+
.withMessageContaining(String.format(
759+
"%s", fixture.driver.getCapabilities()))
760+
.withMessageContaining(String.format(
761+
"Command: [%s, get %s]", fixture.driver.getSessionId(), parameters))
762+
.havingCause()
763+
.withMessage("BOOM!!!");
764+
}
765+
}
766+
708767
@Test
709768
void canHandleWebDriverExceptionReturnedByCommandExecutor() {
710769
WebDriverFixture fixture =

java/test/org/openqa/selenium/remote/RemoteWebElementTest.java

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -91,33 +91,33 @@ void canHandleWebDriverExceptionThrownByCommandExecutor() {
9191

9292
@Test
9393
void canHandleGeneralExceptionThrownByCommandExecutor() {
94-
WebElementFixture fixture =
95-
new WebElementFixture(
96-
new ImmutableCapabilities("browserName", "cheese", "platformName", "WINDOWS"),
97-
echoCapabilities,
98-
exceptionResponder);
94+
try (MockedStatic<Debug> debugMock = Mockito.mockStatic(Debug.class)) {
95+
debugMock.when(Debug::isDebugging).thenReturn(true);
96+
WebElementFixture fixture = new WebElementFixture(
97+
new ImmutableCapabilities("browserName", "cheese", "platformName", "WINDOWS"),
98+
echoCapabilities, exceptionResponder);
9999

100-
assertThatExceptionOfType(WebDriverException.class)
100+
assertThatExceptionOfType(WebDriverException.class)
101101
.isThrownBy(fixture.element::click)
102102
.withMessageStartingWith("Error communicating with the remote browser. It may have died.")
103103
.withMessageContaining("Build info: ")
104-
.withMessageContaining("Driver info: org.openqa.selenium.remote.RemoteWebDriver")
105-
.withMessageContaining(String.format("Session ID: %s", fixture.driver.getSessionId()))
106-
.withMessageContaining(String.format("%s", fixture.driver.getCapabilities()))
107104
.withMessageContaining(
108-
String.format(
109-
"Command: [%s, clickElement {id=%s}]",
110-
fixture.driver.getSessionId(), fixture.element.getId()))
111-
.withMessageContaining(
112-
String.format(
113-
"Element: [[RemoteWebDriver: cheese on windows (%s)] -> id: test]",
114-
fixture.driver.getSessionId()))
105+
"Driver info: org.openqa.selenium.remote.RemoteWebDriver")
106+
.withMessageContaining(String.format(
107+
"Session ID: %s", fixture.driver.getSessionId()))
108+
.withMessageContaining(String.format(
109+
"%s", fixture.driver.getCapabilities()))
110+
.withMessageContaining(String.format(
111+
"Command: [%s, clickElement {id=%s}]", fixture.driver.getSessionId(), fixture.element.getId()))
112+
.withMessageContaining(String.format(
113+
"Element: [[RemoteWebDriver: cheese on WINDOWS (%s)] -> id: test]", fixture.driver.getSessionId()))
115114
.havingCause()
116115
.withMessage("BOOM!!!");
117116

118-
fixture.verifyCommands(
117+
fixture.verifyCommands(
119118
new CommandPayload(
120-
DriverCommand.CLICK_ELEMENT, ImmutableMap.of("id", fixture.element.getId())));
119+
DriverCommand.CLICK_ELEMENT, ImmutableMap.of("id", fixture.element.getId())));
120+
}
121121
}
122122

123123
@Test

0 commit comments

Comments
 (0)