|
18 | 18 | package org.openqa.selenium.firefox;
|
19 | 19 |
|
20 | 20 |
|
21 |
| -import static org.openqa.selenium.firefox.FirefoxProfile.PORT_PREFERENCE; |
22 | 21 | import static java.util.concurrent.TimeUnit.SECONDS;
|
| 22 | +import static org.openqa.selenium.firefox.FirefoxOptions.FIREFOX_OPTIONS; |
| 23 | +import static org.openqa.selenium.firefox.FirefoxProfile.PORT_PREFERENCE; |
23 | 24 |
|
24 | 25 | import com.google.common.base.Preconditions;
|
25 | 26 | import com.google.common.collect.ImmutableList;
|
26 | 27 | import com.google.common.collect.ImmutableMap;
|
27 | 28 | import com.google.common.io.ByteStreams;
|
28 | 29 |
|
| 30 | +import org.openqa.selenium.Capabilities; |
29 | 31 | import org.openqa.selenium.WebDriverException;
|
30 | 32 | import org.openqa.selenium.firefox.internal.ClasspathExtension;
|
31 | 33 | import org.openqa.selenium.firefox.internal.Extension;
|
|
38 | 40 | import java.io.IOException;
|
39 | 41 | import java.net.MalformedURLException;
|
40 | 42 | import java.net.URL;
|
| 43 | +import java.util.Map; |
| 44 | +import java.util.Objects; |
41 | 45 | import java.util.Optional;
|
42 | 46 | import java.util.concurrent.locks.Lock;
|
43 | 47 | import java.util.concurrent.locks.ReentrantLock;
|
| 48 | +import java.util.function.Supplier; |
| 49 | +import java.util.stream.Stream; |
44 | 50 |
|
45 | 51 | public class XpiDriverService extends DriverService {
|
46 | 52 |
|
@@ -175,6 +181,60 @@ public static XpiDriverService createDefaultService() {
|
175 | 181 | }
|
176 | 182 | }
|
177 | 183 |
|
| 184 | + @SuppressWarnings("unchecked") |
| 185 | + static XpiDriverService createDefaultService(Capabilities caps) { |
| 186 | + Builder builder = new Builder().usingAnyFreePort(); |
| 187 | + |
| 188 | + FirefoxProfile profile = Stream.<ThrowingSupplier<FirefoxProfile>>of( |
| 189 | + () -> (FirefoxProfile) caps.getCapability(FirefoxDriver.PROFILE), |
| 190 | + () -> FirefoxProfile.fromJson((String) caps.getCapability(FirefoxDriver.PROFILE)), |
| 191 | + () -> ((FirefoxOptions) caps).getProfile(), |
| 192 | + () -> (FirefoxProfile) ((Map<String, Object>) caps.getCapability(FIREFOX_OPTIONS)).get("profile"), |
| 193 | + () -> FirefoxProfile.fromJson((String) ((Map<String, Object>) caps.getCapability(FIREFOX_OPTIONS)).get("profile")), |
| 194 | + () -> { |
| 195 | + Map<String, Object> options = (Map<String, Object>) caps.getCapability(FIREFOX_OPTIONS); |
| 196 | + FirefoxProfile toReturn = new FirefoxProfile(); |
| 197 | + ((Map<String, Object>) options.get("prefs")).forEach((key, value) -> { |
| 198 | + if (value instanceof Boolean) { toReturn.setPreference(key, (Boolean) value); } |
| 199 | + if (value instanceof Integer) { toReturn.setPreference(key, (Integer) value); } |
| 200 | + if (value instanceof String) { toReturn.setPreference(key, (String) value); } |
| 201 | + }); |
| 202 | + return toReturn; |
| 203 | + }) |
| 204 | + .map(supplier -> { |
| 205 | + try { |
| 206 | + return supplier.get(); |
| 207 | + } catch (Exception e) { |
| 208 | + return null; |
| 209 | + } |
| 210 | + }) |
| 211 | + .filter(Objects::nonNull) |
| 212 | + .findFirst() |
| 213 | + .orElse(null); |
| 214 | + |
| 215 | + if (profile != null) { |
| 216 | + builder.withProfile(profile); |
| 217 | + } |
| 218 | + |
| 219 | + Object binary = caps.getCapability(FirefoxDriver.BINARY); |
| 220 | + if (binary != null) { |
| 221 | + FirefoxBinary actualBinary; |
| 222 | + if (binary instanceof FirefoxBinary) { |
| 223 | + actualBinary = (FirefoxBinary) binary; |
| 224 | + } else if (binary instanceof String) { |
| 225 | + actualBinary = new FirefoxBinary(new File(String.valueOf(binary))); |
| 226 | + } else { |
| 227 | + throw new IllegalArgumentException( |
| 228 | + "Expected binary to be a string or a binary: " + binary); |
| 229 | + } |
| 230 | + |
| 231 | + builder.withBinary(actualBinary); |
| 232 | + } |
| 233 | + |
| 234 | + return builder.build(); |
| 235 | + |
| 236 | + } |
| 237 | + |
178 | 238 | public static Builder builder() {
|
179 | 239 | return new Builder();
|
180 | 240 | }
|
@@ -231,4 +291,23 @@ protected XpiDriverService createDriverService(
|
231 | 291 | }
|
232 | 292 | }
|
233 | 293 | }
|
| 294 | + |
| 295 | + @FunctionalInterface |
| 296 | + private interface ThrowingSupplier<V> extends Supplier<V> { |
| 297 | + |
| 298 | + V throwingGet() throws Exception; |
| 299 | + |
| 300 | + @Override |
| 301 | + default V get() { |
| 302 | + try { |
| 303 | + return throwingGet(); |
| 304 | + } catch (Exception e) { |
| 305 | + if (e instanceof RuntimeException) { |
| 306 | + throw (RuntimeException) e; |
| 307 | + } else { |
| 308 | + throw new RuntimeException(e); |
| 309 | + } |
| 310 | + } |
| 311 | + } |
| 312 | + } |
234 | 313 | }
|
0 commit comments