Skip to content

Update JUnit 5 extension implementation for disabling Sauce #322

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 39 additions & 16 deletions java/junit5/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@
<dependency>
<groupId>com.saucelabs</groupId>
<artifactId>sauce_bindings</artifactId>
<version>1.5.0</version>
<version>1.6.0</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
Expand Down Expand Up @@ -95,21 +95,44 @@
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.1.2</version>
<configuration>
<includes>
<include>**/*Test.java</include>
<include>**/*Example.java</include>
</includes>
<properties>
<configurationParameters>
junit.jupiter.execution.parallel.enabled = true
junit.jupiter.execution.parallel.mode.default = concurrent
junit.jupiter.execution.parallel.mode.classes.default = same_thread
junit.jupiter.execution.parallel.config.strategy = fixed
junit.jupiter.execution.parallel.config.fixed.parallelism = ${surefire.parallel}
</configurationParameters>
</properties>
</configuration>
<executions>
<execution>
<id>parallel-tests</id>
<goals>
<goal>test</goal>
</goals>
<configuration>
<includes>
<include>**/*Test.java</include>
</includes>
<properties>
<configurationParameters>
junit.jupiter.execution.parallel.enabled = true
junit.jupiter.execution.parallel.mode.default = concurrent
junit.jupiter.execution.parallel.config.strategy = fixed
junit.jupiter.execution.parallel.config.fixed.parallelism = ${surefire.parallel}
junit.jupiter.execution.parallel.config.fixed.max-pool-size = ${surefire.parallel}
</configurationParameters>
</properties>
</configuration>
</execution>
<execution>
<id>sequential-tests</id>
<goals>
<goal>test</goal>
</goals>
<configuration>
<includes>
<include>**/*Example.java</include>
</includes>
<properties>
<configurationParameters>
junit.jupiter.execution.parallel.enabled = false
</configurationParameters>
</properties>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,6 @@ public SauceBindingsExtension(DataCenter dataCenter) {

@Override
public void beforeEach(ExtensionContext context) {
if (isExtensionDisabled()) {
return;
}

if (sauceOptions.sauce().getName() == null) {
sauceOptions.sauce().setName(context.getDisplayName());
}
Expand All @@ -79,10 +75,6 @@ public void beforeEach(ExtensionContext context) {

@Override
public void testSuccessful(ExtensionContext context) {
if (isExtensionDisabled()) {
return;
}

try {
session.stop(true);
} catch (NoSuchSessionException e) {
Expand All @@ -105,10 +97,4 @@ public void testFailed(ExtensionContext context, Throwable cause) {
session.stop(false);
}
}

// TODO: Implement this in SauceSession directly
private boolean isExtensionDisabled() {
String value = System.getenv("SAUCE_DISABLED");
return Boolean.parseBoolean(value) || Boolean.getBoolean("sauce.disabled");
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
import com.saucelabs.saucebindings.junit5.SauceBindingsExtension;
import com.saucelabs.saucebindings.options.SauceOptions;
import java.time.Duration;
import java.util.List;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
Expand All @@ -20,40 +22,42 @@ public class ToggleLocalExample {
WebDriver driver;
SauceSession session;

// Allow registering multiple test watchers
@RegisterExtension
static SauceBindingsExtension sauceExtension = new SauceBindingsExtension(getSauceOptions());

@RegisterExtension TestWatcher testWatcher = new LocalTestWatcher();

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

@AfterAll
public static void resetSauce() {
System.clearProperty("sauce.disabled");
}

@BeforeEach
public void setup() {
// TODO: Allow getting session even when disabled
if (isSauceEnabled()) {
session = sauceExtension.getSession();
driver = sauceExtension.getDriver();
} else {
session = sauceExtension.getSession();
if (SauceSession.isDisabled()) {
driver = new ChromeDriver(getCapabilities());
} else {
driver = sauceExtension.getDriver();
}
}

@Test
public void localExample() {
// TODO: Allow this method to be ignored if Sauce is disabled
if (isSauceEnabled()) {
session.annotate("Navigating to Swag Labs");
}
// This code executes whether running locally or on Sauce
driver.get("https://www.saucedemo.com/");

// This code executes if Sauce enabled, and is ignored when disabled
Assertions.assertDoesNotThrow(
() -> {
session.annotate("This gets ignored");
session.addTags(List.of("ignored"));
session.stopNetwork();
session.enableLogging();
session.getAccessibilityResults();
});
}

private static SauceOptions getSauceOptions() {
Expand All @@ -66,33 +70,30 @@ private static SauceOptions getSauceOptions() {
}

private static ChromeOptions getCapabilities() {
ChromeOptions chromeOptions = new ChromeOptions();
chromeOptions.addArguments("--hide-scrollbars");

return chromeOptions;
}

// TODO: Implement this as a method in SauceSession directly
private boolean isSauceEnabled() {
String value = System.getenv("SAUCE_DISABLED");
return Boolean.parseBoolean(value) || !Boolean.getBoolean("sauce.disabled");
return new ChromeOptions();
}

// Do not quit the driver if running on Sauce Labs
public class LocalTestWatcher implements TestWatcher {
@Override
public void testSuccessful(ExtensionContext context) {
if (!isSauceEnabled()) {
System.out.println("Test Succeeded");
System.out.println("Test Succeeded");
if (SauceSession.isDisabled()) {
driver.quit();
}
}

@Override
public void testFailed(ExtensionContext context, Throwable cause) {
if (!isSauceEnabled()) {
System.out.println("Test Failed");
System.out.println("Test Failed: " + cause.getMessage());
if (SauceSession.isDisabled()) {
driver.quit();
}
}
}

@AfterAll
public static void resetSauce() {
System.clearProperty("sauce.disabled");
}
}
Loading