Skip to content

Commit e69b7cc

Browse files
committed
[junit5] use disable feature in main library instead of extension
1 parent ae90c8d commit e69b7cc

File tree

4 files changed

+65
-86
lines changed

4 files changed

+65
-86
lines changed

java/junit5/pom.xml

Lines changed: 36 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@
6161
<dependency>
6262
<groupId>com.saucelabs</groupId>
6363
<artifactId>sauce_bindings</artifactId>
64-
<version>1.5.0</version>
64+
<version>1.6.0</version>
6565
</dependency>
6666
<dependency>
6767
<groupId>org.projectlombok</groupId>
@@ -95,21 +95,41 @@
9595
<groupId>org.apache.maven.plugins</groupId>
9696
<artifactId>maven-surefire-plugin</artifactId>
9797
<version>3.1.2</version>
98-
<configuration>
99-
<includes>
100-
<include>**/*Test.java</include>
101-
<include>**/*Example.java</include>
102-
</includes>
103-
<properties>
104-
<configurationParameters>
105-
junit.jupiter.execution.parallel.enabled = true
106-
junit.jupiter.execution.parallel.mode.default = concurrent
107-
junit.jupiter.execution.parallel.mode.classes.default = same_thread
108-
junit.jupiter.execution.parallel.config.strategy = fixed
109-
junit.jupiter.execution.parallel.config.fixed.parallelism = ${surefire.parallel}
110-
</configurationParameters>
111-
</properties>
112-
</configuration>
98+
<executions>
99+
<execution>
100+
<id>parallel-tests</id>
101+
<goals>
102+
<goal>test</goal>
103+
</goals>
104+
<configuration>
105+
<properties>
106+
<configurationParameters>
107+
junit.jupiter.execution.parallel.enabled = true
108+
junit.jupiter.execution.parallel.mode.default = concurrent
109+
junit.jupiter.execution.parallel.config.strategy = fixed
110+
junit.jupiter.execution.parallel.config.fixed.parallelism = ${surefire.parallel}
111+
junit.jupiter.execution.parallel.config.fixed.max-pool-size = ${surefire.parallel}
112+
</configurationParameters>
113+
</properties>
114+
</configuration>
115+
</execution>
116+
<execution>
117+
<id>sequential-tests</id>
118+
<goals>
119+
<goal>test</goal>
120+
</goals>
121+
<configuration>
122+
<includes>
123+
<include>**/*Example.java</include>
124+
</includes>
125+
<properties>
126+
<configurationParameters>
127+
junit.jupiter.execution.parallel.enabled = false
128+
</configurationParameters>
129+
</properties>
130+
</configuration>
131+
</execution>
132+
</executions>
113133
</plugin>
114134
<plugin>
115135
<groupId>org.apache.maven.plugins</groupId>

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

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -64,10 +64,6 @@ public SauceBindingsExtension(DataCenter dataCenter) {
6464

6565
@Override
6666
public void beforeEach(ExtensionContext context) {
67-
if (isExtensionDisabled()) {
68-
return;
69-
}
70-
7167
if (sauceOptions.sauce().getName() == null) {
7268
sauceOptions.sauce().setName(context.getDisplayName());
7369
}
@@ -79,10 +75,6 @@ public void beforeEach(ExtensionContext context) {
7975

8076
@Override
8177
public void testSuccessful(ExtensionContext context) {
82-
if (isExtensionDisabled()) {
83-
return;
84-
}
85-
8678
try {
8779
session.stop(true);
8880
} catch (NoSuchSessionException e) {
@@ -105,10 +97,4 @@ public void testFailed(ExtensionContext context, Throwable cause) {
10597
session.stop(false);
10698
}
10799
}
108-
109-
// TODO: Implement this in SauceSession directly
110-
private boolean isExtensionDisabled() {
111-
String value = System.getenv("SAUCE_DISABLED");
112-
return Boolean.parseBoolean(value) || Boolean.getBoolean("sauce.disabled");
113-
}
114100
}

java/junit5/src/test/java/com/saucelabs/saucebindings/junit5/DisabledTest.java

Lines changed: 0 additions & 27 deletions
This file was deleted.

java/junit5/src/test/java/com/saucelabs/saucebindings/junit5/examples/ToggleLocalExample.java

Lines changed: 29 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@
55
import com.saucelabs.saucebindings.junit5.SauceBindingsExtension;
66
import com.saucelabs.saucebindings.options.SauceOptions;
77
import java.time.Duration;
8+
import java.util.List;
89
import org.junit.jupiter.api.AfterAll;
10+
import org.junit.jupiter.api.Assertions;
911
import org.junit.jupiter.api.BeforeAll;
1012
import org.junit.jupiter.api.BeforeEach;
1113
import org.junit.jupiter.api.Test;
@@ -20,40 +22,41 @@ public class ToggleLocalExample {
2022
WebDriver driver;
2123
SauceSession session;
2224

25+
// Allow registering multiple test watchers
2326
@RegisterExtension
2427
static SauceBindingsExtension sauceExtension = new SauceBindingsExtension(getSauceOptions());
2528

2629
@RegisterExtension TestWatcher testWatcher = new LocalTestWatcher();
2730

28-
// To run test on Sauce Labs, change this to "false"
31+
// Run tests with this property set to "false" to execute on Sauce Labs
2932
@BeforeAll
3033
public static void disableSauce() {
3134
System.setProperty("sauce.disabled", "true");
3235
}
3336

34-
@AfterAll
35-
public static void resetSauce() {
36-
System.clearProperty("sauce.disabled");
37-
}
38-
3937
@BeforeEach
4038
public void setup() {
41-
// TODO: Allow getting session even when disabled
42-
if (isSauceEnabled()) {
43-
session = sauceExtension.getSession();
44-
driver = sauceExtension.getDriver();
45-
} else {
39+
session = sauceExtension.getSession();
40+
driver = sauceExtension.getDriver();
41+
if (driver == null) {
4642
driver = new ChromeDriver(getCapabilities());
4743
}
4844
}
4945

5046
@Test
5147
public void localExample() {
52-
// TODO: Allow this method to be ignored if Sauce is disabled
53-
if (isSauceEnabled()) {
54-
session.annotate("Navigating to Swag Labs");
55-
}
48+
// This code executes whether running locally or on Sauce
5649
driver.get("https://www.saucedemo.com/");
50+
51+
// This code executes if Sauce enabled, and is ignored when disabled
52+
Assertions.assertDoesNotThrow(
53+
() -> {
54+
session.annotate("This gets ignored");
55+
session.addTags(List.of("ignored"));
56+
session.stopNetwork();
57+
session.enableLogging();
58+
session.getAccessibilityResults();
59+
});
5760
}
5861

5962
private static SauceOptions getSauceOptions() {
@@ -66,33 +69,30 @@ private static SauceOptions getSauceOptions() {
6669
}
6770

6871
private static ChromeOptions getCapabilities() {
69-
ChromeOptions chromeOptions = new ChromeOptions();
70-
chromeOptions.addArguments("--hide-scrollbars");
71-
72-
return chromeOptions;
73-
}
74-
75-
// TODO: Implement this as a method in SauceSession directly
76-
private boolean isSauceEnabled() {
77-
String value = System.getenv("SAUCE_DISABLED");
78-
return Boolean.parseBoolean(value) || !Boolean.getBoolean("sauce.disabled");
72+
return new ChromeOptions();
7973
}
8074

75+
// Do not quit the driver if running on Sauce Labs
8176
public class LocalTestWatcher implements TestWatcher {
8277
@Override
8378
public void testSuccessful(ExtensionContext context) {
84-
if (!isSauceEnabled()) {
85-
System.out.println("Test Succeeded");
79+
System.out.println("Test Succeeded");
80+
if (SauceSession.isDisabled()) {
8681
driver.quit();
8782
}
8883
}
8984

9085
@Override
9186
public void testFailed(ExtensionContext context, Throwable cause) {
92-
if (!isSauceEnabled()) {
93-
System.out.println("Test Failed");
87+
System.out.println("Test Failed");
88+
if (SauceSession.isDisabled()) {
9489
driver.quit();
9590
}
9691
}
9792
}
93+
94+
@AfterAll
95+
public static void resetSauce() {
96+
System.clearProperty("sauce.disabled");
97+
}
9898
}

0 commit comments

Comments
 (0)