Skip to content

Commit a87d120

Browse files
committed
[junit4] deprecate the superclass and implement with a rule
1 parent b463c74 commit a87d120

21 files changed

+415
-13
lines changed

java/junit4/pom.xml

+7-1
Original file line numberDiff line numberDiff line change
@@ -54,13 +54,19 @@
5454
<dependency>
5555
<groupId>com.saucelabs</groupId>
5656
<artifactId>sauce_bindings</artifactId>
57-
<version>1.5.0</version>
57+
<version>1.6.0</version>
5858
</dependency>
5959
<dependency>
6060
<groupId>junit</groupId>
6161
<artifactId>junit</artifactId>
6262
<version>4.13.2</version>
6363
</dependency>
64+
<dependency>
65+
<groupId>org.projectlombok</groupId>
66+
<artifactId>lombok</artifactId>
67+
<version>1.18.30</version>
68+
<scope>provided</scope>
69+
</dependency>
6470
</dependencies>
6571

6672
<build>

java/junit4/src/main/java/com/saucelabs/saucebindings/junit4/SauceBaseTest.java

+2
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
import org.junit.runner.Description;
1212
import org.openqa.selenium.remote.RemoteWebDriver;
1313

14+
/** This class is deprecated, use the SauceBindingsWatcher Rule */
15+
@Deprecated
1416
public class SauceBaseTest {
1517
protected RemoteWebDriver driver;
1618
protected SauceSession session;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
package com.saucelabs.saucebindings.junit4;
2+
3+
import com.saucelabs.saucebindings.DataCenter;
4+
import com.saucelabs.saucebindings.SauceSession;
5+
import com.saucelabs.saucebindings.options.SauceOptions;
6+
import java.util.Arrays;
7+
import java.util.HashMap;
8+
import java.util.Map;
9+
import java.util.Optional;
10+
import java.util.logging.Logger;
11+
import lombok.Getter;
12+
import org.junit.rules.TestWatcher;
13+
import org.junit.runner.Description;
14+
import org.openqa.selenium.Capabilities;
15+
import org.openqa.selenium.NoSuchSessionException;
16+
import org.openqa.selenium.WebDriver;
17+
18+
@Getter
19+
public class SauceBindingsWatcher extends TestWatcher {
20+
private static final Logger logger = Logger.getLogger(SauceBindingsWatcher.class.getName());
21+
private final SauceOptions sauceOptions;
22+
private final DataCenter dataCenter;
23+
private SauceSession session;
24+
private WebDriver driver;
25+
26+
public SauceBindingsWatcher() {
27+
this(new SauceOptions(), DataCenter.US_WEST);
28+
}
29+
30+
public SauceBindingsWatcher(SauceOptions sauceOptions) {
31+
this(sauceOptions, DataCenter.US_WEST);
32+
}
33+
34+
public SauceBindingsWatcher(SauceOptions sauceOptions, DataCenter dataCenter) {
35+
this.sauceOptions = sauceOptions;
36+
this.dataCenter = dataCenter;
37+
}
38+
39+
public SauceBindingsWatcher(Capabilities capabilities) {
40+
this(capabilities, DataCenter.US_WEST);
41+
}
42+
43+
public SauceBindingsWatcher(Capabilities capabilities, DataCenter dataCenter) {
44+
this.sauceOptions = new SauceOptions();
45+
Map<String, Object> capabilitiesMap = new HashMap<>(capabilities.asMap());
46+
Optional.ofNullable(capabilitiesMap.get("sauce:options"))
47+
.filter(Map.class::isInstance)
48+
.map(Map.class::cast)
49+
.ifPresent(
50+
sauceOptionsMap -> {
51+
capabilitiesMap.put("sauce", sauceOptionsMap);
52+
capabilitiesMap.remove("sauce:options");
53+
});
54+
this.sauceOptions.mergeCapabilities(capabilitiesMap);
55+
this.dataCenter = dataCenter;
56+
}
57+
58+
public SauceBindingsWatcher(DataCenter dataCenter) {
59+
this(new SauceOptions(), dataCenter);
60+
}
61+
62+
@Override
63+
protected void starting(Description description) {
64+
if (sauceOptions.sauce().getName() == null) {
65+
sauceOptions.sauce().setName(description.getMethodName());
66+
}
67+
68+
session = new SauceSession(sauceOptions);
69+
session.setDataCenter(dataCenter);
70+
driver = session.start();
71+
}
72+
73+
@Override
74+
protected void succeeded(Description description) {
75+
try {
76+
session.stop(true);
77+
} catch (NoSuchSessionException e) {
78+
logger.severe(
79+
"Driver quit prematurely; Remove calls to `driver.quit()` to allow"
80+
+ " SauceBindingsExtension to stop the test");
81+
}
82+
}
83+
84+
@Override
85+
protected void failed(Throwable e, Description description) {
86+
if (session != null) {
87+
try {
88+
session.annotate("Failure Reason: " + e.getMessage());
89+
90+
Arrays.stream(e.getStackTrace())
91+
.map(StackTraceElement::toString)
92+
.filter(line -> !line.contains("sun"))
93+
.forEach(session::annotate);
94+
95+
session.stop(false);
96+
} catch (NoSuchSessionException ex) {
97+
logger.severe(
98+
"Driver quit prematurely; Remove calls to `driver.quit()` to allow SauceBindingsExtension"
99+
+ " to stop the test");
100+
}
101+
}
102+
}
103+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package com.saucelabs.saucebindings.junit4.examples;
2+
3+
import com.deque.html.axecore.results.Results;
4+
import com.saucelabs.saucebindings.SauceSession;
5+
import com.saucelabs.saucebindings.junit4.SauceBindingsWatcher;
6+
import org.junit.Assert;
7+
import org.junit.Before;
8+
import org.junit.Rule;
9+
import org.junit.Test;
10+
import org.openqa.selenium.WebDriver;
11+
12+
public class AccessibilityExample {
13+
private SauceSession session;
14+
private WebDriver driver;
15+
16+
// 1. Use the SauceBindingsWatcher rule
17+
@Rule public SauceBindingsWatcher sauceWatcher = new SauceBindingsWatcher();
18+
19+
// 2. Get variables created by Watcher
20+
@Before
21+
public void storeVariables() {
22+
this.session = sauceWatcher.getSession();
23+
this.driver = sauceWatcher.getDriver();
24+
}
25+
26+
@Test
27+
public void startSession() {
28+
// 3. Use the driver in your tests just like normal
29+
driver.get("https://www.saucedemo.com/");
30+
31+
// 4. Get and assert on accessibility results
32+
Results results = session.getAccessibilityResults();
33+
Assert.assertEquals(3, results.getViolations().size());
34+
35+
// 5. Session is stopped and results are sent to Sauce Labs automatically by the superclass
36+
}
37+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package com.saucelabs.saucebindings.junit4.examples;
2+
3+
import com.saucelabs.saucebindings.SauceSession;
4+
import com.saucelabs.saucebindings.junit4.SauceBindingsWatcher;
5+
import java.util.HashMap;
6+
import java.util.Map;
7+
import org.junit.Before;
8+
import org.junit.Rule;
9+
import org.junit.Test;
10+
import org.openqa.selenium.Capabilities;
11+
import org.openqa.selenium.WebDriver;
12+
import org.openqa.selenium.safari.SafariOptions;
13+
14+
public class CapabilitiesExample {
15+
WebDriver driver;
16+
SauceSession session;
17+
18+
// 1. Create Selenium Capabilities instance in static method
19+
private static Capabilities getCapabilities() {
20+
SafariOptions browserOptions = new SafariOptions();
21+
browserOptions.setPlatformName("macOS 12");
22+
browserOptions.setBrowserVersion("latest");
23+
Map<String, Object> sauceOptions = new HashMap<>();
24+
sauceOptions.put("idleTimeout", 30);
25+
browserOptions.setCapability("sauce:options", sauceOptions);
26+
return browserOptions;
27+
}
28+
29+
// 2. Pass these options to the SauceBindingsWatcher rule
30+
@Rule public SauceBindingsWatcher sauceWatcher = new SauceBindingsWatcher(getCapabilities());
31+
32+
// 3. Get variables created by Watcher
33+
@Before
34+
public void storeVariables() {
35+
this.session = sauceWatcher.getSession();
36+
this.driver = sauceWatcher.getDriver();
37+
}
38+
39+
@Test
40+
public void basicOptions() {
41+
// 4. Use the session instance to do Sauce Labs things
42+
session.annotate("Navigating to Swag Labs");
43+
44+
// 5. Use the driver instance to do Selenium things
45+
driver.get("https://www.saucedemo.com/");
46+
47+
// 6. Watcher does all teardown activities
48+
}
49+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package com.saucelabs.saucebindings.junit4.examples;
2+
3+
import com.saucelabs.saucebindings.DataCenter;
4+
import com.saucelabs.saucebindings.SauceSession;
5+
import com.saucelabs.saucebindings.junit4.SauceBindingsWatcher;
6+
import org.junit.Before;
7+
import org.junit.Rule;
8+
import org.junit.Test;
9+
import org.openqa.selenium.WebDriver;
10+
11+
public class DataCenterExample {
12+
private SauceSession session;
13+
private WebDriver driver;
14+
15+
// 1. Pass in desired Datacenter to the SauceBindingsWatcher rule
16+
@Rule public SauceBindingsWatcher sauceWatcher = new SauceBindingsWatcher(DataCenter.EU_CENTRAL);
17+
18+
// 2. Get variables created by Watcher
19+
@Before
20+
public void storeVariables() {
21+
this.session = sauceWatcher.getSession();
22+
this.driver = sauceWatcher.getDriver();
23+
}
24+
25+
@Test
26+
public void startSession() {
27+
// 3. Use the session instance to do Sauce Labs things
28+
session.annotate("Navigating to Swag Labs");
29+
30+
// 4. Use the driver instance to do Selenium things
31+
driver.get("https://www.saucedemo.com/");
32+
33+
// 5. Watcher does all teardown activities
34+
}
35+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package com.saucelabs.saucebindings.junit4.examples;
2+
3+
import com.saucelabs.saucebindings.SauceSession;
4+
import com.saucelabs.saucebindings.junit4.SauceBindingsWatcher;
5+
import org.junit.Before;
6+
import org.junit.Rule;
7+
import org.junit.Test;
8+
import org.openqa.selenium.WebDriver;
9+
10+
public class QuickStartExample {
11+
private SauceSession session;
12+
private WebDriver driver;
13+
14+
// 1. Use the SauceBindingsWatcher rule
15+
@Rule public SauceBindingsWatcher sauceWatcher = new SauceBindingsWatcher();
16+
17+
// 2. Get variables created by Watcher
18+
@Before
19+
public void storeVariables() {
20+
this.session = sauceWatcher.getSession();
21+
this.driver = sauceWatcher.getDriver();
22+
}
23+
24+
@Test
25+
public void startSession() {
26+
// 3. Use the session instance to do Sauce Labs things
27+
session.annotate("Navigating to Swag Labs");
28+
29+
// 4. Use the driver instance to do Selenium things
30+
driver.get("https://www.saucedemo.com/");
31+
32+
// 5. Watcher does all teardown activities
33+
}
34+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package com.saucelabs.saucebindings.junit4.examples;
2+
3+
import com.saucelabs.saucebindings.SaucePlatform;
4+
import com.saucelabs.saucebindings.SauceSession;
5+
import com.saucelabs.saucebindings.UnhandledPromptBehavior;
6+
import com.saucelabs.saucebindings.junit4.SauceBindingsWatcher;
7+
import com.saucelabs.saucebindings.options.SauceOptions;
8+
import java.time.Duration;
9+
import org.junit.Before;
10+
import org.junit.Rule;
11+
import org.junit.Test;
12+
import org.openqa.selenium.WebDriver;
13+
14+
public class SauceOptionsExample {
15+
WebDriver driver;
16+
SauceSession session;
17+
18+
// 1. Create SauceOptions instance in static method
19+
public static SauceOptions createSauceOptions() {
20+
return SauceOptions.firefox()
21+
.setBrowserVersion("127.0")
22+
.setPlatformName(SaucePlatform.WINDOWS_10)
23+
.setUnhandledPromptBehavior(UnhandledPromptBehavior.IGNORE)
24+
.setIdleTimeout(Duration.ofSeconds(45))
25+
.setTimeZone("Alaska")
26+
.build();
27+
}
28+
29+
// 2. Pass these options to the SauceBindingsWatcher rule
30+
@Rule public SauceBindingsWatcher sauceWatcher = new SauceBindingsWatcher(createSauceOptions());
31+
32+
// 3. Get variables created by Watcher
33+
@Before
34+
public void storeVariables() {
35+
this.session = sauceWatcher.getSession();
36+
this.driver = sauceWatcher.getDriver();
37+
}
38+
39+
@Test
40+
public void basicOptions() {
41+
// 4. Use the session instance to do Sauce Labs things
42+
session.annotate("Navigating to Swag Labs");
43+
44+
// 5. Use the driver instance to do Selenium things
45+
driver.get("https://www.saucedemo.com/");
46+
47+
// 6. Watcher does all teardown activities
48+
}
49+
}

0 commit comments

Comments
 (0)