Skip to content

Commit aba2b52

Browse files
committed
[junit4] update watcher with latest sauce bindings improvements
1 parent 19643b9 commit aba2b52

File tree

9 files changed

+89
-94
lines changed

9 files changed

+89
-94
lines changed

java/junit4/pom.xml

+11-29
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646

4747
<properties>
4848
<saucebindings-junit4.version>2.0.0-beta.1-SNAPSHOT</saucebindings-junit4.version>
49+
<surefire.parallel>8</surefire.parallel>
4950
<maven.compiler.source>11</maven.compiler.source>
5051
<maven.compiler.target>11</maven.compiler.target>
5152
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
@@ -95,34 +96,15 @@
9596
<groupId>org.apache.maven.plugins</groupId>
9697
<artifactId>maven-surefire-plugin</artifactId>
9798
<version>2.22.1</version>
98-
<executions>
99-
<execution>
100-
<id>sauce-enabled</id>
101-
<goals>
102-
<goal>test</goal>
103-
</goals>
104-
<configuration>
105-
<parallel>all</parallel>
106-
<threadCountMethods>50</threadCountMethods>
107-
<useUnlimitedThreads>true</useUnlimitedThreads>
108-
<redirectTestOutputToFile>false</redirectTestOutputToFile>
109-
</configuration>
110-
</execution>
111-
<execution>
112-
<id>sauce-disabled</id>
113-
<goals>
114-
<goal>test</goal>
115-
</goals>
116-
<configuration>
117-
<systemPropertyVariables>
118-
<sauce.disabled>true</sauce.disabled>
119-
</systemPropertyVariables>
120-
<includes>
121-
<include>**/ToggleTest.java</include>
122-
</includes>
123-
</configuration>
124-
</execution>
125-
</executions>
99+
<configuration>
100+
<includes>
101+
<include>**/*Example.java</include>
102+
</includes>
103+
<parallel>all</parallel>
104+
<threadCountMethods>${surefire.parallel}</threadCountMethods>
105+
<useUnlimitedThreads>true</useUnlimitedThreads>
106+
<redirectTestOutputToFile>false</redirectTestOutputToFile>
107+
</configuration>
126108
</plugin>
127109
<plugin>
128110
<groupId>org.apache.maven.plugins</groupId>
@@ -170,7 +152,7 @@
170152
<dependency>
171153
<groupId>org.apache.maven.scm</groupId>
172154
<artifactId>maven-scm-provider-gitexe</artifactId>
173-
<version>2.1.0</version>
155+
<version>2.0.1</version>
174156
</dependency>
175157
</dependencies>
176158
</plugin>

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

+20-29
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,10 @@
88
import java.io.InputStream;
99
import java.lang.annotation.Annotation;
1010
import java.util.ArrayList;
11-
import java.util.Arrays;
1211
import java.util.List;
12+
import java.util.Objects;
1313
import java.util.Properties;
1414
import java.util.logging.Logger;
15-
import java.util.stream.Collectors;
1615
import lombok.Getter;
1716
import org.junit.AssumptionViolatedException;
1817
import org.junit.experimental.categories.Category;
@@ -29,7 +28,7 @@ public class SauceBindingsWatcher extends TestWatcher {
2928
private final DataCenter dataCenter;
3029
private SauceSession session;
3130
private WebDriver driver;
32-
private static final String buildName = CITools.getBuildName() + ": " + CITools.getBuildNumber();
31+
private static final String build = CITools.getBuildName() + ": " + CITools.getBuildNumber();
3332

3433
public SauceBindingsWatcher() {
3534
this(new SauceOptions(), DataCenter.US_WEST);
@@ -44,21 +43,29 @@ public static Builder builder() {
4443
return new Builder();
4544
}
4645

46+
public static void enable() {
47+
System.setProperty("sauce.enabled", "true");
48+
}
49+
4750
@Override
4851
protected void starting(Description description) {
49-
updateOptions(description);
52+
sauceOptions.sauce().setBuild(build);
53+
updateTestName(description);
54+
updateCustomData();
55+
updateTags(description);
5056

5157
session = new SauceSession(sauceOptions);
5258
session.setDataCenter(dataCenter);
5359
driver = session.start();
5460
}
5561

56-
private void updateOptions(Description description) {
62+
public void updateTestName(Description description) {
5763
String className = description.getClassName();
5864
String simpleClassName = className.substring(className.lastIndexOf(".") + 1);
5965
sauceOptions.sauce().setName(simpleClassName + ": " + description.getMethodName());
60-
sauceOptions.sauce().setBuild(buildName);
66+
}
6167

68+
private void updateCustomData() {
6269
Properties prop = new Properties();
6370
try (InputStream input = getClass().getResourceAsStream("/app.properties")) {
6471
prop.load(input);
@@ -67,23 +74,23 @@ private void updateOptions(Description description) {
6774
} catch (IOException ignored) {
6875
sauceOptions.sauce().getCustomData().put("sauce-bindings-junit4", "unknown");
6976
}
77+
}
7078

71-
List<String> tags = sauceOptions.sauce().getTags();
79+
private void updateTags(Description description) {
80+
List<String> tags =
81+
Objects.requireNonNullElseGet(sauceOptions.sauce().getTags(), ArrayList::new);
7282

7383
List<Annotation> annotations = (List<Annotation>) description.getAnnotations();
7484
for (Annotation annotation : annotations) {
7585
if (annotation instanceof Category) {
7686
Category category = (Category) annotation;
7787
for (Class<?> categoryClass : category.value()) {
78-
if (tags == null) {
79-
tags = new ArrayList<>();
80-
}
8188
tags.add(categoryClass.getSimpleName());
8289
}
8390
}
8491
}
8592

86-
if (tags != null) {
93+
if (!tags.isEmpty()) {
8794
sauceOptions.sauce().setTags(tags);
8895
}
8996
}
@@ -103,14 +110,7 @@ protected void succeeded(Description description) {
103110
protected void failed(Throwable e, Description description) {
104111
if (session != null) {
105112
try {
106-
session.annotate("Failure Reason: " + e.getMessage());
107-
108-
Arrays.stream(e.getStackTrace())
109-
.map(StackTraceElement::toString)
110-
.filter(line -> !line.contains("sun"))
111-
.forEach(session::annotate);
112-
113-
session.stop(false);
113+
session.stop(e);
114114
} catch (NoSuchSessionException ex) {
115115
LOGGER.severe(
116116
"Driver quit prematurely; Remove calls to `driver.quit()` to allow"
@@ -123,16 +123,7 @@ protected void failed(Throwable e, Description description) {
123123
public void skipped(AssumptionViolatedException e, Description description) {
124124
LOGGER.fine("Test Aborted: " + e.getMessage());
125125
if (session != null) {
126-
session.annotate("Test Skipped; marking completed instead of failed");
127-
session.annotate("Reason: " + e.getMessage());
128-
129-
String stackTrace =
130-
Arrays.stream(e.getStackTrace())
131-
.map(StackTraceElement::toString)
132-
.collect(Collectors.joining("\n"));
133-
session.annotate(stackTrace);
134-
135-
session.abort();
126+
session.abort(e);
136127
}
137128
}
138129

java/junit4/src/test/java/com/saucelabs/saucebindings/junit4/ToggleTest.java

+1-5
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,6 @@ public class ToggleTest {
1515

1616
@Rule public SauceBindingsWatcher sauceWatcher = new SauceBindingsWatcher();
1717

18-
static {
19-
System.setProperty("sauce.disabled", "true");
20-
}
21-
2218
@Before
2319
public void storeVariables() {
2420
this.session = sauceWatcher.getSession();
@@ -27,7 +23,7 @@ public void storeVariables() {
2723

2824
@Test
2925
public void disableSauce() {
30-
Assume.assumeTrue("true".equals(System.getProperty("sauce.disabled")));
26+
Assume.assumeFalse(Boolean.getBoolean("sauce.enabled"));
3127

3228
Assert.assertNull(driver);
3329

java/junit4/src/test/java/com/saucelabs/saucebindings/junit4/examples/AccessibilityExample.java

+9-4
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,12 @@ public class AccessibilityExample {
1616
// 1. Use the SauceBindingsWatcher rule
1717
@Rule public SauceBindingsWatcher sauceWatcher = new SauceBindingsWatcher();
1818

19-
// 2. Get variables created by Watcher
19+
// 2. Enable SauceBindingsWatcher rule
20+
static {
21+
SauceBindingsWatcher.enable();
22+
}
23+
24+
// 3. Get variables created by Watcher
2025
@Before
2126
public void storeVariables() {
2227
this.session = sauceWatcher.getSession();
@@ -25,13 +30,13 @@ public void storeVariables() {
2530

2631
@Test
2732
public void startSession() {
28-
// 3. Use the driver in your tests just like normal
33+
// 4. Use the driver in your tests just like normal
2934
driver.get("https://www.saucedemo.com/");
3035

31-
// 4. Get and assert on accessibility results
36+
// 5. Get and assert on accessibility results
3237
Results results = session.getAccessibilityResults();
3338
Assert.assertEquals(3, results.getViolations().size());
3439

35-
// 5. Session is stopped and results are sent to Sauce Labs automatically by the superclass
40+
// 6. Session is stopped and results are sent to Sauce Labs automatically by the superclass
3641
}
3742
}

java/junit4/src/test/java/com/saucelabs/saucebindings/junit4/examples/CapabilitiesExample.java

+9-4
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,12 @@ private static Capabilities createCapabilities() {
3131
public SauceBindingsWatcher sauceWatcher =
3232
SauceBindingsWatcher.builder().withCapabilities(createCapabilities()).build();
3333

34-
// 3. Get variables created by Watcher
34+
// 3. Enable SauceBindingsWatcher rule
35+
static {
36+
SauceBindingsWatcher.enable();
37+
}
38+
39+
// 4. Get variables created by Watcher
3540
@Before
3641
public void storeVariables() {
3742
this.session = sauceWatcher.getSession();
@@ -40,12 +45,12 @@ public void storeVariables() {
4045

4146
@Test
4247
public void basicOptions() {
43-
// 4. Use the session instance to do Sauce Labs things
48+
// 5. Use the session instance to do Sauce Labs things
4449
session.annotate("Navigating to Swag Labs");
4550

46-
// 5. Use the driver instance to do Selenium things
51+
// 6. Use the driver instance to do Selenium things
4752
driver.get("https://www.saucedemo.com/");
4853

49-
// 6. Watcher does all teardown activities
54+
// 7. Watcher does all teardown activities
5055
}
5156
}

java/junit4/src/test/java/com/saucelabs/saucebindings/junit4/examples/DataCenterExample.java

+9-4
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,12 @@ public class DataCenterExample {
1717
public SauceBindingsWatcher sauceWatcher =
1818
SauceBindingsWatcher.builder().withDataCenter(DataCenter.EU_CENTRAL).build();
1919

20-
// 2. Get variables created by Watcher
20+
// 2. Enable SauceBindingsWatcher rule
21+
static {
22+
SauceBindingsWatcher.enable();
23+
}
24+
25+
// 3. Get variables created by Watcher
2126
@Before
2227
public void storeVariables() {
2328
this.session = sauceWatcher.getSession();
@@ -26,12 +31,12 @@ public void storeVariables() {
2631

2732
@Test
2833
public void startSession() {
29-
// 3. Use the session instance to do Sauce Labs things
34+
// 4. Use the session instance to do Sauce Labs things
3035
session.annotate("Navigating to Swag Labs");
3136

32-
// 4. Use the driver instance to do Selenium things
37+
// 5. Use the driver instance to do Selenium things
3338
driver.get("https://www.saucedemo.com/");
3439

35-
// 5. Watcher does all teardown activities
40+
// 6. Watcher does all teardown activities
3641
}
3742
}

java/junit4/src/test/java/com/saucelabs/saucebindings/junit4/examples/QuickStartExample.java

+9-4
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,12 @@ public class QuickStartExample {
1414
// 1. Use the SauceBindingsWatcher rule
1515
@Rule public SauceBindingsWatcher sauceWatcher = new SauceBindingsWatcher();
1616

17-
// 2. Get variables created by Watcher
17+
// 2. Enable SauceBindingsWatcher rule
18+
static {
19+
SauceBindingsWatcher.enable();
20+
}
21+
22+
// 3. Get variables created by Watcher
1823
@Before
1924
public void storeVariables() {
2025
this.session = sauceWatcher.getSession();
@@ -23,12 +28,12 @@ public void storeVariables() {
2328

2429
@Test
2530
public void startSession() {
26-
// 3. Use the session instance to do Sauce Labs things
31+
// 4. Use the session instance to do Sauce Labs things
2732
session.annotate("Navigating to Swag Labs");
2833

29-
// 4. Use the driver instance to do Selenium things
34+
// 5. Use the driver instance to do Selenium things
3035
driver.get("https://www.saucedemo.com/");
3136

32-
// 5. Watcher does all teardown activities
37+
// 6. Watcher does all teardown activities
3338
}
3439
}

java/junit4/src/test/java/com/saucelabs/saucebindings/junit4/examples/ToggleLocalExample.java renamed to java/junit4/src/test/java/com/saucelabs/saucebindings/junit4/examples/RunLocalExample.java

+12-11
Original file line numberDiff line numberDiff line change
@@ -3,33 +3,34 @@
33
import com.saucelabs.saucebindings.SauceSession;
44
import com.saucelabs.saucebindings.junit4.SauceBindingsWatcher;
55
import org.junit.Before;
6-
import org.junit.BeforeClass;
76
import org.junit.Rule;
87
import org.junit.Test;
98
import org.junit.rules.TestWatcher;
109
import org.junit.runner.Description;
1110
import org.openqa.selenium.WebDriver;
1211
import org.openqa.selenium.chrome.ChromeDriver;
1312

14-
public class ToggleLocalExample {
13+
public class RunLocalExample {
1514
private SauceSession session;
1615
private WebDriver driver;
1716

18-
// Change this property to "true" to run locally
19-
@BeforeClass
20-
public static void disableSauce() {
21-
System.setProperty("sauce.disabled", "false");
22-
}
23-
2417
// 1. Set multiple rules for when Sauce is and isn't enabled
2518
@Rule public SauceBindingsWatcher sauceWatcher = new SauceBindingsWatcher();
2619
@Rule public TestWatcher localWatcher = new LocalTestWatcher();
2720

21+
// Sauce Labs execution is disabled by default,
22+
// To run tests without Sauce, do not enable the watcher (`SauceBindingsWatcher.enable()`)
23+
// and do not execute with `-Dsauce.enabled=true`
24+
static {
25+
System.out.println("Sauce Bindings Extension not Enabled");
26+
// SauceBindingsWatcher.enable();
27+
}
28+
2829
// 2. Start driver if running locally
2930
@Before
3031
public void storeVariables() {
3132
this.session = sauceWatcher.getSession();
32-
this.driver = SauceSession.isDisabled() ? new ChromeDriver() : sauceWatcher.getDriver();
33+
this.driver = SauceSession.isEnabled() ? sauceWatcher.getDriver(): new ChromeDriver();
3334
}
3435

3536
@Test
@@ -44,15 +45,15 @@ public class LocalTestWatcher extends TestWatcher {
4445
@Override
4546
public void succeeded(Description description) {
4647
System.out.println("Test Succeeded");
47-
if (SauceSession.isDisabled()) {
48+
if (!SauceSession.isEnabled()) {
4849
driver.quit();
4950
}
5051
}
5152

5253
@Override
5354
public void failed(Throwable e, Description description) {
5455
System.out.println("Test Failed: " + e.getMessage());
55-
if (SauceSession.isDisabled()) {
56+
if (!SauceSession.isEnabled()) {
5657
driver.quit();
5758
}
5859
}

0 commit comments

Comments
 (0)