-
Notifications
You must be signed in to change notification settings - Fork 17
[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
Changes from 9 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
e0de512
[junit5] use Google Java Formatter
titusfortner 3e86815
[junit5] implement this library as an extension
titusfortner c67d25f
[junit5] comparison examples without extension
titusfortner d229a5f
[junit5] update examples and mark places to update sauce bindings
titusfortner ab2609d
Using `getProperty` instead of `getenv`
diemol 777bf6a
Using proper logging
diemol 4f766ea
Formatting file
diemol 22fd320
Improving test setup and teardown
diemol ac51adb
Formatting file
diemol 753745a
Using a recent browser version
diemol cdecd31
Using a recent browser version
diemol 051e114
Disabling test because the feature has not been implemented
diemol File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
114 changes: 114 additions & 0 deletions
114
java/junit5/src/main/java/com/saucelabs/saucebindings/junit5/SauceBindingsExtension.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
diemol marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
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"); | ||
diemol marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return Boolean.parseBoolean(value) || Boolean.getBoolean("sauce.disabled"); | ||
} | ||
} |
27 changes: 0 additions & 27 deletions
27
java/junit5/src/test/java/com/saucelabs/saucebindings/junit5/AddHooksTest.java
This file was deleted.
Oops, something went wrong.
28 changes: 0 additions & 28 deletions
28
java/junit5/src/test/java/com/saucelabs/saucebindings/junit5/AdjustOptionsTest.java
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.