Skip to content

Commit e5ce6fd

Browse files
authored
Look for Selenium Manager in path defined by Environment Variable (#12752)
This allows people to build or download the binary as necessary and place it in any directory
1 parent 553791c commit e5ce6fd

File tree

5 files changed

+85
-74
lines changed

5 files changed

+85
-74
lines changed

dotnet/src/webdriver/SeleniumManager.cs

+24-23
Original file line numberDiff line numberDiff line change
@@ -34,35 +34,36 @@ namespace OpenQA.Selenium
3434
/// </summary>
3535
public static class SeleniumManager
3636
{
37-
private readonly static string binaryFullPath;
37+
private static readonly string BinaryFullPath = Environment.GetEnvironmentVariable("SE_MANAGER_PATH");
3838

3939
static SeleniumManager()
4040
{
41-
var currentDirectory = AppContext.BaseDirectory;
4241

43-
string binary;
44-
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
42+
if (BinaryFullPath == null)
4543
{
46-
binary = "selenium-manager/windows/selenium-manager.exe";
47-
}
48-
else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
49-
{
50-
binary = "selenium-manager/linux/selenium-manager";
51-
}
52-
else if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
53-
{
54-
binary = "selenium-manager/macos/selenium-manager";
55-
}
56-
else
57-
{
58-
throw new WebDriverException("Selenium Manager did not find supported operating system");
44+
var currentDirectory = AppContext.BaseDirectory;
45+
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
46+
{
47+
BinaryFullPath = Path.Combine(currentDirectory, "selenium-manager\\windows\\selenium-manager.exe");
48+
}
49+
else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
50+
{
51+
BinaryFullPath = Path.Combine(currentDirectory, "selenium-manager\\linux\\selenium-manager");
52+
}
53+
else if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
54+
{
55+
BinaryFullPath = Path.Combine(currentDirectory, "selenium-manager\\macos\\selenium-manager");
56+
}
57+
else
58+
{
59+
throw new PlatformNotSupportedException(
60+
$"Selenium Manager doesn't support your runtime platform: {RuntimeInformation.OSDescription}");
61+
}
5962
}
6063

61-
binaryFullPath = Path.Combine(currentDirectory, binary);
62-
63-
if (!File.Exists(binaryFullPath))
64+
if (!File.Exists(BinaryFullPath))
6465
{
65-
throw new WebDriverException($"Unable to locate or obtain Selenium Manager binary at {binaryFullPath}");
66+
throw new WebDriverException($"Unable to locate or obtain Selenium Manager binary at {BinaryFullPath}");
6667
}
6768
}
6869

@@ -102,7 +103,7 @@ public static string DriverPath(DriverOptions options)
102103
}
103104
}
104105

105-
Dictionary<string, object> output = RunCommand(binaryFullPath, argsBuilder.ToString());
106+
Dictionary<string, object> output = RunCommand(BinaryFullPath, argsBuilder.ToString());
106107
string browserPath = (string)output["browser_path"];
107108
string driverPath = (string)output["driver_path"];
108109

