Skip to content

Commit 0eabe11

Browse files
committed
[java] Adding more information about executed command and target element to WebDriverException
1 parent ccd13d6 commit 0eabe11

File tree

3 files changed

+42
-28
lines changed

3 files changed

+42
-28
lines changed

java/client/src/org/openqa/selenium/WebDriverException.java

+26-26
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
import java.util.HashMap;
2323
import java.util.Map;
24+
import java.util.Optional;
2425
import java.util.stream.Collectors;
2526
import java.util.stream.Stream;
2627

@@ -58,26 +59,25 @@ public String getMessage() {
5859
}
5960

6061
private String createMessage(String originalMessageString) {
61-
String supportMessage = getSupportUrl() == null ?
62-
"" : "For documentation on this error, please visit: " + getSupportUrl();
62+
String supportMessage = Optional.ofNullable(getSupportUrl())
63+
.map(url -> String.format("For documentation on this error, please visit: %s", url))
64+
.orElse("");
6365

6466
return Stream.of(
65-
originalMessageString == null ? "" : originalMessageString,
66-
supportMessage,
67-
getBuildInformation().toString(),
68-
getSystemInformation(),
69-
getAdditionalInformation()
67+
originalMessageString == null ? "" : originalMessageString,
68+
supportMessage,
69+
getBuildInformation().toString(),
70+
getSystemInformation(),
71+
getAdditionalInformation()
7072
).filter(s -> !(s == null || s.equals(""))).collect(Collectors.joining("\n"));
7173
}
7274

7375
public String getSystemInformation() {
74-
return String.format("System info: host: '%s', ip: '%s', os.name: '%s', os.arch: '%s', os.version: '%s', java.version: '%s'",
75-
HOST_NAME,
76-
HOST_ADDRESS,
77-
System.getProperty("os.name"),
78-
System.getProperty("os.arch"),
79-
System.getProperty("os.version"),
80-
System.getProperty("java.version"));
76+
return String.format(
77+
"System info: host: '%s', ip: '%s', os.name: '%s', os.arch: '%s', os.version: '%s', java.version: '%s'",
78+
HOST_NAME, HOST_ADDRESS,
79+
System.getProperty("os.name"), System.getProperty("os.arch"),
80+
System.getProperty("os.version"), System.getProperty("java.version"));
8181
}
8282

8383
public String getSupportUrl() {
@@ -90,13 +90,13 @@ public BuildInfo getBuildInformation() {
9090

9191
public static String getDriverName(StackTraceElement[] stackTraceElements) {
9292
return Stream.of(stackTraceElements)
93-
.filter(e -> e.getClassName().endsWith("Driver"))
94-
.map(e -> {
95-
String[] bits = e.getClassName().split("\\.");
96-
return bits[bits.length - 1];
97-
})
98-
.reduce((first, last) -> last)
99-
.orElse("unknown");
93+
.filter(e -> e.getClassName().endsWith("Driver"))
94+
.map(e -> {
95+
String[] bits = e.getClassName().split("\\.");
96+
return bits[bits.length - 1];
97+
})
98+
.reduce((first, last) -> last)
99+
.orElse("unknown");
100100
}
101101

102102
public void addInfo(String key, String value) {
@@ -105,12 +105,12 @@ public void addInfo(String key, String value) {
105105

106106
public String getAdditionalInformation() {
107107
extraInfo.computeIfAbsent(
108-
DRIVER_INFO, key -> "driver.version: " + getDriverName(getStackTrace()));
108+
DRIVER_INFO, key -> "driver.version: " + getDriverName(getStackTrace()));
109109

110110
return extraInfo.entrySet().stream()
111-
.map(entry -> entry.getValue() != null && entry.getValue().startsWith(entry.getKey())
112-
? entry.getValue()
113-
: entry.getKey() + ": " + entry.getValue())
114-
.collect(Collectors.joining("\n"));
111+
.map(entry -> entry.getValue() != null && entry.getValue().startsWith(entry.getKey())
112+
? entry.getValue()
113+
: entry.getKey() + ": " + entry.getValue())
114+
.collect(Collectors.joining("\n"));
115115
}
116116
}

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

+4
Original file line numberDiff line numberDiff line change
@@ -508,8 +508,12 @@ protected Response execute(CommandPayload payload) {
508508
// Unwrap the response value by converting any JSON objects of the form
509509
// {"ELEMENT": id} to RemoteWebElements.
510510
Object value = getElementConverter().apply(response.getValue());
511+
if (value instanceof WebDriverException) {
512+
((WebDriverException) value).addInfo("Command", command.toString());
513+
}
511514
response.setValue(value);
512515
} catch (WebDriverException e) {
516+
e.addInfo("Command", command.toString());
513517
throw e;
514518
} catch (Exception e) {
515519
log(sessionId, command.getName(), command, When.EXCEPTION);

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

+12-2
Original file line numberDiff line numberDiff line change
@@ -235,11 +235,21 @@ protected List<WebElement> findElements(String using, String value) {
235235
}
236236

237237
protected Response execute(CommandPayload payload) {
238-
return parent.execute(payload);
238+
try {
239+
return parent.execute(payload);
240+
} catch (WebDriverException ex) {
241+
ex.addInfo("Element", this.toString());
242+
throw ex;
243+
}
239244
}
240245

241246
protected Response execute(String command, Map<String, ?> parameters) {
242-
return parent.execute(command, parameters);
247+
try {
248+
return parent.execute(command, parameters);
249+
} catch (WebDriverException ex) {
250+
ex.addInfo("Element", this.toString());
251+
throw ex;
252+
}
243253
}
244254

245255
@Override

0 commit comments

Comments
 (0)