Skip to content

Commit

Permalink
Allow the new HTMLRunner to run suites from the local filesystem.
Browse files Browse the repository at this point in the history
We do this by starting an HTTPd and serving them off the disk
because some drivers have a different security model between
file and http URLs and we don't want to cause grief.
  • Loading branch information
shs96c committed Jun 18, 2016
1 parent 73a2cce commit 0e92301
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 12 deletions.
11 changes: 7 additions & 4 deletions java/client/src/org/openqa/selenium/internal/SocketLock.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

import org.openqa.selenium.WebDriverException;

import java.io.Closeable;
import java.io.IOException;
import java.net.BindException;
import java.net.InetSocketAddress;
Expand All @@ -31,7 +32,7 @@
*
* @author gregory.block@gmail.com (Gregory Block)
*/
public class SocketLock implements Lock {
public class SocketLock implements Closeable, Lock {
public static final int DEFAULT_PORT = 7055;
private static final long DELAY_BETWEEN_SOCKET_CHECKS = 2000;

Expand Down Expand Up @@ -102,9 +103,11 @@ public void lock(long timeoutInMillis) throws WebDriverException {
}
}

/**
*
*/
@Override
public void close() throws IOException {
unlock();
}

public void unlock() {
try {
if (lockSocket.isBound()) lockSocket.close();
Expand Down
1 change: 1 addition & 0 deletions java/server/src/org/openqa/selenium/server/htmlrunner/BUCK
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ java_library(
'//java/client/src/org/openqa/selenium/remote:remote',
'//java/client/src/org/openqa/selenium/safari:safari',
'//third_party/java/guava:guava',
'//third_party/java/jetty:jetty',
],
visibility = [
'//java/server/src/com/thoughtworks/selenium:leg-rc',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,28 @@
import org.openqa.selenium.edge.EdgeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.internal.SocketLock;
import org.openqa.selenium.net.PortProber;
import org.openqa.selenium.opera.OperaDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.safari.SafariDriver;
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.server.handler.ContextHandler;
import org.seleniumhq.jetty9.server.handler.ResourceHandler;
import org.seleniumhq.jetty9.util.resource.Resource;

import java.io.File;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.logging.Level;
import java.util.logging.Logger;

Expand All @@ -48,14 +61,11 @@
*/
public class HTMLLauncher implements HTMLResultsListener {

static Logger log = Logger.getLogger(HTMLLauncher.class.getName());
// private SeleniumServer remoteControl;
private static Logger log = Logger.getLogger(HTMLLauncher.class.getName());

private Server server;
// private HTMLTestResults results;
//
// public HTMLLauncher(SeleniumServer remoteControl) {
// this.remoteControl = remoteControl;
// }
//

/**
* Launches a single HTML Selenium test suite.
*
Expand Down Expand Up @@ -136,6 +146,15 @@ private String runHTMLSuite(String browser, String browserURL, String suiteURL,

return results.isSuccessful() ? "PASSED" : "FAILED";
} finally {
if (server != null) {
try {
server.stop();
} catch (Exception e) {
// Nothing sane to do. Log the error and carry on
log.log(Level.INFO, "Exception shutting down server. You may ignore this.", e);
}
}

driver.quit();
}

Expand Down Expand Up @@ -194,6 +213,41 @@ private URL determineSuiteUrl(String browserUrl, String suiteURL) throws IOExcep
return verifySuiteUrl(new URL(suiteURL));
}

// Is the suiteURL a file?
Path path = Paths.get(suiteURL);
if (Files.exists(path)) {
// Not all drivers can read files from the disk, so we need to host the suite somewhere.
try (SocketLock lock = new SocketLock()) {
server = new Server();
HttpConfiguration httpConfig = new HttpConfiguration();

ServerConnector http = new ServerConnector(server, new HttpConnectionFactory(httpConfig));
int port = PortProber.findFreePort();
http.setPort(port);
http.setIdleTimeout(500000);
server.setConnectors(new Connector[]{http});

ResourceHandler handler = new ResourceHandler();
handler.setDirectoriesListed(true);
handler.setWelcomeFiles(new String[]{"index.html"});
handler.setBaseResource(Resource.newResource(path.toFile()));

ContextHandler context = new ContextHandler("/tests");
context.setHandler(handler);

server.setHandler(handler);
server.start();

PortProber.pollPort(port);

URL serverUrl = server.getURI().toURL();
return new URL(serverUrl.getProtocol(), serverUrl.getHost(), serverUrl.getPort(),
"/tests/");
} catch (Exception e) {
throw new IOException(e);
}
}

// Well then, it must be a URL relative to whatever the browserUrl. Probe and find out.
URL browser = new URL(browserUrl);
return verifySuiteUrl(new URL(browser, suiteURL));
Expand Down
3 changes: 2 additions & 1 deletion third_party/java/jetty/BUCK
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@ java_library(
'//java/client/test/com/thoughtworks/selenium:tests',
'//java/client/test/org/openqa/selenium:tests',
'//java/client/test/org/openqa/selenium/environment:environment',
'//java/server/src/org/openqa/selenium/remote/server:standalone-server-lib',
'//java/server/src/org/openqa/grid:grid',
'//java/server/src/org/openqa/selenium/remote/server:standalone-server-lib',
'//java/server/src/org/openqa/selenium/server/htmlrunner:htmlrunner',
'//java/server/test/org/openqa/selenium:lib',
'//java/server/test/com/thoughtworks/selenium/webdriven:webdriven',
'//java/server/test/org/openqa/selenium/remote/server:tests',
Expand Down

0 comments on commit 0e92301

Please sign in to comment.