Skip to content

Commit

Permalink
Provide an end-point that emulates the original RC server's
Browse files Browse the repository at this point in the history
This is not intended to be completely accurate, but is designed
to allow users to keep their existing selenium rc tests without
needing to rewrite them all.

Because RC will be removed in Selenium 3 this implementation is
backed by a webdriver-selenium instance.
  • Loading branch information
shs96c committed Oct 7, 2015
1 parent db20d58 commit 083ca70
Show file tree
Hide file tree
Showing 8 changed files with 331 additions and 0 deletions.
1 change: 1 addition & 0 deletions java/client/src/com/thoughtworks/selenium/webdriven/BUCK
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ java_library(name = 'webdriven',
'//java/client/test/org/openqa/selenium/v1:tests',
'//java/client/test/org/openqa/selenium/v1/internal/seleniumemulation:LargeTests',
'//java/client/test/org/openqa/selenium/v1/internal/seleniumemulation:tests',
'//java/server/src/com/thoughtworks/selenium/webdriven:rc-emulation-servlet',
'//java/server/src/org/openqa/grid/selenium:classes',
'//java/server/src/org/openqa/selenium/server:server',
],
Expand Down
20 changes: 20 additions & 0 deletions java/server/src/com/thoughtworks/selenium/webdriven/BUCK
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@

