|
18 | 18 | package org.openqa.selenium.remote.service; |
19 | 19 |
|
20 | 20 | import java.io.File; |
| 21 | +import java.util.ArrayList; |
| 22 | +import java.util.Arrays; |
| 23 | +import java.util.List; |
| 24 | +import java.util.Map; |
21 | 25 | import java.util.logging.Logger; |
22 | 26 | import org.openqa.selenium.Capabilities; |
| 27 | +import org.openqa.selenium.MutableCapabilities; |
| 28 | +import org.openqa.selenium.Proxy; |
23 | 29 | import org.openqa.selenium.WebDriverException; |
24 | 30 | import org.openqa.selenium.internal.Require; |
25 | 31 | import org.openqa.selenium.manager.SeleniumManager; |
@@ -47,7 +53,9 @@ public static Result getPath(DriverService service, Capabilities options, boolea |
47 | 53 | result = new Result(System.getProperty(service.getDriverProperty())); |
48 | 54 | if (result.getDriverPath() == null) { |
49 | 55 | 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); |
51 | 59 | } catch (RuntimeException e) { |
52 | 60 | throw new WebDriverException( |
53 | 61 | 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 |
77 | 85 |
|
78 | 86 | throw new NoSuchDriverException(message); |
79 | 87 | } |
| 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 | + } |
80 | 147 | } |
0 commit comments