Skip to content
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

WIP - override what user passes in with what is in a config file #345

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
6 changes: 6 additions & 0 deletions java/junit5/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,12 @@
<artifactId>junit-jupiter</artifactId>
<version>5.10.3</version>
</dependency>
<dependency>
<groupId>org.yaml</groupId>
<artifactId>snakeyaml</artifactId>
<version>2.2</version>
<scope>compile</scope>
</dependency>
</dependencies>

<profiles>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,21 @@
import com.saucelabs.saucebindings.CITools;
import com.saucelabs.saucebindings.DataCenter;
import com.saucelabs.saucebindings.SauceSession;
import com.saucelabs.saucebindings.SystemManager;
import com.saucelabs.saucebindings.options.SauceOptions;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.Properties;
import java.util.logging.Logger;

import lombok.SneakyThrows;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.extension.BeforeEachCallback;
import org.junit.jupiter.api.extension.ExtensionContext;
Expand All @@ -20,6 +27,7 @@
import org.openqa.selenium.Capabilities;
import org.openqa.selenium.NoSuchSessionException;
import org.openqa.selenium.WebDriver;
import org.yaml.snakeyaml.Yaml;

public class SauceBindingsExtension implements TestWatcher, BeforeEachCallback, ParameterResolver {
private static final Logger LOGGER = Logger.getLogger(SauceBindingsExtension.class.getName());
Expand All @@ -32,11 +40,35 @@ public SauceBindingsExtension() {
}

private SauceBindingsExtension(SauceOptions sauceOptions, DataCenter dataCenter) {
this.sauceOptions = sauceOptions;
if (Objects.equals(SystemManager.get("SAUCE_CONFIG_OVERRIDE"), "true")) {
File defaultLocation = new File(System.getProperty("user.dir") + "/sauce-config.yml");
this.sauceOptions = optionsFromConfig(defaultLocation);
} else {
this.sauceOptions = sauceOptions;
}

this.dataCenter = dataCenter;
this.build = CITools.getBuildName() + ": " + CITools.getBuildNumber();
}

@SneakyThrows
static SauceOptions optionsFromConfig(File location) {
InputStream input = new FileInputStream(location);
Yaml yaml = new Yaml();
Map<String, Object> data = yaml.load(input);
List<String> topLevelKeys = new ArrayList<>(data.keySet());

String configKey =
System.getProperty(
"sauce.platform.key",
System.getenv().getOrDefault("SAUCE_PLATFORM_KEY", topLevelKeys.get(0)));

Map<String, Object> configData = (Map<String, Object>) data.get(configKey);
SauceOptions options = new SauceOptions();
options.mergeCapabilities(configData);
return options;
}

public void enable() {
System.setProperty("sauce.enabled", "true");
}
Expand Down Expand Up @@ -180,6 +212,12 @@ public Builder withDataCenter(DataCenter dataCenter) {
return this;
}

@SneakyThrows
public Builder withConfigFile(File file) {
this.sauceOptions = optionsFromConfig(file);
return this;
}

public SauceBindingsExtension build() {
return new SauceBindingsExtension(sauceOptions, dataCenter);
}
Expand Down
Loading