java_library(name = 'rc-emulation-servlet',
srcs = [
'WebDriverBackedSeleniumServlet.java',
],
deps = [
'//java/client/src/com/thoughtworks/selenium:selenium',
'//java/client/src/com/thoughtworks/selenium/webdriven:webdriven',
'//java/client/src/org/openqa/selenium:webdriver-api',
'//java/client/src/org/openqa/selenium/remote:remote',
'//java/server/src/org/openqa/selenium/remote/server:server',
'//java/server/src/org/openqa/selenium/remote/server:sessions',
'//third_party/java/guava-libraries:guava-libraries',
'//third_party/java/servlet:servlet-api',
],
visibility = [
'//java/server/src/org/openqa/selenium/remote/server:standalone-server',
'//java/server/test/com/thoughtworks/selenium/webdriven:webdriven',
],
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,199 @@
// 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 com.thoughtworks.selenium.webdriven;

import static org.openqa.selenium.remote.server.DriverServlet.SESSIONS_KEY;

import com.google.common.base.Joiner;
import com.google.common.base.Supplier;
import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;
import com.google.common.cache.RemovalListener;
import com.google.common.cache.RemovalNotification;
import com.google.common.collect.ImmutableMap;

import com.thoughtworks.selenium.CommandProcessor;
import com.thoughtworks.selenium.SeleniumException;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.remote.BrowserType;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.SessionId;
import org.openqa.selenium.remote.server.DefaultDriverSessions;
import org.openqa.selenium.remote.server.DriverSessions;
import org.openqa.selenium.remote.server.Session;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.concurrent.TimeUnit;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
* An implementation of the original selenium rc server endpoint, using a webdriver-backed selenium
* in order to get things working.
*/
public class WebDriverBackedSeleniumServlet extends HttpServlet {

private static final Random UUID_SEED = new Random();

// Prepare the shared set of thingies
static Cache<SessionId, CommandProcessor> SESSIONS = CacheBuilder.newBuilder()
.expireAfterAccess(5, TimeUnit.MINUTES)
.removalListener(new RemovalListener<SessionId, CommandProcessor>() {
@Override
public void onRemoval(RemovalNotification<SessionId, CommandProcessor> notification) {
CommandProcessor holder = notification.getValue();
if (holder != null) {
try {
holder.stop();
} catch (Exception e) {
// Nothing sane to do.
}
}
}
})
.build();

private final ImmutableMap<String, DesiredCapabilities> drivers =
ImmutableMap.<String, DesiredCapabilities>builder()
.put("*" + BrowserType.FIREFOX_PROXY, DesiredCapabilities.firefox())
.put("*" + BrowserType.FIREFOX, DesiredCapabilities.firefox())
.put("*" + BrowserType.CHROME, DesiredCapabilities.firefox())
.put("*" + BrowserType.FIREFOX_CHROME, DesiredCapabilities.firefox())
.put("*" + BrowserType.IEXPLORE_PROXY, DesiredCapabilities.internetExplorer())
.put("*" + BrowserType.SAFARI, DesiredCapabilities.safari())
.put("*" + BrowserType.IE_HTA, DesiredCapabilities.internetExplorer())
.put("*" + BrowserType.IEXPLORE, DesiredCapabilities.internetExplorer())
.put("*" + BrowserType.GOOGLECHROME, DesiredCapabilities.chrome())
.build();

private final Supplier<DriverSessions> sessionsSupplier;

public WebDriverBackedSeleniumServlet() {
this.sessionsSupplier = new Supplier<DriverSessions>() {
@Override
public DriverSessions get() {
Object attribute = getServletContext().getAttribute(SESSIONS_KEY);
if (attribute == null) {
attribute = new DefaultDriverSessions();
}
return (DriverSessions) attribute;
}
};
}

@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {

String cmd = req.getParameter("cmd");
SessionId sessionId = new SessionId(req.getParameter("sessionId"));
String[] args = deserializeArgs(req);

if (cmd == null) {
resp.sendError(HttpServletResponse.SC_NOT_FOUND);
return;
}

StringBuilder printableArgs = new StringBuilder("[");
Joiner.on(", ").appendTo(printableArgs, args);
printableArgs.append("]");
getServletContext().log(
String.format("Command request: %s%s on session %s", cmd, printableArgs, sessionId));

if ("getNewBrowserSession".equals(cmd)) {
// We wait until we see the start command before actually getting the webdriver instance. For
// now, pre-allocate a session id we can use to refer to the session properly.

// browserStartCommand, browserURL, extensionJs, optionsString
final DesiredCapabilities capabilities = new DesiredCapabilities(drivers.get(args[0]));

try {
sessionId = sessionsSupplier.get().newSession(capabilities);
Session session = sessionsSupplier.get().get(sessionId);
WebDriver driver = session.getDriver();
CommandProcessor commandProcessor = new WebDriverCommandProcessor(args[1], driver);
SESSIONS.put(sessionId, commandProcessor);
sendResponse(resp, sessionId.toString());
} catch (Exception e) {
sendError(resp, "Unable to create session: " + e.getMessage());
}
return;
} else if ("testComplete".equals(cmd)) {
sessionsSupplier.get().deleteSession(sessionId);

CommandProcessor commandProcessor = SESSIONS.getIfPresent(sessionId);
if (commandProcessor == null) {
resp.sendError(HttpServletResponse.SC_NOT_FOUND);
return;
}
SESSIONS.invalidate(sessionId);
sendResponse(resp, null);
return;
}

// Common case.
CommandProcessor commandProcessor = SESSIONS.getIfPresent(sessionId);
if (commandProcessor == null) {
resp.sendError(HttpServletResponse.SC_NOT_FOUND);
return;
}
try {
String result = commandProcessor.doCommand(cmd, args);
sendResponse(resp, result);
} catch (SeleniumException e) {
sendError(resp, e.getMessage());
}
}

private void sendResponse(HttpServletResponse resp, String result) throws IOException {
resp.setStatus(HttpServletResponse.SC_OK);
resp.setCharacterEncoding(StandardCharsets.UTF_8.displayName());
resp.getWriter().append("OK").append(result == null ? "" : "," + result);
resp.flushBuffer();
}

private void sendError(HttpServletResponse resp, String result) throws IOException {
resp.setStatus(HttpServletResponse.SC_OK);
resp.setCharacterEncoding(StandardCharsets.UTF_8.displayName());
resp.getWriter().append("ERROR").append(result == null ? "" : ": " + result);
resp.flushBuffer();
}

private String[] deserializeArgs(HttpServletRequest req) {
// 5 was picked as the maximum length used by the `start` command
List<String> args = new ArrayList<>();
for (int i = 0; i < 5; i++) {
String value = req.getParameter(String.valueOf(i + 1));
if (value != null) {
args.add(value);
} else {
break;
}
}
return args.toArray(new String[args.size()]);
}

}
4 changes: 4 additions & 0 deletions java/server/src/org/openqa/selenium/remote/server/BUCK
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ java_library(name = 'sessions',
'//third_party/java/guava-libraries:guava-libraries',
],
visibility = [
'//java/server/src/com/thoughtworks/selenium/webdriven:rc-emulation-servlet',
'//java/server/src/org/openqa/selenium/server:server',
'//java/server/test/com/thoughtworks/selenium/webdriven:webdriven',
],
)

Expand Down Expand Up @@ -57,7 +59,9 @@ java_library(name = 'server',
'//third_party/java/servlet:servlet-api',
],
visibility = [
'//java/server/src/com/thoughtworks/selenium/webdriven:rc-emulation-servlet',
'//java/server/src/org/openqa/selenium/server:server',
'//java/server/test/com/thoughtworks/selenium/webdriven:webdriven',
],
)

