Skip to content

[junit5] implement this library with extension instead of superclass #304

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

Merged
merged 12 commits into from
Jul 8, 2024
23 changes: 23 additions & 0 deletions .github/workflows/java.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,17 @@ jobs:
skip-commit: true
- name: Print diffs
run: git --no-pager diff --exit-code
formatting-junit5:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: axel-op/googlejavaformat-action@v3
with:
args: "--replace"
files: "java/junit5/**/*.java"
skip-commit: true
- name: Print diffs
run: git --no-pager diff --exit-code
main:
runs-on: ubuntu-latest
steps:
Expand All @@ -41,6 +52,9 @@ jobs:
uses: actions/setup-java@v1
with:
java-version: 11
- name: Remove driver directories
run: |
sudo rm -rf $CHROMEWEBDRIVER $EDGEWEBDRIVER $GECKOWEBDRIVER
- name: Test with Maven
env:
SAUCE_USERNAME: ${{ secrets.SAUCE_USERNAME }}
Expand All @@ -54,6 +68,9 @@ jobs:
uses: actions/setup-java@v1
with:
java-version: 11
- name: Remove driver directories
run: |
sudo rm -rf $CHROMEWEBDRIVER $EDGEWEBDRIVER $GECKOWEBDRIVER
- name: Test with Maven
env:
SAUCE_USERNAME: ${{ secrets.SAUCE_USERNAME }}
Expand All @@ -67,6 +84,9 @@ jobs:
uses: actions/setup-java@v1
with:
java-version: 11
- name: Remove driver directories
run: |
sudo rm -rf $CHROMEWEBDRIVER $EDGEWEBDRIVER $GECKOWEBDRIVER
- name: Test with Maven
env:
SAUCE_USERNAME: ${{ secrets.SAUCE_USERNAME }}
Expand All @@ -80,6 +100,9 @@ jobs:
# uses: actions/setup-java@v1
# with:
# java-version: 11
# - name: Remove driver directories
# run: |
# sudo rm -rf $CHROMEWEBDRIVER $EDGEWEBDRIVER $GECKOWEBDRIVER
# - name: Test with Maven
# env:
# SAUCE_USERNAME: ${{ secrets.SAUCE_USERNAME }}
Expand Down
15 changes: 11 additions & 4 deletions java/junit5/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,12 @@
<artifactId>sauce_bindings</artifactId>
<version>1.4.0</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.28</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
Expand Down Expand Up @@ -88,16 +94,17 @@
<artifactId>maven-surefire-plugin</artifactId>
<version>3.1.2</version>
<configuration>
<systemPropertyVariables>
<webdriver.http.factory>jdk-http-client</webdriver.http.factory>
</systemPropertyVariables>
<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}
junit.jupiter.execution.parallel.config.fixed.max-pool-size = ${surefire.parallel}
</configurationParameters>
</properties>
</configuration>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,73 +3,73 @@
import com.saucelabs.saucebindings.DataCenter;
import com.saucelabs.saucebindings.SauceSession;
import com.saucelabs.saucebindings.options.SauceOptions;
import java.util.Arrays;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.TestInfo;
import org.junit.jupiter.api.extension.ExtensionContext;
import org.junit.jupiter.api.extension.RegisterExtension;
import org.junit.jupiter.api.extension.TestWatcher;
import org.openqa.selenium.remote.RemoteWebDriver;

import java.util.Arrays;

