Skip to content

Commit a537497

Browse files
authored
[java] move error message and stack trace annotation from test runners to main bindings (#339)
1 parent 144a1b7 commit a537497

File tree

2 files changed

+32
-23
lines changed

2 files changed

+32
-23
lines changed

java/junit5/src/main/java/com/saucelabs/saucebindings/junit5/SauceBindingsExtension.java

+2-22
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,10 @@
77
import java.io.IOException;
88
import java.io.InputStream;
99
import java.util.ArrayList;
10-
import java.util.Arrays;
1110
import java.util.List;
1211
import java.util.Optional;
1312
import java.util.Properties;
1413
import java.util.logging.Logger;
15-
import java.util.stream.Collectors;
1614
import org.junit.jupiter.api.DisplayName;
1715
import org.junit.jupiter.api.extension.BeforeEachCallback;
1816
import org.junit.jupiter.api.extension.ExtensionContext;
@@ -22,7 +20,6 @@
2220
import org.openqa.selenium.Capabilities;
2321
import org.openqa.selenium.NoSuchSessionException;
2422
import org.openqa.selenium.WebDriver;
25-
import org.openqa.selenium.remote.RemoteWebDriver;
2623

2724
public class SauceBindingsExtension implements TestWatcher, BeforeEachCallback, ParameterResolver {
2825
private static final Logger LOGGER = Logger.getLogger(SauceBindingsExtension.class.getName());
@@ -101,7 +98,6 @@ private ExtensionContext.Store getStore(ExtensionContext context) {
10198
public void testSuccessful(ExtensionContext context) {
10299
if (!SauceSession.isDisabled()) {
103100
SauceSession session = (SauceSession) getStore(context).get("session");
104-
RemoteWebDriver driver = session.getDriver();
105101
try {
106102
session.stop(true);
107103
} catch (NoSuchSessionException e) {
@@ -117,14 +113,7 @@ public void testFailed(ExtensionContext context, Throwable cause) {
117113
if (!SauceSession.isDisabled()) {
118114
SauceSession session = (SauceSession) getStore(context).get("session");
119115
try {
120-
session.annotate("Failure Reason: " + cause.getMessage());
121-
122-
Arrays.stream(cause.getStackTrace())
123-
.map(StackTraceElement::toString)
124-
.filter(line -> !line.contains("sun"))
125-
.forEach(session::annotate);
126-
127-
session.stop(false);
116+
session.stop(cause);
128117
} catch (NoSuchSessionException e) {
129118
LOGGER.severe(
130119
"Driver quit prematurely; Remove calls to `driver.quit()` to allow"
@@ -138,16 +127,7 @@ public void testAborted(ExtensionContext context, Throwable cause) {
138127
LOGGER.fine("Test Aborted: " + cause.getMessage());
139128
SauceSession session = (SauceSession) getStore(context).get("session");
140129
if (session != null) {
141-
session.annotate("Test Aborted; marking completed instead of failed");
142-
session.annotate("Reason: " + cause.getMessage());
143-
144-
String stackTrace =
145-
Arrays.stream(cause.getStackTrace())
146-
.map(StackTraceElement::toString)
147-
.collect(Collectors.joining("\n"));
148-
session.annotate(stackTrace);
149-
150-
session.abort();
130+
session.abort(cause);
151131
}
152132
}
153133

java/main/src/main/java/com/saucelabs/saucebindings/SauceSession.java

+30-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@
66
import com.saucelabs.saucebindings.options.SauceOptions;
77
import java.net.MalformedURLException;
88
import java.net.URL;
9+
import java.util.Arrays;
910
import java.util.List;
11+
import java.util.stream.Collectors;
1012
import lombok.Getter;
1113
import lombok.Setter;
1214
import org.openqa.selenium.Capabilities;
@@ -128,13 +130,32 @@ public void stop(Boolean passed) {
128130
}
129131
}
130132

133+
public void stop(Throwable cause) {
134+
if (isDisabled()) {
135+
return;
136+
}
137+
138+
if (this.driver != null) {
139+
annotate("Failure Reason: " + cause.getMessage());
140+
141+
sendStackTrace(cause);
142+
updateResult("failed");
143+
quit();
144+
}
145+
}
146+
131147
/** Ends the session on Sauce Labs and quits the driver without setting a test result. */
132-
public void abort() {
148+
public void abort(Throwable cause) {
133149
if (isDisabled()) {
134150
return;
135151
}
136152

153+
this.result = "complete";
137154
if (this.driver != null) {
155+
annotate("Test Aborted; marking completed instead of failed");
156+
annotate("Reason: " + cause.getMessage());
157+
158+
sendStackTrace(cause);
138159
printToConsole();
139160
quit();
140161
}
@@ -313,6 +334,14 @@ private void updateResult(String result) {
313334
printToConsole();
314335
}
315336

337+
private void sendStackTrace(Throwable cause) {
338+
String stackTrace =
339+
Arrays.stream(cause.getStackTrace())
340+
.map(StackTraceElement::toString)
341+
.collect(Collectors.joining("\n"));
342+
annotate(stackTrace);
343+
}
344+
316345
public void printToConsole() {
317346
// Add output for the Sauce OnDemand Jenkins plugin
318347
// The first print statement will automatically populate links on Jenkins to Sauce

0 commit comments

Comments
 (0)