Skip to content

Commit 9ff4c2b

Browse files
debanjanc01diemol
andauthored
UnreachableBrowserException logs the command parameter details only in debug mode (#11328)
* UnreachableBrowserException logs the command parameter details only in debug mode * [java] Refining PR --------- Co-authored-by: Diego Molina <diemol@gmail.com> Co-authored-by: Diego Molina <diemol@users.noreply.github.com>
1 parent bc3b548 commit 9ff4c2b

File tree

7 files changed

+109
-24
lines changed

7 files changed

+109
-24
lines changed

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 & 1 deletion
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;
@@ -542,7 +543,14 @@ protected Response execute(CommandPayload payload) {
542543
"Error communicating with the remote browser. It may have died.", e);
543544
}
544545
populateWebDriverException(toThrow);
545-
toThrow.addInfo("Command", command.toString());
546+
// Showing full command information when user is debugging
547+
// Avoid leaking user/pwd values for authenticated Grids.
548+
if (toThrow instanceof UnreachableBrowserException && !Debug.isDebugging()) {
549+
toThrow.addInfo("Command", "[" + command.getSessionId() + ", " + command.getName()
550+
+ " " + command.getParameters().keySet() + "]");
551+
} else {
552+
toThrow.addInfo("Command", command.toString());
553+
}
546554
throw toThrow;
547555
} finally {
548556
Thread.currentThread().setName(currentName);

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: 60 additions & 1 deletion
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

@@ -698,13 +701,69 @@ void canHandleGeneralExceptionThrownByCommandExecutor() {
698701
.withMessageContaining(String.format("Session ID: %s", fixture.driver.getSessionId()))
699702
.withMessageContaining(String.format("%s", fixture.driver.getCapabilities()))
700703
.withMessageContaining(
701-
String.format("Command: [%s, getCurrentUrl {}]", fixture.driver.getSessionId()))
704+
String.format("Command: [%s, getCurrentUrl []]", fixture.driver.getSessionId()))
702705
.havingCause()
703706
.withMessage("BOOM!!!");
704707

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:password@somedomain.com", "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(UnreachableBrowserException.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:password@somedomain.com", "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(UnreachableBrowserException.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: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,16 @@
3030
import com.google.common.collect.ImmutableMap;
3131
import org.junit.jupiter.api.Tag;
3232
import org.junit.jupiter.api.Test;
33+
import org.mockito.MockedStatic;
34+
import org.mockito.Mockito;
3335
import org.openqa.selenium.By;
3436
import org.openqa.selenium.Dimension;
3537
import org.openqa.selenium.ImmutableCapabilities;
3638
import org.openqa.selenium.NoSuchElementException;
3739
import org.openqa.selenium.Point;
3840
import org.openqa.selenium.Rectangle;
3941
import org.openqa.selenium.WebDriverException;
42+
import org.openqa.selenium.internal.Debug;
4043

4144
@Tag("UnitTests")
4245
class RemoteWebElementTest {
@@ -91,33 +94,33 @@ void canHandleWebDriverExceptionThrownByCommandExecutor() {
9194

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

100-
assertThatExceptionOfType(WebDriverException.class)
103+
assertThatExceptionOfType(WebDriverException.class)
101104
.isThrownBy(fixture.element::click)
102105
.withMessageStartingWith("Error communicating with the remote browser. It may have died.")
103106
.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()))
107107
.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()))
108+
"Driver info: org.openqa.selenium.remote.RemoteWebDriver")
109+
.withMessageContaining(String.format(
110+
"Session ID: %s", fixture.driver.getSessionId()))
111+
.withMessageContaining(String.format(
112+
"%s", fixture.driver.getCapabilities()))
113+
.withMessageContaining(String.format(
114+
"Command: [%s, clickElement {id=%s}]", fixture.driver.getSessionId(), fixture.element.getId()))
115+
.withMessageContaining(String.format(
116+
"Element: [[RemoteWebDriver: cheese on windows (%s)] -> id: test]", fixture.driver.getSessionId()))
115117
.havingCause()
116118
.withMessage("BOOM!!!");
117119

118-
fixture.verifyCommands(
120+
fixture.verifyCommands(
119121
new CommandPayload(
120-
DriverCommand.CLICK_ELEMENT, ImmutableMap.of("id", fixture.element.getId())));
122+
DriverCommand.CLICK_ELEMENT, ImmutableMap.of("id", fixture.element.getId())));
123+
}
121124
}
122125

123126
@Test

0 commit comments

Comments
 (0)