Skip to content

Commit

Permalink
[java][sm] Configure Selenium Manager environment from System Propert…
Browse files Browse the repository at this point in the history
…ies (#13858)

* [java][sm] Configure Selenium Manager environment from System Properties

* Running format script

---------

Co-authored-by: Diego Molina <diemol@users.noreply.github.com>
Co-authored-by: Diego Molina <diemol@gmail.com>
  • Loading branch information
3 people committed May 6, 2024
1 parent fe2edbd commit 7b83fc1
Showing 1 changed file with 22 additions and 7 deletions.
29 changes: 22 additions & 7 deletions java/src/org/openqa/selenium/manager/SeleniumManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import java.nio.file.Paths;
import java.time.Duration;
import java.util.List;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.openqa.selenium.Beta;
Expand Down Expand Up @@ -62,6 +63,7 @@ public class SeleniumManager {
private static final String CACHE_PATH_ENV = "SE_CACHE_PATH";
private static final String BETA_PREFIX = "0.";
private static final String EXE = ".exe";
private static final String SE_ENV_PREFIX = "SE_";

private static volatile SeleniumManager manager;
private final String managerPath = System.getenv("SE_MANAGER_PATH");
Expand Down Expand Up @@ -119,8 +121,21 @@ private static Result runCommand(Path binary, List<String> arguments) {
String output;
int code;
try {
ExternalProcess.Builder processBuilder = ExternalProcess.builder();

Properties properties = System.getProperties();
for (String name : properties.stringPropertyNames()) {
if (name.startsWith(SE_ENV_PREFIX)) {
// read property with 'default' value due to concurrency
String value = properties.getProperty(name, "");
if (!value.isEmpty()) {
processBuilder.environment(name, value);
}
}
}
ExternalProcess process =
ExternalProcess.builder().command(binary.toAbsolutePath().toString(), arguments).start();
processBuilder.command(binary.toAbsolutePath().toString(), arguments).start();

if (!process.waitFor(Duration.ofHours(1))) {
LOG.warning("Selenium Manager did not exit, shutting it down");
process.shutdown();
Expand Down Expand Up @@ -240,13 +255,13 @@ private Level getLogLevel() {
}

private Path getBinaryInCache(String binaryName) throws IOException {
String cachePath = DEFAULT_CACHE_PATH.replace(HOME, System.getProperty("user.home"));

// Look for cache path as env
String cachePathEnv = System.getenv(CACHE_PATH_ENV);
if (cachePathEnv != null) {
cachePath = cachePathEnv;
}
// Look for cache path as system property or env
String cachePath = System.getProperty(CACHE_PATH_ENV, "");
if (cachePath.isEmpty()) cachePath = System.getenv(CACHE_PATH_ENV);
if (cachePath == null) cachePath = DEFAULT_CACHE_PATH;

cachePath = cachePath.replace(HOME, System.getProperty("user.home"));

// If cache path is not writable, SM will be extracted to a temporal folder
Path cacheParent = Paths.get(cachePath);
Expand Down

0 comments on commit 7b83fc1

Please sign in to comment.