Skip to content

Commit 776b45d

Browse files
committed
Some services can be configured via Capabilities
While this isn't great, it may be necessary. Notably, the SafariDriver can be configured to use either the Technology Preview or the normal version, and the XpiDriverService might want to use the firefox profile or binary specified.
1 parent 6831a7a commit 776b45d

File tree

2 files changed

+85
-1
lines changed

2 files changed

+85
-1
lines changed

java/client/src/org/openqa/selenium/firefox/XpiDriverService.java

+80-1
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,16 @@
1818
package org.openqa.selenium.firefox;
1919

2020

21-
import static org.openqa.selenium.firefox.FirefoxProfile.PORT_PREFERENCE;
2221
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;
2324

2425
import com.google.common.base.Preconditions;
2526
import com.google.common.collect.ImmutableList;
2627
import com.google.common.collect.ImmutableMap;
2728
import com.google.common.io.ByteStreams;
2829

30+
import org.openqa.selenium.Capabilities;
2931
import org.openqa.selenium.WebDriverException;
3032
import org.openqa.selenium.firefox.internal.ClasspathExtension;
3133
import org.openqa.selenium.firefox.internal.Extension;
@@ -38,9 +40,13 @@
3840
import java.io.IOException;
3941
import java.net.MalformedURLException;
4042
import java.net.URL;
43+
import java.util.Map;
44+
import java.util.Objects;
4145
import java.util.Optional;
4246
import java.util.concurrent.locks.Lock;
4347
import java.util.concurrent.locks.ReentrantLock;
48+
import java.util.function.Supplier;
49+
import java.util.stream.Stream;
4450

4551
public class XpiDriverService extends DriverService {
4652

@@ -175,6 +181,60 @@ public static XpiDriverService createDefaultService() {
175181
}
176182
}
177183

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+
178238
public static Builder builder() {
179239
return new Builder();
180240
}
@@ -231,4 +291,23 @@ protected XpiDriverService createDriverService(
231291
}
232292
}
233293
}
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+
}
234313
}

java/client/src/org/openqa/selenium/safari/SafariDriverService.java

+5
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import com.google.common.collect.ImmutableList;
2323
import com.google.common.collect.ImmutableMap;
2424

25+
import org.openqa.selenium.Capabilities;
2526
import org.openqa.selenium.WebDriverException;
2627
import org.openqa.selenium.net.PortProber;
2728
import org.openqa.selenium.remote.service.DriverService;
@@ -58,6 +59,10 @@ public static SafariDriverService createDefaultService(SafariOptions options) {
5859
throw new WebDriverException("SafariDriver requires Safari 10 running on OSX El Capitan or greater.");
5960
}
6061

62+
static SafariDriverService createDefaultService(Capabilities caps) {
63+
return createDefaultService(new SafariOptions());
64+
}
65+
6166
@Override
6267
protected void waitUntilAvailable() throws MalformedURLException {
6368
try {

0 commit comments

Comments
 (0)