From 083ca704edc0e38ea709568ae15f89bf4e245cba Mon Sep 17 00:00:00 2001 From: Simon Stewart Date: Tue, 6 Oct 2015 10:54:01 +0100 Subject: [PATCH] Provide an end-point that emulates the original RC server's 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. --- .../com/thoughtworks/selenium/webdriven/BUCK | 1 + .../com/thoughtworks/selenium/webdriven/BUCK | 20 ++ .../WebDriverBackedSeleniumServlet.java | 199 ++++++++++++++++++ .../org/openqa/selenium/remote/server/BUCK | 4 + .../com/thoughtworks/selenium/webdriven/BUCK | 16 ++ .../WebDriverBackedSeleniumServletTest.java | 85 ++++++++ third_party/java/jetty/BUCK | 1 + third_party/java/servlet/BUCK | 5 + 8 files changed, 331 insertions(+) create mode 100644 java/server/src/com/thoughtworks/selenium/webdriven/BUCK create mode 100644 java/server/src/com/thoughtworks/selenium/webdriven/WebDriverBackedSeleniumServlet.java create mode 100644 java/server/test/com/thoughtworks/selenium/webdriven/BUCK create mode 100644 java/server/test/com/thoughtworks/selenium/webdriven/WebDriverBackedSeleniumServletTest.java diff --git a/java/client/src/com/thoughtworks/selenium/webdriven/BUCK b/java/client/src/com/thoughtworks/selenium/webdriven/BUCK index e712b2ea32576..cfe7da5ec60fe 100644 --- a/java/client/src/com/thoughtworks/selenium/webdriven/BUCK +++ b/java/client/src/com/thoughtworks/selenium/webdriven/BUCK @@ -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', ], diff --git a/java/server/src/com/thoughtworks/selenium/webdriven/BUCK b/java/server/src/com/thoughtworks/selenium/webdriven/BUCK new file mode 100644 index 0000000000000..619d84dc66b8c --- /dev/null +++ b/java/server/src/com/thoughtworks/selenium/webdriven/BUCK @@ -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', + ], +) diff --git a/java/server/src/com/thoughtworks/selenium/webdriven/WebDriverBackedSeleniumServlet.java b/java/server/src/com/thoughtworks/selenium/webdriven/WebDriverBackedSeleniumServlet.java new file mode 100644 index 0000000000000..8e019feb1fd27 --- /dev/null +++ b/java/server/src/com/thoughtworks/selenium/webdriven/WebDriverBackedSeleniumServlet.java @@ -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 SESSIONS = CacheBuilder.newBuilder() + .expireAfterAccess(5, TimeUnit.MINUTES) + .removalListener(new RemovalListener() { + @Override + public void onRemoval(RemovalNotification notification) { + CommandProcessor holder = notification.getValue(); + if (holder != null) { + try { + holder.stop(); + } catch (Exception e) { + // Nothing sane to do. + } + } + } + }) + .build(); + + private final ImmutableMap drivers = + ImmutableMap.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 sessionsSupplier; + + public WebDriverBackedSeleniumServlet() { + this.sessionsSupplier = new Supplier() { + @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 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()]); + } + +} diff --git a/java/server/src/org/openqa/selenium/remote/server/BUCK b/java/server/src/org/openqa/selenium/remote/server/BUCK index 65fa724830e92..b16c5f6e38c44 100644 --- a/java/server/src/org/openqa/selenium/remote/server/BUCK +++ b/java/server/src/org/openqa/selenium/remote/server/BUCK @@ -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', ], ) @@ -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', ], ) diff --git a/java/server/test/com/thoughtworks/selenium/webdriven/BUCK b/java/server/test/com/thoughtworks/selenium/webdriven/BUCK new file mode 100644 index 0000000000000..6e2a7ba7d4432 --- /dev/null +++ b/java/server/test/com/thoughtworks/selenium/webdriven/BUCK @@ -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', + ], +) diff --git a/java/server/test/com/thoughtworks/selenium/webdriven/WebDriverBackedSeleniumServletTest.java b/java/server/test/com/thoughtworks/selenium/webdriven/WebDriverBackedSeleniumServletTest.java new file mode 100644 index 0000000000000..0c5553009f5a2 --- /dev/null +++ b/java/server/test/com/thoughtworks/selenium/webdriven/WebDriverBackedSeleniumServletTest.java @@ -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")); + } +} diff --git a/third_party/java/jetty/BUCK b/third_party/java/jetty/BUCK index 8eefed2494a12..c568cd281ad45 100644 --- a/third_party/java/jetty/BUCK +++ b/third_party/java/jetty/BUCK @@ -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', ], ) diff --git a/third_party/java/servlet/BUCK b/third_party/java/servlet/BUCK index d42730582d72a..9b66a53fb8e96 100644 --- a/third_party/java/servlet/BUCK +++ b/third_party/java/servlet/BUCK @@ -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',