Skip to content

Commit

Permalink
[java] Secession of legacy firefox driver to a separate maven artifact
Browse files Browse the repository at this point in the history
  • Loading branch information
barancev committed Nov 16, 2018
1 parent d3b3c55 commit 017f2c7
Show file tree
Hide file tree
Showing 9 changed files with 185 additions and 61 deletions.
1 change: 1 addition & 0 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ JAVA_RELEASE_TARGETS = [
'//java/client/src/org/openqa/selenium/chrome:chrome',
'//java/client/src/org/openqa/selenium/edge:edge',
'//java/client/src/org/openqa/selenium/firefox:firefox',
'//java/client/src/org/openqa/selenium/firefox:firefox-legacy',
'//java/client/src/org/openqa/selenium/ie:ie',
'//java/client/src/org/openqa/selenium/lift:lift',
'//java/client/src/org/openqa/selenium/opera:opera',
Expand Down
1 change: 1 addition & 0 deletions java/client/src/org/openqa/selenium/BUCK
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ java_library(
':selenium',
'//java/client/src/org/openqa/selenium/chrome:chrome',
'//java/client/src/org/openqa/selenium/firefox:firefox',
'//java/client/src/org/openqa/selenium/firefox:firefox-legacy',
'//java/client/src/org/openqa/selenium/edge:edge',
'//java/client/src/org/openqa/selenium/ie:ie',
'//java/client/src/org/openqa/selenium/opera:opera',
Expand Down
41 changes: 37 additions & 4 deletions java/client/src/org/openqa/selenium/firefox/BUCK
Original file line number Diff line number Diff line change
@@ -1,16 +1,49 @@
load("//:selenium-version.bzl", "SE_VERSION")
load("//java:rules.bzl", "java_library")

LEGACY_SOURCES = [
'XpiDriverInfo.java',
'XpiDriverService.java',
]

java_library(name = 'firefox-legacy',
maven_coords = 'org.seleniumhq.selenium:selenium-firefox-legacy-driver:jar:' + SE_VERSION,
maven_pom_template = '//java/client/src/org/openqa/selenium:template-pom',
module_info = "module-info-legacy.txt",
srcs = LEGACY_SOURCES,
resources = [
':amd64',
':i386',
':webdriver.xpi',
],
exported_deps = [
'//java/client/src/org/openqa/selenium/remote:remote',
],
provided_deps = [
'//third_party/java/service:auto-service',
],
annotation_processor_deps = [
'//third_party/java/auto:auto-common',
'//third_party/java/service:auto-service',
'//third_party/java/guava:guava',
],
annotation_processors = [
'com.google.auto.service.processor.AutoServiceProcessor',
],
deps = [
':firefox',
'//third_party/java/guava:guava',
],
visibility = [ 'PUBLIC' ],
)

java_library(name = 'firefox',
maven_coords = 'org.seleniumhq.selenium:selenium-firefox-driver:jar:' + SE_VERSION,
maven_pom_template = '//java/client/src/org/openqa/selenium:template-pom',
module_info = "module-info.txt",
srcs = glob(['*.java']),
srcs = glob(['*.java'], exclude = LEGACY_SOURCES),
resources = [
':amd64',
':i386',
':prefs',
':webdriver.xpi',
],
exported_deps = [
'//java/client/src/org/openqa/selenium/remote:remote',
Expand Down
52 changes: 18 additions & 34 deletions java/client/src/org/openqa/selenium/firefox/FirefoxDriver.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,9 @@

import java.nio.file.Path;
import java.util.Objects;
import java.util.ServiceLoader;
import java.util.Set;
import java.util.stream.StreamSupport;

/**
* An implementation of the {#link WebDriver} interface that drives Firefox.
Expand Down Expand Up @@ -134,12 +136,12 @@ public FirefoxDriver(Capabilities desiredCapabilities) {
}

/**
* @deprecated Use {@link #FirefoxDriver(GeckoDriverService, FirefoxOptions)}.
* @deprecated Use {@link #FirefoxDriver(FirefoxDriverService, FirefoxOptions)}.
*/
@Deprecated
public FirefoxDriver(GeckoDriverService service, Capabilities desiredCapabilities) {
public FirefoxDriver(FirefoxDriverService service, Capabilities desiredCapabilities) {
this(
Objects.requireNonNull(service, "No geckodriver service provided"),
Objects.requireNonNull(service, "No driver service provided"),
new FirefoxOptions(desiredCapabilities));
}

Expand All @@ -148,46 +150,28 @@ public FirefoxDriver(FirefoxOptions options) {
webStorage = new RemoteWebStorage(getExecuteMethod());
}

public FirefoxDriver(GeckoDriverService service) {
super(new FirefoxDriverCommandExecutor(service), new FirefoxOptions());
webStorage = new RemoteWebStorage(getExecuteMethod());
}

public FirefoxDriver(XpiDriverService service) {
super(new FirefoxDriverCommandExecutor(service), new FirefoxOptions());
webStorage = new RemoteWebStorage(getExecuteMethod());
}

public FirefoxDriver(GeckoDriverService service, FirefoxOptions options) {
super(new FirefoxDriverCommandExecutor(service), dropCapabilities(options));
webStorage = new RemoteWebStorage(getExecuteMethod());
public FirefoxDriver(FirefoxDriverService service) {
this(service, new FirefoxOptions());
}

public FirefoxDriver(XpiDriverService service, FirefoxOptions options) {
public FirefoxDriver(FirefoxDriverService service, FirefoxOptions options) {
super(new FirefoxDriverCommandExecutor(service), dropCapabilities(options));
webStorage = new RemoteWebStorage(getExecuteMethod());
}

private static CommandExecutor toExecutor(FirefoxOptions options) {
Objects.requireNonNull(options, "No options to construct executor from");
DriverService.Builder<?, ?> builder;

if (! Boolean.parseBoolean(System.getProperty(SystemProperty.DRIVER_USE_MARIONETTE, "true"))
|| options.isLegacy()) {
FirefoxProfile profile = options.getProfile();
if (profile == null) {
profile = new FirefoxProfile();
options.setCapability(FirefoxDriver.PROFILE, profile);
}
builder = XpiDriverService.builder()
.withBinary(options.getBinary())
.withProfile(profile);
} else {
builder = new GeckoDriverService.Builder()
.usingFirefoxBinary(options.getBinary());
}

return new FirefoxDriverCommandExecutor(builder.build());
String sysProperty = System.getProperty(SystemProperty.DRIVER_USE_MARIONETTE);
boolean isLegacy = (sysProperty != null && ! Boolean.parseBoolean(sysProperty))
|| options.isLegacy();

FirefoxDriverService.Builder<?, ?> builder =
StreamSupport.stream(ServiceLoader.load(FirefoxDriverService.Builder.class).spliterator(), false)
.filter(b -> b.isLegacy() == isLegacy)
.findFirst().orElseThrow(() -> new WebDriverException());

return new FirefoxDriverCommandExecutor(builder.withOptions(options).build());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// Licensed to the Software Freedom Conservancy (SFC) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The SFC licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.

package org.openqa.selenium.firefox;

import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;

import org.openqa.selenium.remote.service.DriverService;

import java.io.File;
import java.io.IOException;

public abstract class FirefoxDriverService extends DriverService {

/**
* @param executable The GeckoDriver executable.
* @param port Which port to start the GeckoDriver on.
* @param args The arguments to the launched server.
* @param environment The environment for the launched server.
* @throws IOException If an I/O error occurs.
*/
public FirefoxDriverService(
File executable,
int port,
ImmutableList<String> args,
ImmutableMap<String, String> environment) throws IOException {
super(executable, port, args, environment);
}

public static abstract class Builder<DS extends FirefoxDriverService, B extends FirefoxDriverService.Builder<?, ?>>
extends DriverService.Builder<DS, B> {

protected abstract boolean isLegacy();
protected abstract Builder withOptions(FirefoxOptions options);
}
}
30 changes: 19 additions & 11 deletions java/client/src/org/openqa/selenium/firefox/GeckoDriverService.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,19 +29,16 @@
import org.openqa.selenium.WebDriverException;
import org.openqa.selenium.net.PortProber;
import org.openqa.selenium.remote.BrowserType;
import org.openqa.selenium.remote.service.DriverService;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.MalformedURLException;

//import org.apache.commons.io.output.NullOutputStream;

/**
* Manages the life and death of an GeckoDriver aka 'wires'.
*/
public class GeckoDriverService extends DriverService {
public class GeckoDriverService extends FirefoxDriverService {

/**
* System property that defines the location of the GeckoDriver executable
Expand All @@ -50,7 +47,6 @@ public class GeckoDriverService extends DriverService {
public static final String GECKO_DRIVER_EXE_PROPERTY = "webdriver.gecko.driver";

/**
*
* @param executable The GeckoDriver executable.
* @param port Which port to start the GeckoDriver on.
* @param args The arguments to the launched server.
Expand Down Expand Up @@ -111,9 +107,9 @@ protected boolean hasShutdownEndpoint() {
/**
* Builder used to configure new {@link GeckoDriverService} instances.
*/
@AutoService(DriverService.Builder.class)
public static class Builder extends DriverService.Builder<
GeckoDriverService, GeckoDriverService.Builder> {
@AutoService(FirefoxDriverService.Builder.class)
public static class Builder extends FirefoxDriverService.Builder<
GeckoDriverService, GeckoDriverService.Builder> {

private FirefoxBinary firefoxBinary;

Expand All @@ -130,13 +126,19 @@ public Builder(FirefoxBinary binary) {
this.firefoxBinary = binary;
}

@Override
protected boolean isLegacy() {
return false;
}

@Override
public int score(Capabilities capabilites) {
if (capabilites.is(FirefoxDriver.MARIONETTE)) {
return 0; // We're not meant for this one.
if (capabilites.getCapability(FirefoxDriver.MARIONETTE) != null
&& ! capabilites.is(FirefoxDriver.MARIONETTE)) {
return 0;
}

int score = 0;
int score = 1;

if (BrowserType.FIREFOX.equals(capabilites.getBrowserName())) {
score++;
Expand All @@ -162,6 +164,12 @@ public Builder usingFirefoxBinary(FirefoxBinary firefoxBinary) {
return this;
}

@Override
protected FirefoxDriverService.Builder withOptions(FirefoxOptions options) {
usingFirefoxBinary(options.getBinary());
return this;
}

@Override
protected File findDefaultExecutable() {
return findExecutable(
Expand Down
29 changes: 22 additions & 7 deletions java/client/src/org/openqa/selenium/firefox/XpiDriverService.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@

package org.openqa.selenium.firefox;


import static java.util.concurrent.TimeUnit.SECONDS;
import static org.openqa.selenium.firefox.FirefoxOptions.FIREFOX_OPTIONS;
import static org.openqa.selenium.firefox.FirefoxProfile.PORT_PREFERENCE;
Expand All @@ -35,7 +34,6 @@
import org.openqa.selenium.io.FileHandler;
import org.openqa.selenium.net.UrlChecker;
import org.openqa.selenium.os.CommandLine;
import org.openqa.selenium.remote.service.DriverService;

import java.io.File;
import java.io.FileNotFoundException;
Expand All @@ -56,7 +54,7 @@
import java.util.function.Supplier;
import java.util.stream.Stream;

public class XpiDriverService extends DriverService {
public class XpiDriverService extends FirefoxDriverService {

private static final String NO_FOCUS_LIBRARY_NAME = "x_ignore_nofocus.so";
private static final String PATH_PREFIX =
Expand Down Expand Up @@ -354,19 +352,24 @@ public static Builder builder() {
return new Builder();
}

@AutoService(DriverService.Builder.class)
public static class Builder extends DriverService.Builder<XpiDriverService, XpiDriverService.Builder> {
@AutoService(FirefoxDriverService.Builder.class)
public static class Builder extends FirefoxDriverService.Builder<XpiDriverService, XpiDriverService.Builder> {

private FirefoxBinary binary = null;
private FirefoxProfile profile = null;

@Override
protected boolean isLegacy() {
return true;
}

@Override
public int score(Capabilities capabilites) {
if (!capabilites.is(FirefoxDriver.MARIONETTE)) {
if (capabilites.is(FirefoxDriver.MARIONETTE)) {
return 0;
}

int score = 1;
int score = 0;

if (capabilites.getCapability(FirefoxDriver.BINARY) != null) {
score++;
Expand All @@ -389,6 +392,18 @@ public Builder withProfile(FirefoxProfile profile) {
return this;
}

@Override
protected FirefoxDriverService.Builder withOptions(FirefoxOptions options) {
FirefoxProfile profile = options.getProfile();
if (profile == null) {
profile = new FirefoxProfile();
options.setCapability(FirefoxDriver.PROFILE, profile);
}
withBinary(options.getBinary());
withProfile(profile);
return this;
}

@Override
protected File findDefaultExecutable() {
if (binary == null) {
Expand Down
34 changes: 34 additions & 0 deletions java/client/src/org/openqa/selenium/firefox/module-info-legacy.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// Licensed to the Software Freedom Conservancy (SFC) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The SFC licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.

module org.openqa.selenium.firefox {
requires java.xml;

requires transitive com.google.common;
requires transitive java.logging;
requires transitive org.openqa.selenium.core;
requires transitive org.openqa.selenium.remote;

exports org.openqa.selenium.firefox;

provides org.openqa.selenium.remote.service.DriverService$Builder with
org.openqa.selenium.firefox.XpiDriverService$Builder;

provides org.openqa.selenium.WebDriverInfo with
org.openqa.selenium.firefox.XpiDriverInfo;

}

0 comments on commit 017f2c7

Please sign in to comment.