Skip to content

Commit

Permalink
Redesigning MarionetteDriver to use the gecko shim
Browse files Browse the repository at this point in the history
  • Loading branch information
barancev committed Aug 24, 2015
1 parent a660392 commit ad00dd5
Show file tree
Hide file tree
Showing 5 changed files with 164 additions and 39 deletions.
102 changes: 102 additions & 0 deletions java/client/src/org/openqa/selenium/firefox/GeckoDriverService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
// 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.WebDriverException;
import org.openqa.selenium.firefox.internal.Executable;
import org.openqa.selenium.remote.service.DriverService;

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

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

/**
* System property that defines the location of the GeckoDriver executable
* that will be used by the {@link #createDefaultService() default service}.
*/
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.
* @param environment The environment for the launched server.
* @throws IOException If an I/O error occurs.
*/
private GeckoDriverService(File executable, int port, ImmutableList<String> args,
ImmutableMap<String, String> environment) throws IOException {
super(executable, port, args, environment);
}

/**
* Configures and returns a new {@link GeckoDriverService} using the default configuration. In
* this configuration, the service will use the GeckoDriver executable identified by the
* {@link #GECKO_DRIVER_EXE_PROPERTY} system property. Each service created by this method will
* be configured to use a free port on the current system.
*
* @return A new GeckoDriverService using the default configuration.
*/
public static GeckoDriverService createDefaultService() {
return new Builder().usingAnyFreePort().build();
}

/**
* Builder used to configure new {@link GeckoDriverService} instances.
*/
public static class Builder extends DriverService.Builder<
GeckoDriverService, GeckoDriverService.Builder> {

@Override
protected File findDefaultExecutable() {
return findExecutable("wires", GECKO_DRIVER_EXE_PROPERTY,
"https://github.com/jgraham/wires",
"https://github.com/jgraham/wires");
}

@Override
protected ImmutableList<String> createArgs() {
ImmutableList.Builder<String> argsBuilder = ImmutableList.builder();
argsBuilder.add(String.format("--webdriver-port=%d", getPort()));
if (getLogFile() != null) {
argsBuilder.add(String.format("--log-file=\"%s\"", getLogFile().getAbsolutePath()));
}
argsBuilder.add("-b");
argsBuilder.add(new Executable(null).getPath());
return argsBuilder.build();
}

@Override
protected GeckoDriverService createDriverService(File exe, int port,
ImmutableList<String> args,
ImmutableMap<String, String> environment) {
try {
return new GeckoDriverService(exe, port, args, environment);
} catch (IOException e) {
throw new WebDriverException(e);
}
}
}
}
81 changes: 49 additions & 32 deletions java/client/src/org/openqa/selenium/firefox/MarionetteDriver.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,64 +17,81 @@

package org.openqa.selenium.firefox;

import com.google.common.base.Throwables;

import org.openqa.selenium.Beta;
import org.openqa.selenium.Capabilities;
import org.openqa.selenium.WebDriverException;
import org.openqa.selenium.firefox.internal.MarionetteConnection;
import org.openqa.selenium.interactions.ActionChainExecutor;
import org.openqa.selenium.interactions.CanPerformActionChain;
import org.openqa.selenium.internal.Lock;
import org.openqa.selenium.remote.RemoteActionChainExecutor;
import org.openqa.selenium.remote.RemoteExecuteMethod;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.FileDetector;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.openqa.selenium.remote.service.DriverCommandExecutor;

