Skip to content

Commit

Permalink
Look for Selenium Manager in path defined by Environment Variable (#1…
Browse files Browse the repository at this point in the history
…2752)

This allows people to build or download the binary as necessary and place it in any directory
  • Loading branch information
titusfortner authored Sep 24, 2023
1 parent 553791c commit e5ce6fd
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 74 deletions.
47 changes: 24 additions & 23 deletions dotnet/src/webdriver/SeleniumManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,35 +34,36 @@ namespace OpenQA.Selenium
/// </summary>
public static class SeleniumManager
{
private readonly static string binaryFullPath;
private static readonly string BinaryFullPath = Environment.GetEnvironmentVariable("SE_MANAGER_PATH");

static SeleniumManager()
{
var currentDirectory = AppContext.BaseDirectory;

string binary;
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
if (BinaryFullPath == null)
{
binary = "selenium-manager/windows/selenium-manager.exe";
}
else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
{
binary = "selenium-manager/linux/selenium-manager";
}
else if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
{
binary = "selenium-manager/macos/selenium-manager";
}
else
{
throw new WebDriverException("Selenium Manager did not find supported operating system");
var currentDirectory = AppContext.BaseDirectory;
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
BinaryFullPath = Path.Combine(currentDirectory, "selenium-manager\\windows\\selenium-manager.exe");
}
else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
{
BinaryFullPath = Path.Combine(currentDirectory, "selenium-manager\\linux\\selenium-manager");
}
else if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
{
BinaryFullPath = Path.Combine(currentDirectory, "selenium-manager\\macos\\selenium-manager");
}
else
{
throw new PlatformNotSupportedException(
$"Selenium Manager doesn't support your runtime platform: {RuntimeInformation.OSDescription}");
}
}

binaryFullPath = Path.Combine(currentDirectory, binary);

if (!File.Exists(binaryFullPath))
if (!File.Exists(BinaryFullPath))
{
throw new WebDriverException($"Unable to locate or obtain Selenium Manager binary at {binaryFullPath}");
throw new WebDriverException($"Unable to locate or obtain Selenium Manager binary at {BinaryFullPath}");
}
}

Expand Down Expand Up @@ -102,7 +103,7 @@ public static string DriverPath(DriverOptions options)
}
}

Dictionary<string, object> output = RunCommand(binaryFullPath, argsBuilder.ToString());
Dictionary<string, object> output = RunCommand(BinaryFullPath, argsBuilder.ToString());
string browserPath = (string)output["browser_path"];
string driverPath = (string)output["driver_path"];

