Skip to content

Commit

Permalink
Implementing *webdriver launcher as an extension of RC server
Browse files Browse the repository at this point in the history
  • Loading branch information
barancev committed Mar 23, 2015
1 parent 8cfcac4 commit f5181c3
Show file tree
Hide file tree
Showing 11 changed files with 242 additions and 65 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,13 @@ public class SeleniumDriverResourceHandler extends ResourceHandler {
private BrowserLauncherFactory browserLauncherFactory;
private final BrowserSessionFactory browserSessionFactory;

public SeleniumDriverResourceHandler(SeleniumServer remoteControl) {
this(remoteControl, new BrowserLauncherFactory());
}

public SeleniumDriverResourceHandler(
SeleniumServer remoteControl, DriverSessions webdriverSessions) {
browserLauncherFactory = new BrowserLauncherFactory(webdriverSessions);
SeleniumServer remoteControl, BrowserLauncherFactory browserLauncherFactory) {
this.browserLauncherFactory = browserLauncherFactory;
browserSessionFactory = new BrowserSessionFactory(browserLauncherFactory);
this.remoteControl = remoteControl;
}
Expand Down
51 changes: 9 additions & 42 deletions java/server/src/org/openqa/selenium/server/SeleniumServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@

package org.openqa.selenium.server;

import static java.lang.String.format;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.openqa.jetty.http.HashUserRealm;
Expand All @@ -27,13 +25,8 @@
import org.openqa.jetty.http.SocketListener;
import org.openqa.jetty.http.handler.SecurityHandler;
import org.openqa.jetty.jetty.Server;
import org.openqa.jetty.jetty.servlet.ServletHandler;
import org.openqa.jetty.util.MultiException;
import org.openqa.selenium.internal.BuildInfo;
import org.openqa.selenium.net.NetworkUtils;
import org.openqa.selenium.remote.server.DefaultDriverSessions;
import org.openqa.selenium.remote.server.DriverServlet;
import org.openqa.selenium.remote.server.DriverSessions;
import org.openqa.selenium.remote.server.log.LoggingManager;
import org.openqa.selenium.remote.server.log.LoggingOptions;
import org.openqa.selenium.server.BrowserSessionFactory.BrowserSessionInfo;
Expand Down Expand Up @@ -194,15 +187,14 @@
*/
public class SeleniumServer implements SslCertificateGenerator {

private Log LOGGER;
protected Log LOGGER;

private Server server;
private SeleniumDriverResourceHandler driver;
private SeleniumHTMLRunnerResultsHandler postResultsHandler;
private StaticContentHandler staticContentHandler;
private final RemoteControlConfiguration configuration;
private Thread shutDownHook;
private static final NetworkUtils networkUtils = new NetworkUtils();

private ProxyHandler proxyHandler;

Expand Down Expand Up @@ -358,50 +350,25 @@ private void assembleHandlers(boolean slowResources, RemoteControlConfiguration
context.addHandler(postResultsHandler);
server.addContext(context);

// Both the selenium and webdriver contexts must be able to share sessions
DefaultDriverSessions webdriverSessions = new DefaultDriverSessions();
server.addContext(createDriverContextWithSeleniumDriverResourceHandler(context));

server.addContext(createDriverContextWithSeleniumDriverResourceHandler(
context, webdriverSessions));
server.addContext(createWebDriverRemoteContext(webdriverSessions));
addExtraContexts(server, context);
}

private HttpContext createDriverContextWithSeleniumDriverResourceHandler(
HttpContext context, DriverSessions webdriverSessions) {
private HttpContext createDriverContextWithSeleniumDriverResourceHandler(HttpContext context) {
// Associate the SeleniumDriverResourceHandler with the /selenium-server/driver context
HttpContext driverContext = new HttpContext();
driverContext.setContextPath("/selenium-server/driver");
driver = new SeleniumDriverResourceHandler(this, webdriverSessions);
driver = createDriverResourceHandler();
context.addHandler(driver);
return driverContext;
}

private HttpContext createWebDriverRemoteContext(DriverSessions webDriverSessions) {
HttpContext webdriverContext = new HttpContext();
protected SeleniumDriverResourceHandler createDriverResourceHandler() {
return new SeleniumDriverResourceHandler(this);
}

long sessionTimeout = configuration.getTimeoutInSeconds();
if (sessionTimeout == 0) {
sessionTimeout = -1;
}
long browserTimeout = configuration.getBrowserTimeoutInMs();
if (browserTimeout == 0) {
browserTimeout = -1;
} else {
browserTimeout /= 1000;
}
webdriverContext.setInitParameter("webdriver.server.session.timeout", String.valueOf(sessionTimeout));
webdriverContext.setInitParameter("webdriver.server.browser.timeout", String.valueOf(browserTimeout));
webdriverContext.setAttribute(DriverServlet.SESSIONS_KEY, webDriverSessions);
webdriverContext.setContextPath("/wd");
ServletHandler handler = new ServletHandler();
handler.addServlet("WebDriver remote server", "/hub/*", DriverServlet.class.getName());
webdriverContext.addHandler(handler);

LOGGER.info(format("RemoteWebDriver instances should connect to: http://%s:%d/wd/hub",
networkUtils.getPrivateLocalAddress(), getPort())); // todo: This is still buggy because it
// should resolve to external port

return webdriverContext;
protected void addExtraContexts(Server server, HttpContext context) {
}

private void addStaticContentHandler(boolean slowResources,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@

import org.openqa.selenium.Capabilities;
import org.openqa.selenium.remote.BrowserType;
import org.openqa.selenium.remote.server.DriverSessions;
import org.openqa.selenium.server.RemoteControlConfiguration;

import java.lang.reflect.Constructor;
Expand All @@ -43,7 +42,6 @@ public class BrowserLauncherFactory {

private static final Map<String, Class<? extends BrowserLauncher>> supportedBrowsers =
Maps.newHashMap();
private final DriverSessions webdriverSessions;

static {
supportedBrowsers.put(BrowserType.FIREFOX_PROXY, FirefoxCustomProfileLauncher.class);
Expand All @@ -60,15 +58,6 @@ public class BrowserLauncherFactory {
supportedBrowsers.put(BrowserType.KONQUEROR, KonquerorLauncher.class);
supportedBrowsers.put(BrowserType.MOCK, MockBrowserLauncher.class);
supportedBrowsers.put(BrowserType.GOOGLECHROME, GoogleChromeLauncher.class);
supportedBrowsers.put("webdriver", DrivenSeleniumLauncher.class);
}

public BrowserLauncherFactory() {
this(null);
}

public BrowserLauncherFactory(DriverSessions webdriverSessions) {
this.webdriverSessions = webdriverSessions;
}

/**
Expand Down Expand Up @@ -165,7 +154,7 @@ private RuntimeException browserNotSupported(String browser) {
return new RuntimeException(errorMessage.toString());
}

private BrowserLauncher createBrowserLauncher(Class<? extends BrowserLauncher> c,
protected BrowserLauncher createBrowserLauncher(Class<? extends BrowserLauncher> c,
String browserStartCommand,
String sessionId, RemoteControlConfiguration configuration, Capabilities browserOptions) {
try {
Expand All @@ -177,10 +166,6 @@ private BrowserLauncher createBrowserLauncher(Class<? extends BrowserLauncher> c
browserLauncher =
ctor.newInstance(browserOptions, configuration, sessionId, browserStartCommand);

if (browserLauncher instanceof DrivenSeleniumLauncher) {
((DrivenSeleniumLauncher) browserLauncher).setDriverSessions(webdriverSessions);
}

return browserLauncher;
} catch (InvocationTargetException e) {
throw e.getTargetException();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
limitations under the License.
*/

package org.openqa.selenium.server.browserlaunchers;
package org.openqa.selenium.server.browserlaunchers.webdriven;

import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Throwables;
Expand All @@ -31,6 +31,8 @@
import org.openqa.selenium.remote.server.DriverSessions;
import org.openqa.selenium.remote.server.Session;
import org.openqa.selenium.server.RemoteControlConfiguration;
import org.openqa.selenium.server.browserlaunchers.BrowserLauncher;
import org.openqa.selenium.server.browserlaunchers.ServerHttpChannel;

import java.util.logging.Level;
import java.util.logging.Logger;
Expand Down
17 changes: 15 additions & 2 deletions java/server/src/org/openqa/selenium/server/build.desc
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,26 @@ java_library(name = "configuration",

java_library(name = "base",
srcs = [
"**/*.java",
"webdriven/*.java",
"browserlaunchers/webdriven/*.java",
],
deps = [
":leg-rc-base",
"//java/client/src/com/thoughtworks/selenium/webdriven",
])

java_library(name = "leg-rc-base",
srcs = [
"*.java",
"browserlaunchers/*.java",
"cli/*.java",
"commands/*.java",
"htmlrunner/*.java",
],
deps = [
":configuration",
":resourcemanagement",
"//java/client/src/com/thoughtworks/selenium:api",
"//java/client/src/com/thoughtworks/selenium/webdriven",
"//java/client/src/org/openqa/selenium:codecs",
"//java/client/src/org/openqa/selenium/browserlaunchers/locators",
"//java/client/src/org/openqa/selenium/net",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* Copyright 2011 Software Freedom Conservancy.
*
* Licensed 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.server.webdriven;

import org.openqa.selenium.Capabilities;
import org.openqa.selenium.remote.server.DriverSessions;
import org.openqa.selenium.server.RemoteControlConfiguration;
import org.openqa.selenium.server.browserlaunchers.BrowserLauncher;
import org.openqa.selenium.server.browserlaunchers.BrowserLauncherFactory;
import org.openqa.selenium.server.browserlaunchers.webdriven.DrivenSeleniumLauncher;

public class DrivenBrowserLauncherFactory extends BrowserLauncherFactory {

private final DriverSessions webdriverSessions;

static {
addBrowserLauncher("webdriver", DrivenSeleniumLauncher.class);
}

public DrivenBrowserLauncherFactory(DriverSessions webdriverSessions) {
super();
this.webdriverSessions = webdriverSessions;
}

protected BrowserLauncher createBrowserLauncher(Class<? extends BrowserLauncher> c,
String browserStartCommand,
String sessionId, RemoteControlConfiguration configuration, Capabilities browserOptions) {
final BrowserLauncher browserLauncher = super.createBrowserLauncher(
c, browserStartCommand, sessionId, configuration, browserOptions);
if (browserLauncher instanceof DrivenSeleniumLauncher) {
((DrivenSeleniumLauncher) browserLauncher).setDriverSessions(webdriverSessions);
}
return browserLauncher;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* Copyright 2011 Software Freedom Conservancy.
*
* Licensed 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.server.webdriven;


import org.openqa.selenium.remote.server.DriverSessions;
import org.openqa.selenium.server.SeleniumDriverResourceHandler;
import org.openqa.selenium.server.SeleniumServer;

@SuppressWarnings("serial")
public class DrivenSeleniumDriverResourceHandler extends SeleniumDriverResourceHandler {

public DrivenSeleniumDriverResourceHandler(
SeleniumServer remoteControl, DriverSessions webdriverSessions) {
super(remoteControl, new DrivenBrowserLauncherFactory(webdriverSessions));
}

}
Loading

0 comments on commit f5181c3

Please sign in to comment.