Skip to content

Commit 04b22d3

Browse files
committed
[java] Selenium Manager generates output from argument list instead of capabilities instance
1 parent c2c41b8 commit 04b22d3

File tree

2 files changed

+95
-2
lines changed

2 files changed

+95
-2
lines changed

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

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ public class SeleniumManager {
7272
private static volatile SeleniumManager manager;
7373
private final String managerPath = System.getenv("SE_MANAGER_PATH");
7474
private Path binary = managerPath == null ? null : Paths.get(managerPath);
75-
private String seleniumManagerVersion;
75+
private final String seleniumManagerVersion;
7676
private boolean binaryInTemporalFolder = false;
7777

7878
/** Wrapper for the Selenium Manager binary. */
@@ -221,6 +221,8 @@ private synchronized Path getBinary() {
221221
*
222222
* @param options browser options used to start the session
223223
* @return the browser binary path when present, only Chrome/Firefox/Edge
224+
* @deprecated see {@see
225+
* org.openqa.selenium.remote.service.DriverFinder#getBrowserBinary(Capabilities)}
224226
*/
225227
private String getBrowserBinary(Capabilities options) {
226228
List<String> vendorOptionsCapabilities =
@@ -248,7 +250,9 @@ private String getBrowserBinary(Capabilities options) {
248250
*
249251
* @param options Browser Options instance.
250252
* @return the location of the driver.
253+
* @deprecated use {@link #getResult(List)} instead with the list of arguments.
251254
*/
255+
@Deprecated
252256
public Result getDriverPath(Capabilities options, boolean offline) {
253257
Path binaryFile = getBinary();
254258
if (binaryFile == null) {
@@ -306,6 +310,25 @@ public Result getDriverPath(Capabilities options, boolean offline) {
306310
return result;
307311
}
308312

313+
/**
314+
* Executes Selenium Manager to get the locations of the requested assets
315+
*
316+
* @param arguments List of command line arguments to send to Selenium Manager binary
317+
* @return the locations of the assets from Selenium Manager execution
318+
*/
319+
public Result getResult(List<String> arguments) {
320+
arguments.add("--language-binding");
321+
arguments.add("java");
322+
arguments.add("--output");
323+
arguments.add("json");
324+
325+
if (getLogLevel().intValue() <= Level.FINE.intValue()) {
326+
arguments.add("--debug");
327+
}
328+
329+
return runCommand(getBinary(), arguments);
330+
}
331+
309332
private Level getLogLevel() {
310333
Level level = LOG.getLevel();
311334
if (level == null && LOG.getParent() != null) {

java/src/org/openqa/selenium/remote/service/DriverFinder.java

Lines changed: 71 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,14 @@
1818
package org.openqa.selenium.remote.service;
1919

2020
import java.io.File;
21+
import java.util.ArrayList;
22+
import java.util.Arrays;
23+
import java.util.List;
24+
import java.util.Map;
25+
import java.util.logging.Logger;
2126
import org.openqa.selenium.Capabilities;
27+
import org.openqa.selenium.MutableCapabilities;
28+
import org.openqa.selenium.Proxy;
2229
import org.openqa.selenium.WebDriverException;
2330
import org.openqa.selenium.internal.Require;
2431
import org.openqa.selenium.manager.SeleniumManager;
@@ -27,6 +34,8 @@
2734

2835
public class DriverFinder {
2936

37+
private static final Logger LOG = Logger.getLogger(DriverFinder.class.getName());
38+
3039
public static Result getPath(DriverService service, Capabilities options) {
3140
return getPath(service, options, false);
3241
}
@@ -37,7 +46,9 @@ public static Result getPath(DriverService service, Capabilities options, boolea
3746

3847
if (result.getDriverPath() == null) {
3948
try {
40-
result = SeleniumManager.getInstance().getDriverPath(options, offline);
49+
List<String> arguments = toArguments(options, offline);
50+
result = SeleniumManager.getInstance().getResult(arguments);
51+
((MutableCapabilities) options).setCapability("browserVersion", (String) null);
4152
} catch (RuntimeException e) {
4253
throw new WebDriverException(
4354
String.format("Unable to obtain: %s, error %s", options, e.getMessage()), e);
@@ -62,4 +73,63 @@ public static Result getPath(DriverService service, Capabilities options, boolea
6273

6374
throw new NoSuchDriverException(message);
6475
}
76+
77+
private static List<String> toArguments(Capabilities options, boolean offline) {
78+
List<String> arguments = new ArrayList<>();
79+
arguments.add("--browser");
80+
arguments.add(options.getBrowserName());
81+
82+
if (!options.getBrowserVersion().isEmpty()) {
83+
arguments.add("--browser-version");
84+
arguments.add(options.getBrowserVersion());
85+
}
86+
87+
String browserBinary = getBrowserBinary(options);
88+
if (browserBinary != null && !browserBinary.isEmpty()) {
89+
arguments.add("--browser-path");
90+
arguments.add(browserBinary);
91+
}
92+
93+
if (offline) {
94+
arguments.add("--offline");
95+
}
96+
97+
Proxy proxy = Proxy.extractFrom(options);
98+
if (proxy != null) {
99+
arguments.add("--proxy");
100+
if (proxy.getSslProxy() != null) {
101+
arguments.add(proxy.getSslProxy());
102+
} else if (proxy.getHttpProxy() != null) {
103+
arguments.add(proxy.getHttpProxy());
104+
}
105+
}
106+
return arguments;
107+
}
108+
109+
/**
110+
* Returns the browser binary path when present in the vendor options
111+
*
112+
* @param options browser options used to start the session
113+
* @return the browser binary path when present, only Chrome/Firefox/Edge
114+
*/
115+
private static String getBrowserBinary(Capabilities options) {
116+
List<String> vendorOptionsCapabilities =
117+
Arrays.asList("moz:firefoxOptions", "goog:chromeOptions", "ms:edgeOptions");
118+
for (String vendorOptionsCapability : vendorOptionsCapabilities) {
119+
if (options.asMap().containsKey(vendorOptionsCapability)) {
120+
try {
121+
@SuppressWarnings("unchecked")
122+
Map<String, Object> vendorOptions =
123+
(Map<String, Object>) options.getCapability(vendorOptionsCapability);
124+
return (String) vendorOptions.get("binary");
125+
} catch (Exception e) {
126+
LOG.warning(
127+
String.format(
128+
"Exception while retrieving the browser binary path. %s: %s",
129+
options, e.getMessage()));
130+
}
131+
}
132+
}
133+
return null;
134+
}
65135
}

0 commit comments

Comments
 (0)