Expand Down Expand Up @@ -130,7 +131,7 @@ public static string DriverPath(DriverOptions options)
private static Dictionary<string, object> RunCommand(string fileName, string arguments)
{
Process process = new Process();
process.StartInfo.FileName = binaryFullPath;
process.StartInfo.FileName = BinaryFullPath;
process.StartInfo.Arguments = arguments;
process.StartInfo.UseShellExecute = false;
process.StartInfo.CreateNoWindow = true;
Expand Down
42 changes: 22 additions & 20 deletions java/src/org/openqa/selenium/manager/SeleniumManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import java.nio.file.FileVisitResult;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.SimpleFileVisitor;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.ArrayList;
Expand Down Expand Up @@ -61,11 +62,11 @@ public class SeleniumManager {
private static final Logger LOG = Logger.getLogger(SeleniumManager.class.getName());

private static final String SELENIUM_MANAGER = "selenium-manager";
private static final String EXE = ".exe";

private static volatile SeleniumManager manager;

private Path binary;
private final String managerPath = System.getenv("SE_MANAGER_PATH");
private Path binary = managerPath == null ? null : Paths.get(managerPath);

/** Wrapper for the Selenium Manager binary. */
private SeleniumManager() {
Expand Down Expand Up @@ -158,30 +159,31 @@ private static Result runCommand(Path binary, List<String> arguments) {
*/
private synchronized Path getBinary() {
if (binary == null) {
try {
Platform current = Platform.getCurrent();
String folder = "linux";
String extension = "";
if (current.is(WINDOWS)) {
extension = EXE;
folder = "windows";
} else if (current.is(MAC)) {
folder = "macos";
}
String binaryPath = String.format("%s/%s%s", folder, SELENIUM_MANAGER, extension);
try (InputStream inputStream = this.getClass().getResourceAsStream(binaryPath)) {
Path tmpPath = Files.createTempDirectory(SELENIUM_MANAGER + System.nanoTime());
Platform current = Platform.getCurrent();
String folder = "linux";
String extension = "";
if (current.is(WINDOWS)) {
extension = ".exe";
folder = "windows";
} else if (current.is(MAC)) {
folder = "macos";
}
String binaryPath = String.format("%s/%s%s", folder, SELENIUM_MANAGER, extension);
try (InputStream inputStream = this.getClass().getResourceAsStream(binaryPath)) {
Path tmpPath = Files.createTempDirectory(SELENIUM_MANAGER + System.nanoTime());

deleteOnExit(tmpPath);
deleteOnExit(tmpPath);

binary = tmpPath.resolve(SELENIUM_MANAGER + extension);
Files.copy(inputStream, binary, REPLACE_EXISTING);
}
binary.toFile().setExecutable(true);
binary = tmpPath.resolve(SELENIUM_MANAGER + extension);
Files.copy(inputStream, binary, REPLACE_EXISTING);
} catch (Exception e) {
throw new WebDriverException("Unable to obtain Selenium Manager Binary", e);
}
} else if (!Files.exists(binary)) {
throw new WebDriverException(String.format("Unable to obtain Selenium Manager Binary at: %s", binary));
}
binary.toFile().setExecutable(true);

LOG.fine(String.format("Selenium Manager binary found at: %s", binary));

return binary;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ function getBinary() {

let seleniumManagerBasePath = path.join(__dirname, '..', '/bin')

const filePath = path.join(seleniumManagerBasePath, directory, file)
const filePath = process.env.SE_MANAGER_PATH || path.join(seleniumManagerBasePath, directory, file)

if (!fs.existsSync(filePath)) {
throw new Error(`Unable to obtain Selenium Manager at ${filePath}`)
Expand Down
21 changes: 12 additions & 9 deletions py/selenium/webdriver/common/selenium_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,19 +40,22 @@ def get_binary() -> Path:
:Returns: The Selenium Manager executable location
"""
platform = sys.platform

dirs = {
"darwin": "macos",
"win32": "windows",
"cygwin": "windows",
}
if os.getenv("SE_MANAGER_PATH"):
path = os.getenv("SE_MANAGER_PATH")
else:
platform = sys.platform

directory = dirs.get(platform) if dirs.get(platform) else platform
dirs = {
"darwin": "macos",
"win32": "windows",
"cygwin": "windows",
}

file = "selenium-manager.exe" if directory == "windows" else "selenium-manager"
directory = dirs.get(platform) if dirs.get(platform) else platform
file = "selenium-manager.exe" if directory == "windows" else "selenium-manager"

path = Path(__file__).parent.joinpath(directory, file)
path = Path(__file__).parent.joinpath(directory, file)

if not path.is_file() and os.environ["CONDA_PREFIX"]:
# conda has a separate package selenium-manager, installs in bin
Expand Down
47 changes: 26 additions & 21 deletions rb/lib/selenium/webdriver/common/selenium_manager.rb
Original file line number Diff line number Diff line change
Expand Up @@ -75,31 +75,36 @@ def generate_command(binary, options)
# @return [String] the path to the correct selenium manager
def binary
@binary ||= begin
path = File.expand_path(bin_path, __FILE__)
path << if Platform.windows?
'/windows/selenium-manager.exe'
elsif Platform.mac?
'/macos/selenium-manager'
elsif Platform.linux?
'/linux/selenium-manager'
end
location = File.expand_path(path, __FILE__)

begin
Platform.assert_file(location)
Platform.assert_executable(location)
rescue TypeError
raise Error::WebDriverError,
"Unable to locate or obtain Selenium Manager binary; #{location} is not a valid file object"
rescue Error::WebDriverError => e
raise Error::WebDriverError, "Selenium Manager binary located, but #{e.message}"
end

WebDriver.logger.debug("Selenium Manager binary found at #{location}", id: :selenium_manager)
location = ENV.fetch('SE_MANAGER_PATH', begin
directory = File.expand_path(bin_path, __FILE__)
if Platform.windows?
"#{directory}/windows/selenium-manager.exe"
elsif Platform.mac?
"#{directory}/macos/selenium-manager"
elsif Platform.linux?
"#{directory}/linux/selenium-manager"
end
end)

validate_location(location)
location
end
end

def validate_location(location)
begin
Platform.assert_file(location)
Platform.assert_executable(location)
rescue TypeError
raise Error::WebDriverError,
"Unable to locate or obtain Selenium Manager binary; #{location} is not a valid file object"
rescue Error::WebDriverError => e
raise Error::WebDriverError, "Selenium Manager binary located, but #{e.message}"
end

WebDriver.logger.debug("Selenium Manager binary found at #{location}", id: :selenium_manager)
end

def run(*command)
command += %w[--output json]
command << '--debug' if WebDriver.logger.debug?
Expand Down

0 comments on commit e5ce6fd

Please sign in to comment.