/**
* An implementation of the {#link WebDriver} interface that drives Firefox using Marionette interface.
*/
@Beta
public class MarionetteDriver extends FirefoxDriver implements CanPerformActionChain {
public class MarionetteDriver extends RemoteWebDriver {

/**
* Port which is used by default.
*/
private final static int DEFAULT_PORT = 0;

public MarionetteDriver() {
this(new FirefoxBinary(), null);
this(null, null, DEFAULT_PORT);
}

public MarionetteDriver(FirefoxProfile profile) {
super(profile);
public MarionetteDriver(Capabilities capabilities) {
this(null, capabilities, DEFAULT_PORT);
}

public MarionetteDriver(Capabilities desiredCapabilities) {
super(desiredCapabilities);
public MarionetteDriver(int port) {
this(null, null, port);
}

public MarionetteDriver(Capabilities desiredCapabilities, Capabilities requiredCapabilities) {
super(desiredCapabilities, requiredCapabilities);
public MarionetteDriver(GeckoDriverService service) {
this(service, null, DEFAULT_PORT);
}

public MarionetteDriver(FirefoxBinary binary, FirefoxProfile profile) {
super(binary, profile);
public MarionetteDriver(GeckoDriverService service, Capabilities capabilities) {
this(service, capabilities, DEFAULT_PORT);
}

public MarionetteDriver(FirefoxBinary binary, FirefoxProfile profile, Capabilities capabilities) {
super(binary, profile, capabilities);
public MarionetteDriver(GeckoDriverService service, Capabilities capabilities,
int port) {
if (capabilities == null) {
capabilities = DesiredCapabilities.internetExplorer();
}

if (service == null) {
service = setupService(capabilities, port);
}
run(service, capabilities);
}

public MarionetteDriver(FirefoxBinary binary, FirefoxProfile profile,
Capabilities desiredCapabilities, Capabilities requiredCapabilities) {
super(binary, profile, desiredCapabilities, requiredCapabilities);
private void run(GeckoDriverService service, Capabilities capabilities) {
setCommandExecutor(new DriverCommandExecutor(service));

startSession(capabilities);
}

protected ExtensionConnection connectTo(FirefoxBinary binary, FirefoxProfile profile,
String host) {
Lock lock = obtainLock(profile);
@Override
public void setFileDetector(FileDetector detector) {
throw new WebDriverException(
"Setting the file detector only works on remote webdriver instances obtained " +
"via RemoteWebDriver");
}

private GeckoDriverService setupService(Capabilities caps, int port) {
try {
FirefoxBinary bin = binary == null ? new FirefoxBinary() : binary;
GeckoDriverService.Builder builder = new GeckoDriverService.Builder();
builder.usingPort(port);

return new MarionetteConnection(lock, bin, profile, host);
} catch (Exception e) {
throw new WebDriverException(e);
}
}
return builder.build();

public ActionChainExecutor getActionChainExecutor() {
return new RemoteActionChainExecutor(new RemoteExecuteMethod(this));
} catch (IllegalStateException ex) {
throw Throwables.propagate(ex);
}
}
}
1 change: 1 addition & 0 deletions java/client/src/org/openqa/selenium/firefox/build.desc
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ java_library(name = "firefox",
"//java/client/src/org/openqa/selenium/net",
"//java/client/src/org/openqa/selenium/os",
"//java/client/src/org/openqa/selenium/remote",
"//java/client/src/org/openqa/selenium/remote/service",
"//third_party/java/commons-io",
"//third_party/java/guava-libraries",
"//third_party/java/commons-exec",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -163,11 +163,11 @@ public void start() throws IOException {
process.copyOutputTo(System.err);
process.executeAsync();

URL status = new URL(url.toString() + "/status");
new UrlChecker().waitUntilAvailable(20, SECONDS, status);
} catch (UrlChecker.TimeoutException e) {
process.checkForError();
throw new WebDriverException("Timed out waiting for driver server to start.", e);
//URL status = new URL(url.toString() + "/status");
//new UrlChecker().waitUntilAvailable(20, SECONDS, status);
//} catch (UrlChecker.TimeoutException e) {
// process.checkForError();
// throw new WebDriverException("Timed out waiting for driver server to start.", e);
} finally {
lock.unlock();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,13 @@ public WebDriver get() {
profile.setEnableNativeEvents(enableNativeEvents);
desiredCapsToUse.setCapability(FirefoxDriver.PROFILE, profile);

return driverClass.getConstructor(Capabilities.class,
Capabilities.class).newInstance(desiredCapsToUse, requiredCapabilities);
try {
return driverClass.getConstructor(Capabilities.class,
Capabilities.class)
.newInstance(desiredCapsToUse, requiredCapabilities);
} catch (NoSuchMethodException ex) {
return driverClass.getConstructor(Capabilities.class).newInstance(desiredCapsToUse);
}
}

try {
Expand Down

1 comment on commit ad00dd5

@joshbruning
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did you mean to do this?

//URL status = new URL(url.toString() + "/status");
//new UrlChecker().waitUntilAvailable(20, SECONDS, status);
//} catch (UrlChecker.TimeoutException e) {
// process.checkForError();
// throw new WebDriverException("Timed out waiting for driver server to start.", e);

Please sign in to comment.