Skip to content

Commit d964028

Browse files
committed
[java] improve Session logic
1 parent ebe674a commit d964028

File tree

1 file changed

+25
-23
lines changed

1 file changed

+25
-23
lines changed

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

Lines changed: 25 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66
import com.saucelabs.saucebindings.options.SauceOptions;
77
import lombok.Getter;
88
import lombok.Setter;
9+
import org.openqa.selenium.Capabilities;
910
import org.openqa.selenium.InvalidArgumentException;
10-
import org.openqa.selenium.MutableCapabilities;
1111
import org.openqa.selenium.remote.RemoteWebDriver;
1212

1313
import java.net.MalformedURLException;
@@ -19,7 +19,7 @@ public class SauceSession {
1919
@Setter private URL sauceUrl;
2020
@Getter private String result;
2121

22-
@Getter private RemoteWebDriver driver;
22+
@Getter protected RemoteWebDriver driver;
2323

2424
public SauceSession() {
2525
this(new SauceOptions());
@@ -40,23 +40,23 @@ public SauceSession(SauceOptions options) {
4040
}
4141

4242
public RemoteWebDriver start() {
43-
driver = createRemoteWebDriver(getSauceUrl(), sauceOptions.toCapabilities());
43+
this.driver = createRemoteWebDriver(getSauceUrl(), sauceOptions.toCapabilities());
4444
return driver;
4545
}
4646

4747
public URL getSauceUrl() {
48-
if (sauceUrl != null) {
49-
return sauceUrl;
50-
} else {
51-
try {
52-
return new URL(dataCenter.getValue());
53-
} catch (MalformedURLException e) {
54-
throw new InvalidArgumentException("Invalid URL");
48+
try {
49+
if (sauceUrl != null) {
50+
return sauceUrl;
51+
} else {
52+
return new URL(getDataCenter().getValue());
5553
}
54+
} catch (MalformedURLException e) {
55+
throw new InvalidArgumentException("Invalid URL", e);
5656
}
5757
}
5858

59-
protected RemoteWebDriver createRemoteWebDriver(URL url, MutableCapabilities capabilities) {
59+
protected RemoteWebDriver createRemoteWebDriver(URL url, Capabilities capabilities) {
6060
return new RemoteWebDriver(url, capabilities);
6161
}
6262

@@ -82,27 +82,29 @@ public void stop(Boolean passed) {
8282
}
8383

8484
public void stop(String result) {
85-
updateResult(result);
86-
stop();
85+
if (this.driver != null) {
86+
updateResult(result);
87+
quit();
88+
}
8789
}
8890

8991
private void updateResult(String result) {
9092
this.result = result;
9193
getDriver().executeScript("sauce:job-result=" + result);
94+
9295
// Add output for the Sauce OnDemand Jenkins plugin
9396
// The first print statement will automatically populate links on Jenkins to Sauce
9497
// The second print statement will output the job link to logging/console
95-
if (this.driver != null) {
96-
String sauceReporter = String.format("SauceOnDemandSessionID=%s job-name=%s", this.driver.getSessionId(), this.sauceOptions.sauce().getName());
97-
String sauceTestLink = String.format("Test Job Link: https://app.saucelabs.com/tests/%s", this.driver.getSessionId());
98-
System.out.print(sauceReporter + "\n" + sauceTestLink + "\n");
99-
}
98+
String sauceReporter = String.format("SauceOnDemandSessionID=%s job-name=%s",
99+
this.driver.getSessionId(),
100+
this.sauceOptions.sauce().getName());
101+
String sauceTestLink = String.format("Test Job Link: https://app.saucelabs.com/tests/%s",
102+
this.driver.getSessionId());
103+
System.out.print(sauceReporter + "\n" + sauceTestLink + "\n");
100104
}
101105

102-
private void stop() {
103-
if(driver !=null) {
104-
driver.quit();
105-
driver = null;
106-
}
106+
private void quit() {
107+
driver.quit();
108+
driver = null;
107109
}
108110
}

0 commit comments

Comments
 (0)