Expand Down
16 changes: 16 additions & 0 deletions java/server/test/com/thoughtworks/selenium/webdriven/BUCK
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@

java_test(name = 'webdriven',
srcs = glob(['*.java']),
deps = [
'//java/client/src/com/thoughtworks/selenium:selenium',
'//java/client/src/org/openqa/selenium/firefox:firefox',
'//java/client/src/org/openqa/selenium/net:net',
'//java/client/test/org/openqa/selenium:helpers',
'//java/client/test/org/openqa/selenium/environment:environment',
'//java/server/src/com/thoughtworks/selenium/webdriven:rc-emulation-servlet',
'//java/server/src/org/openqa/selenium/remote/server:server',
'//third_party/java/jetty:jetty',
'//third_party/java/junit:junit',
'//third_party/java/servlet:servlet-api',
],
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
package com.thoughtworks.selenium.webdriven;


import static org.junit.Assert.assertTrue;

import com.thoughtworks.selenium.DefaultSelenium;
import com.thoughtworks.selenium.Selenium;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.Pages;
import org.openqa.selenium.environment.GlobalTestEnvironment;
import org.openqa.selenium.environment.InProcessTestEnvironment;
import org.openqa.selenium.environment.TestEnvironment;
import org.openqa.selenium.environment.webserver.AppServer;
import org.openqa.selenium.net.PortProber;
import org.openqa.selenium.remote.server.DefaultDriverSessions;
import org.openqa.selenium.remote.server.DriverServlet;
import org.seleniumhq.jetty9.server.Connector;
import org.seleniumhq.jetty9.server.HttpConfiguration;
import org.seleniumhq.jetty9.server.HttpConnectionFactory;
import org.seleniumhq.jetty9.server.Server;
import org.seleniumhq.jetty9.server.ServerConnector;
import org.seleniumhq.jetty9.servlet.ServletContextHandler;

public class WebDriverBackedSeleniumServletTest {

private Server server;
private int port;
private AppServer appServer;
private Pages pages;

@Before
public void setUpServer() throws Exception {
server = new Server();

// Register the emulator
ServletContextHandler handler = new ServletContextHandler();

DefaultDriverSessions webdriverSessions = new DefaultDriverSessions();
handler.setAttribute(DriverServlet.SESSIONS_KEY, webdriverSessions);
handler.setContextPath("/");
handler.addServlet(WebDriverBackedSeleniumServlet.class, "/selenium-server/driver/");
server.setHandler(handler);

// And bind a port
port = PortProber.findFreePort();
HttpConfiguration httpConfig = new HttpConfiguration();
ServerConnector http = new ServerConnector(server, new HttpConnectionFactory(httpConfig));
http.setPort(port);
server.setConnectors(new Connector[]{http});

// Wait until the server is actually started.
server.start();
PortProber.pollPort(port);
}

@Before
public void prepTheEnvironment() {
TestEnvironment environment = GlobalTestEnvironment.get(InProcessTestEnvironment.class);
appServer = environment.getAppServer();

pages = new Pages(appServer);
}

@After
public void stopServer() throws Exception {
if (server != null) {
server.stop();
}
}

@Test
public void searchGoogle() {
Selenium selenium = new DefaultSelenium("localhost", port, "*firefox", appServer.whereIs("/"));
selenium.start();

selenium.open(pages.simpleTestPage);
String text = selenium.getBodyText();

selenium.stop();
assertTrue(text.contains("More than one line of text"));
}
}
1 change: 1 addition & 0 deletions third_party/java/jetty/BUCK
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ java_library(
visibility = [
'//java/client/test/org/openqa/selenium/environment:environment',
'//java/server/src/org/openqa/grid:grid',
'//java/server/test/com/thoughtworks/selenium/webdriven:webdriven',
],
)

Expand Down
5 changes: 5 additions & 0 deletions third_party/java/servlet/BUCK
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ prebuilt_jar(name = 'servlet-api',
visibility = [
'//java/client/test/org/openqa/selenium/environment:environment',

# The standalone selenium server
'//java/server/src/com/thoughtworks/selenium/webdriven:rc-emulation-servlet',
'//java/server/test/com/thoughtworks/selenium/webdriven:webdriven',
'//java/server/src/org/openqa/selenium/remote/server:standalone-server-lib',

# Everything under grid
'//java/server/src/org/openqa/grid/...',
'//java/server/src/org/openqa/selenium/remote/server:server',
Expand Down

0 comments on commit 083ca70

Please sign in to comment.