Skip to content

Commit

Permalink
Implementing ability to generate test pages dynamically from test met…
Browse files Browse the repository at this point in the history
…hods
  • Loading branch information
barancev committed Apr 9, 2017
1 parent 32ff140 commit 1cffe48
Show file tree
Hide file tree
Showing 4 changed files with 115 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import org.openqa.selenium.BuckBuild;
import org.openqa.selenium.environment.TestEnvironment;
import org.openqa.selenium.environment.webserver.AppServer;
import org.openqa.selenium.environment.webserver.Page;
import org.openqa.selenium.net.PortProber;
import org.openqa.selenium.net.UrlChecker;
import org.openqa.selenium.os.CommandLine;
Expand Down Expand Up @@ -113,6 +114,11 @@ public String whereIsWithCredentials(String relativeUrl, String user, String pas
throw new UnsupportedOperationException("whereIsWithCredentials");
}

@Override
public String create(Page page) {
throw new UnsupportedOperationException("create");
}

@Override
public void start() {
// no-op
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,10 @@ public interface AppServer {

String whereIsWithCredentials(String relativeUrl, String user, String password);

String create(Page page);

void start();

void stop();

}
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@
import org.seleniumhq.jetty9.util.ssl.SslContextFactory;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.EnumSet;
Expand All @@ -63,6 +66,7 @@ public class JettyAppServer implements AppServer {
private static final int DEFAULT_HTTPS_PORT = 2410;
private static final String DEFAULT_CONTEXT_PATH = "/common";
private static final String JS_SRC_CONTEXT_PATH = "/javascript";
private static final String TEMP_SRC_CONTEXT_PATH = "/temp";
private static final String CLOSURE_CONTEXT_PATH = "/third_party/closure/goog";
private static final String THIRD_PARTY_JS_CONTEXT_PATH = "/third_party/js";

Expand All @@ -74,6 +78,7 @@ public class JettyAppServer implements AppServer {

private ContextHandlerCollection handlers;
private final String hostName;
private File tempPagesDir;

public JettyAppServer() {
this(detectHostname(), getHttpPort(), getHttpsPort());
Expand Down Expand Up @@ -102,6 +107,10 @@ public JettyAppServer(String hostName, int httpPort, int httpsPort) {
DEFAULT_CONTEXT_PATH, locate("common/src/web"));
ServletContextHandler jsContext = addResourceHandler(
JS_SRC_CONTEXT_PATH, locate("javascript"));
TemporaryFilesystem tempFs = TemporaryFilesystem.getDefaultTmpFS();
tempPagesDir = tempFs.createTempDir("pages", "test");
ServletContextHandler tempContext = addResourceHandler(
TEMP_SRC_CONTEXT_PATH, tempPagesDir.toPath());
addResourceHandler(CLOSURE_CONTEXT_PATH, locate("third_party/closure/goog"));
addResourceHandler(THIRD_PARTY_JS_CONTEXT_PATH, locate("third_party/js"));

Expand Down Expand Up @@ -169,6 +178,20 @@ public String whereIsWithCredentials(String relativeUrl, String user, String pas
return "http://" + user + ":" + pass + "@" + getHostName() + ":" + port + relativeUrl;
}

@Override
public String create(Page page) {
try {
Path target = Files.createTempFile(tempPagesDir.toPath(), "page", ".html");
try (Writer out = new FileWriter(target.toFile())) {
out.write(page.toString());
}
return String.format("http://%s:%d%s/%s",
getHostName(), port, TEMP_SRC_CONTEXT_PATH, target.getFileName());
} catch (IOException e) {
throw new RuntimeException("Can't create page on test server", e);
}
}

protected String getMainContextPath(String relativeUrl) {
if (!relativeUrl.startsWith("/")) {
relativeUrl = DEFAULT_CONTEXT_PATH + "/" + relativeUrl;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
// 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.environment.webserver;

import java.util.stream.Collectors;
import java.util.stream.Stream;

public class Page {

private String title = "";
private String[] scripts = {};
private String[] styles = {};
private String[] bodyParts = {};
private String onLoad;
private String onBeforeUnload;

public Page withTitle(String title) {
this.title = title;
return this;
}

public Page withScripts(String... scripts) {
this.scripts = scripts;
return this;
}

public Page withStyles(String... styles) {
this.styles = styles;
return this;
}

public Page withBody(String... bodyParts) {
this.bodyParts = bodyParts;
return this;
}

public Page withOnLoad(String onLoad) {
this.onLoad = onLoad;
return this;
}

public Page withOnBeforeUnload(String onBeforeUnload) {
this.onBeforeUnload = onBeforeUnload;
return this;
}

public String toString() {
return Stream.of(
"<html>",
"<head>",
String.format("<title>%s</title>", title),
"</head>",
"<script type='text/javascript'>",
Stream.of(scripts).collect(Collectors.joining("\n")),
"</script>",
"<style>",
Stream.of(styles).collect(Collectors.joining("\n")),
"</style>",
String.format(
"<body %s %s>",
onLoad == null ? "" : String.format("onload='%s'", onLoad),
onBeforeUnload == null ? "" : String.format("onbeforeunload='%s'", onBeforeUnload)),
Stream.of(bodyParts).collect(Collectors.joining("\n")),
"</body>",
"</html>")
.collect(Collectors.joining("\n"));
}
}

0 comments on commit 1cffe48

Please sign in to comment.