/** This class is deprecated, use the SauceBindingsExtension */
@Deprecated
public class SauceBaseTest {
protected RemoteWebDriver driver;
protected SauceSession session;
protected DataCenter dataCenter = DataCenter.US_WEST;
protected RemoteWebDriver driver;
protected SauceSession session;
protected DataCenter dataCenter = DataCenter.US_WEST;

@RegisterExtension
public SauceTestWatcher watcher = new SauceTestWatcher();

/**
* This is designed to be able to be overridden in a subclass
*
* @return default instance of SauceOptions
*/
public SauceOptions createSauceOptions() {
return new SauceOptions();
}
@RegisterExtension public SauceTestWatcher watcher = new SauceTestWatcher();

/**
* This method ensures a test name is set by default, and then starts the session
* It creates a session and a driver
*/
@BeforeEach
public void setUp(TestInfo testinfo) {
SauceOptions sauceOptions = createSauceOptions();
if (sauceOptions.sauce().getName() == null) {
sauceOptions.sauce().setName(testinfo.getDisplayName());
}
session = new SauceSession(sauceOptions);
session.setDataCenter(getDataCenter());
driver = session.start();
}
/**
* This is designed to be able to be overridden in a subclass
*
* @return default instance of SauceOptions
*/
public SauceOptions createSauceOptions() {
return new SauceOptions();
}

public DataCenter getDataCenter() {
return this.dataCenter;
/**
* This method ensures a test name is set by default, and then starts the session It creates a
* session and a driver
*/
@BeforeEach
public void setUp(TestInfo testinfo) {
SauceOptions sauceOptions = createSauceOptions();
if (sauceOptions.sauce().getName() == null) {
sauceOptions.sauce().setName(testinfo.getDisplayName());
}
session = new SauceSession(sauceOptions);
session.setDataCenter(getDataCenter());
driver = session.start();
}

/**
* This TestWatcher can be overridden by creating a subclass of the TestWatcher,
* then defining a separate rule in the subclass of the SauceBaseTest class
*/
public class SauceTestWatcher implements TestWatcher {
@Override
public void testSuccessful(ExtensionContext context) {
session.stop(true);
}
public DataCenter getDataCenter() {
return this.dataCenter;
}

@Override
public void testFailed(ExtensionContext context, Throwable cause) {
driver.executeScript("sauce:context=Failure Reason: " + cause.getMessage());
/**
* This TestWatcher can be overridden by creating a subclass of the TestWatcher, then defining a
* separate rule in the subclass of the SauceBaseTest class
*/
public class SauceTestWatcher implements TestWatcher {
@Override
public void testSuccessful(ExtensionContext context) {
session.stop(true);
}

for (Object trace : Arrays.stream(cause.getStackTrace()).toArray()) {
if (trace.toString().contains("sun")) {
break;
}
driver.executeScript("sauce:context=Backtrace: " + trace);
}
@Override
public void testFailed(ExtensionContext context, Throwable cause) {
driver.executeScript("sauce:context=Failure Reason: " + cause.getMessage());

session.stop(false);
for (Object trace : Arrays.stream(cause.getStackTrace()).toArray()) {
if (trace.toString().contains("sun")) {
break;
}
driver.executeScript("sauce:context=Backtrace: " + trace);
}

session.stop(false);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
package com.saucelabs.saucebindings.junit5;

import com.saucelabs.saucebindings.DataCenter;
import com.saucelabs.saucebindings.SauceSession;
import com.saucelabs.saucebindings.options.SauceOptions;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
import java.util.logging.Logger;
import lombok.Getter;
import org.junit.jupiter.api.extension.BeforeEachCallback;
import org.junit.jupiter.api.extension.ExtensionContext;
import org.junit.jupiter.api.extension.TestWatcher;
import org.openqa.selenium.Capabilities;
import org.openqa.selenium.NoSuchSessionException;
import org.openqa.selenium.WebDriver;

public class SauceBindingsExtension implements TestWatcher, BeforeEachCallback {
private static final Logger logger = Logger.getLogger(SauceBindingsExtension.class.getName());
private final SauceOptions sauceOptions;
private final DataCenter dataCenter;
@Getter private SauceSession session;
@Getter private WebDriver driver;

public SauceBindingsExtension() {
this(new SauceOptions(), DataCenter.US_WEST);
}

public SauceBindingsExtension(SauceOptions sauceOptions) {
this(sauceOptions, DataCenter.US_WEST);
}

public SauceBindingsExtension(SauceOptions sauceOptions, DataCenter dataCenter) {
this.sauceOptions = sauceOptions;
this.dataCenter = dataCenter;
}

public SauceBindingsExtension(Capabilities capabilities) {
this(capabilities, DataCenter.US_WEST);
}

public SauceBindingsExtension(Capabilities capabilities, DataCenter dataCenter) {
this.sauceOptions = new SauceOptions();

// TODO: Update Sauce Bindings to handle "sauce:options" the same as it handles "sauce
Map<String, Object> capabilitiesMap = new HashMap<>(capabilities.asMap());
Optional.ofNullable(capabilitiesMap.get("sauce:options"))
.filter(Map.class::isInstance)
.map(Map.class::cast)
.ifPresent(
sauceOptionsMap -> {
capabilitiesMap.put("sauce", sauceOptionsMap);
capabilitiesMap.remove("sauce:options");
});

this.sauceOptions.mergeCapabilities(capabilitiesMap);
this.dataCenter = dataCenter;
}

public SauceBindingsExtension(DataCenter dataCenter) {
this(new SauceOptions(), dataCenter);
}

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

if (sauceOptions.sauce().getName() == null) {
sauceOptions.sauce().setName(context.getDisplayName());
}

session = new SauceSession(sauceOptions);
session.setDataCenter(dataCenter);
driver = session.start();
}

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

try {
session.stop(true);
} catch (NoSuchSessionException e) {
logger.severe(
"Driver quit prematurely; Remove calls to `driver.quit()` to allow SauceBindingsExtension"
+ " to stop the test");
}
}

@Override
public void testFailed(ExtensionContext context, Throwable cause) {
if (session != null) {
session.annotate("Failure Reason: " + cause.getMessage());

Arrays.stream(cause.getStackTrace())
.map(StackTraceElement::toString)
.filter(line -> !line.contains("sun"))
.forEach(session::annotate);

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.

This file was deleted.

Loading
Loading