Skip to content

Commit

Permalink
[java] Adding Firefox XPI driver to bazel build and fixing Firefox dr…
Browse files Browse the repository at this point in the history
…iver scores.
  • Loading branch information
barancev committed Dec 4, 2018
1 parent e745da2 commit fefe718
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ public int score(Capabilities capabilites) {
return 0;
}

int score = 1;
int score = 0;

if (BrowserType.FIREFOX.equals(capabilites.getBrowserName())) {
score++;
Expand Down
30 changes: 30 additions & 0 deletions java/client/src/org/openqa/selenium/firefox/xpi/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
load("//java:version.bzl", "SE_VERSION")

genrule(
name = "webdriver_prefs",
srcs = [
"//third_party/js/selenium:webdriver_prefs",
],
outs = ["webdriver_prefs.json"],
cmd = "cp \"$(location //third_party/js/selenium:webdriver_prefs)\" \"$@\"",
)

java_library(
name = "xpi",
srcs = glob(["*.java"]),
resources = [
":webdriver_prefs",
],
tags = [
"maven_coordinates=org.seleniumhq.selenium:selenium-firefox-xpi-driver:" + SE_VERSION,
],
visibility = [
"//visibility:public",
],
deps = [
"//java/client/src/org/openqa/selenium/firefox",
"//java/client/src/org/openqa/selenium/remote",
"//third_party/java/guava",
"//third_party/java/service",
],
)
Original file line number Diff line number Diff line change
Expand Up @@ -307,12 +307,21 @@ public static XpiDriverService createDefaultService() {
static XpiDriverService createDefaultService(Capabilities caps) {
Builder builder = new Builder().usingAnyFreePort();

FirefoxProfile profile = Stream.<ThrowingSupplier<FirefoxProfile>>of(
FirefoxProfile profile = Stream.<Supplier<FirefoxProfile>>of(
() -> (FirefoxProfile) caps.getCapability(FirefoxDriver.PROFILE),
() -> FirefoxProfile.fromJson((String) caps.getCapability(FirefoxDriver.PROFILE)),
() -> { try {
return FirefoxProfile.fromJson((String) caps.getCapability(FirefoxDriver.PROFILE));
} catch (IOException ex) {
throw new RuntimeException(ex);
}},
() -> ((FirefoxOptions) caps).getProfile(),
() -> (FirefoxProfile) ((Map<String, Object>) caps.getCapability(FIREFOX_OPTIONS)).get("profile"),
() -> FirefoxProfile.fromJson((String) ((Map<String, Object>) caps.getCapability(FIREFOX_OPTIONS)).get("profile")),
() -> { try {
return FirefoxProfile.fromJson(
(String) ((Map<String, Object>) caps.getCapability(FIREFOX_OPTIONS)).get("profile"));
} catch (IOException ex) {
throw new RuntimeException(ex);
}},
() -> {
Map<String, Object> options = (Map<String, Object>) caps.getCapability(FIREFOX_OPTIONS);
FirefoxProfile toReturn = new FirefoxProfile();
Expand Down Expand Up @@ -446,23 +455,4 @@ protected XpiDriverService createDriverService(
}
}
}

@FunctionalInterface
private interface ThrowingSupplier<V> extends Supplier<V> {

V throwingGet() throws Exception;

@Override
default V get() {
try {
return throwingGet();
} catch (Exception e) {
if (e instanceof RuntimeException) {
throw (RuntimeException) e;
} else {
throw new RuntimeException(e);
}
}
}
}
}
1 change: 1 addition & 0 deletions java/client/test/org/openqa/selenium/remote/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ gen_java_tests(
"//java/client/src/org/openqa/selenium",
"//java/client/src/org/openqa/selenium/chrome",
"//java/client/src/org/openqa/selenium/firefox",
"//java/client/src/org/openqa/selenium/firefox/xpi",
"//java/client/src/org/openqa/selenium/ie",
"//java/client/src/org/openqa/selenium/remote",
"//third_party/java/assertj",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,10 @@
import org.openqa.selenium.SessionNotCreatedException;
import org.openqa.selenium.chrome.ChromeDriverService;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.firefox.FirefoxDriverService;
import org.openqa.selenium.firefox.FirefoxOptions;
import org.openqa.selenium.firefox.GeckoDriverService;
import org.openqa.selenium.firefox.xpi.XpiDriverService;
import org.openqa.selenium.ie.InternetExplorerDriverService;
import org.openqa.selenium.ie.InternetExplorerOptions;
import org.openqa.selenium.json.Json;
Expand Down Expand Up @@ -272,6 +274,22 @@ public void shouldDetectDriverServicesAndUseThoseIfNoOtherChoiceMade() {
assertThat(plan.getDriverService().getClass()).isEqualTo(expectedServiceClass);
}

@Test
public void shouldPreferMarionette() {
// Make sure we have at least one of the services available
Capabilities caps = new FirefoxOptions();

RemoteWebDriverBuilder.Plan plan = RemoteWebDriver.builder()
.addAlternative(caps)
.getPlan();

assertThat(new XpiDriverService.Builder().score(caps)).isEqualTo(0);
assertThat(new GeckoDriverService.Builder().score(caps)).isEqualTo(1);

assertThat(plan.isUsingDriverService()).isTrue();
assertThat(plan.getDriverService().getClass()).isEqualTo(GeckoDriverService.class);
}

@Test
public void oneOfWillClearOutTheCurrentlySetCapabilities() {
RemoteWebDriverBuilder builder = RemoteWebDriver.builder()
Expand Down
1 change: 1 addition & 0 deletions third_party/js/selenium/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@ filegroup(
],
visibility = [
"//java/client/src/org/openqa/selenium/firefox:__pkg__",
"//java/client/src/org/openqa/selenium/firefox/xpi:__pkg__",
],
)

0 comments on commit fefe718

Please sign in to comment.