Skip to content

Commit feed485

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

File tree

2 files changed

+92
-2
lines changed

2 files changed

+92
-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. */
@@ -223,6 +223,8 @@ private synchronized Path getBinary() {
223223
*
224224
* @param options browser options used to start the session
225225
* @return the browser binary path when present, only Chrome/Firefox/Edge
226+
* @deprecated see {@see
227+
* org.openqa.selenium.remote.service.DriverFinder#getBrowserBinary(Capabilities)}
226228
*/
227229
private String getBrowserBinary(Capabilities options) {
228230
List<String> vendorOptionsCapabilities =
@@ -250,7 +252,9 @@ private String getBrowserBinary(Capabilities options) {
250252
*
251253
* @param options Browser Options instance.
252254
* @return the location of the driver.
255+
* @deprecated use {@link #getResult(List)} instead with the list of arguments.
253256
*/
257+
@Deprecated
254258
public Result getDriverPath(Capabilities options, boolean offline) {
255259
Path binaryFile = getBinary();
256260
if (binaryFile == null) {
@@ -308,6 +312,25 @@ public Result getDriverPath(Capabilities options, boolean offline) {
308312
return result;
309313
}
310314

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

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

Lines changed: 68 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,8 +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;
2125
import java.util.logging.Logger;
2226
import org.openqa.selenium.Capabilities;
27+
import org.openqa.selenium.MutableCapabilities;
28+
import org.openqa.selenium.Proxy;
2329
import org.openqa.selenium.WebDriverException;
2430
import org.openqa.selenium.internal.Require;
2531
import org.openqa.selenium.manager.SeleniumManager;
@@ -47,7 +53,9 @@ public static Result getPath(DriverService service, Capabilities options, boolea
4753
result = new Result(System.getProperty(service.getDriverProperty()));
4854
if (result.getDriverPath() == null) {
4955
try {
50-
result = SeleniumManager.getInstance().getDriverPath(options, offline);
56+
List<String> arguments = toArguments(options, offline);
57+
result = SeleniumManager.getInstance().getResult(arguments);
58+
((MutableCapabilities) options).setCapability("browserVersion", (String) null);
5159
} catch (RuntimeException e) {
5260
throw new WebDriverException(
5361
String.format("Unable to obtain: %s, error %s", options, e.getMessage()), e);
@@ -77,4 +85,63 @@ public static Result getPath(DriverService service, Capabilities options, boolea
7785

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

0 commit comments

Comments
 (0)