@@ -130,7 +131,7 @@ public static string DriverPath(DriverOptions options)
130131
private static Dictionary<string, object> RunCommand(string fileName, string arguments)
131132
{
132133
Process process = new Process();
133-
process.StartInfo.FileName = binaryFullPath;
134+
process.StartInfo.FileName = BinaryFullPath;
134135
process.StartInfo.Arguments = arguments;
135136
process.StartInfo.UseShellExecute = false;
136137
process.StartInfo.CreateNoWindow = true;

java/src/org/openqa/selenium/manager/SeleniumManager.java

+22-20
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import java.nio.file.FileVisitResult;
2626
import java.nio.file.Files;
2727
import java.nio.file.Path;
28+
import java.nio.file.Paths;
2829
import java.nio.file.SimpleFileVisitor;
2930
import java.nio.file.attribute.BasicFileAttributes;
3031
import java.util.ArrayList;
@@ -61,11 +62,11 @@ public class SeleniumManager {
6162
private static final Logger LOG = Logger.getLogger(SeleniumManager.class.getName());
6263

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

6666
private static volatile SeleniumManager manager;
6767

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

7071
/** Wrapper for the Selenium Manager binary. */
7172
private SeleniumManager() {
@@ -158,30 +159,31 @@ private static Result runCommand(Path binary, List<String> arguments) {
158159
*/
159160
private synchronized Path getBinary() {
160161
if (binary == null) {
161-
try {
162-
Platform current = Platform.getCurrent();
163-
String folder = "linux";
164-
String extension = "";
165-
if (current.is(WINDOWS)) {
166-
extension = EXE;
167-
folder = "windows";
168-
} else if (current.is(MAC)) {
169-
folder = "macos";
170-
}
171-
String binaryPath = String.format("%s/%s%s", folder, SELENIUM_MANAGER, extension);
172-
try (InputStream inputStream = this.getClass().getResourceAsStream(binaryPath)) {
173-
Path tmpPath = Files.createTempDirectory(SELENIUM_MANAGER + System.nanoTime());
162+
Platform current = Platform.getCurrent();
163+
String folder = "linux";
164+
String extension = "";
165+
if (current.is(WINDOWS)) {
166+
extension = ".exe";
167+
folder = "windows";
168+
} else if (current.is(MAC)) {
169+
folder = "macos";
170+
}
171+
String binaryPath = String.format("%s/%s%s", folder, SELENIUM_MANAGER, extension);
172+
try (InputStream inputStream = this.getClass().getResourceAsStream(binaryPath)) {
173+
Path tmpPath = Files.createTempDirectory(SELENIUM_MANAGER + System.nanoTime());
174174

175-
deleteOnExit(tmpPath);
175+
deleteOnExit(tmpPath);
176176

177-
binary = tmpPath.resolve(SELENIUM_MANAGER + extension);
178-
Files.copy(inputStream, binary, REPLACE_EXISTING);
179-
}
180-
binary.toFile().setExecutable(true);
177+
binary = tmpPath.resolve(SELENIUM_MANAGER + extension);
178+
Files.copy(inputStream, binary, REPLACE_EXISTING);
181179
} catch (Exception e) {
182180
throw new WebDriverException("Unable to obtain Selenium Manager Binary", e);
183181
}
182+
} else if (!Files.exists(binary)) {
183+
throw new WebDriverException(String.format("Unable to obtain Selenium Manager Binary at: %s", binary));
184184
}
185+
binary.toFile().setExecutable(true);
186+
185187
LOG.fine(String.format("Selenium Manager binary found at: %s", binary));
186188

187189
return binary;

javascript/node/selenium-webdriver/common/seleniumManager.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ function getBinary() {
4646

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

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

5151
if (!fs.existsSync(filePath)) {
5252
throw new Error(`Unable to obtain Selenium Manager at ${filePath}`)

py/selenium/webdriver/common/selenium_manager.py

+12-9
Original file line numberDiff line numberDiff line change
@@ -40,19 +40,22 @@ def get_binary() -> Path:
4040
4141
:Returns: The Selenium Manager executable location
4242
"""
43-
platform = sys.platform
4443

45-
dirs = {
46-
"darwin": "macos",
47-
"win32": "windows",
48-
"cygwin": "windows",
49-
}
44+
if os.getenv("SE_MANAGER_PATH"):
45+
path = os.getenv("SE_MANAGER_PATH")
46+
else:
47+
platform = sys.platform
5048

51-
directory = dirs.get(platform) if dirs.get(platform) else platform
49+
dirs = {
50+
"darwin": "macos",
51+
"win32": "windows",
52+
"cygwin": "windows",
53+
}
5254

53-
file = "selenium-manager.exe" if directory == "windows" else "selenium-manager"
55+
directory = dirs.get(platform) if dirs.get(platform) else platform
56+
file = "selenium-manager.exe" if directory == "windows" else "selenium-manager"
5457

55-
path = Path(__file__).parent.joinpath(directory, file)
58+
path = Path(__file__).parent.joinpath(directory, file)
5659

5760
if not path.is_file() and os.environ["CONDA_PREFIX"]:
5861
# conda has a separate package selenium-manager, installs in bin

rb/lib/selenium/webdriver/common/selenium_manager.rb

+26-21
Original file line numberDiff line numberDiff line change
@@ -75,31 +75,36 @@ def generate_command(binary, options)
7575
# @return [String] the path to the correct selenium manager
7676
def binary
7777
@binary ||= begin
78-
path = File.expand_path(bin_path, __FILE__)
79-
path << if Platform.windows?
80-
'/windows/selenium-manager.exe'
81-
elsif Platform.mac?
82-
'/macos/selenium-manager'
83-
elsif Platform.linux?
84-
'/linux/selenium-manager'
85-
end
86-
location = File.expand_path(path, __FILE__)
87-
88-
begin
89-
Platform.assert_file(location)
90-
Platform.assert_executable(location)
91-
rescue TypeError
92-
raise Error::WebDriverError,
93-
"Unable to locate or obtain Selenium Manager binary; #{location} is not a valid file object"
94-
rescue Error::WebDriverError => e
95-
raise Error::WebDriverError, "Selenium Manager binary located, but #{e.message}"
96-
end
97-
98-
WebDriver.logger.debug("Selenium Manager binary found at #{location}", id: :selenium_manager)
78+
location = ENV.fetch('SE_MANAGER_PATH', begin
79+
directory = File.expand_path(bin_path, __FILE__)
80+
if Platform.windows?
81+
"#{directory}/windows/selenium-manager.exe"
82+
elsif Platform.mac?
83+
"#{directory}/macos/selenium-manager"
84+
elsif Platform.linux?
85+
"#{directory}/linux/selenium-manager"
86+
end
87+
end)
88+
89+
validate_location(location)
9990
location
10091
end
10192
end
10293

94+
def validate_location(location)
95+
begin
96+
Platform.assert_file(location)
97+
Platform.assert_executable(location)
98+
rescue TypeError
99+
raise Error::WebDriverError,
100+
"Unable to locate or obtain Selenium Manager binary; #{location} is not a valid file object"
101+
rescue Error::WebDriverError => e
102+
raise Error::WebDriverError, "Selenium Manager binary located, but #{e.message}"
103+
end
104+
105+
WebDriver.logger.debug("Selenium Manager binary found at #{location}", id: :selenium_manager)
106+
end
107+
103108
def run(*command)
104109
command += %w[--output json]
105110
command << '--debug' if WebDriver.logger.debug?

0 commit comments

Comments
 (0)