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

[dotnet] Lazy-load Selenium manager binary location #14639

Merged
merged 8 commits into from
Oct 24, 2024
25 changes: 12 additions & 13 deletions dotnet/src/webdriver/SeleniumManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,27 +36,25 @@ public static class SeleniumManager
{
private static readonly ILogger _logger = Log.GetLogger(typeof(SeleniumManager));

private static readonly string BinaryFullPath = Environment.GetEnvironmentVariable("SE_MANAGER_PATH");

private static readonly JsonSerializerOptions _serializerOptions = new() { PropertyNameCaseInsensitive = true, TypeInfoResolver = SeleniumManagerSerializerContext.Default };

static SeleniumManager()
private static readonly Lazy<string> _lazyBinaryFullPath = new(() =>
{

if (BinaryFullPath == null)
string? binaryFullPath = Environment.GetEnvironmentVariable("SE_MANAGER_PATH");
nvborisenko marked this conversation as resolved.
Show resolved Hide resolved
if (binaryFullPath == null)
{
var currentDirectory = AppContext.BaseDirectory;
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
BinaryFullPath = Path.Combine(currentDirectory, "selenium-manager", "windows", "selenium-manager.exe");
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");
binaryFullPath = Path.Combine(currentDirectory, "selenium-manager", "linux", "selenium-manager");
}
else if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
{
BinaryFullPath = Path.Combine(currentDirectory, "selenium-manager", "macos", "selenium-manager");
binaryFullPath = Path.Combine(currentDirectory, "selenium-manager", "macos", "selenium-manager");
}
else
{
Expand All @@ -65,11 +63,12 @@ static SeleniumManager()
}
}

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}");
}
}
return binaryFullPath;
});

/// <summary>
/// Determines the location of the browser and driver binaries.
Expand All @@ -88,7 +87,7 @@ public static Dictionary<string, string> BinaryPaths(string arguments)
argsBuilder.Append(" --debug");
}

var smCommandResult = RunCommand(BinaryFullPath, argsBuilder.ToString());
var smCommandResult = RunCommand(_lazyBinaryFullPath.Value, argsBuilder.ToString());
Dictionary<string, string> binaryPaths = new()
{
{ "browser_path", smCommandResult.BrowserPath },
Expand All @@ -115,7 +114,7 @@ public static Dictionary<string, string> BinaryPaths(string arguments)
private static SeleniumManagerResponse.ResultResponse RunCommand(string fileName, string arguments)
{
Process process = new Process();
process.StartInfo.FileName = BinaryFullPath;
process.StartInfo.FileName = _lazyBinaryFullPath.Value;
process.StartInfo.Arguments = arguments;
process.StartInfo.UseShellExecute = false;
process.StartInfo.CreateNoWindow = true;
Expand Down
Loading