Skip to content

Commit 1cffe48

Browse files
committed
Implementing ability to generate test pages dynamically from test methods
1 parent 32ff140 commit 1cffe48

File tree

4 files changed

+115
-0
lines changed

4 files changed

+115
-0
lines changed

java/client/test/com/thoughtworks/selenium/testing/SeleniumTestEnvironment.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import org.openqa.selenium.BuckBuild;
2323
import org.openqa.selenium.environment.TestEnvironment;
2424
import org.openqa.selenium.environment.webserver.AppServer;
25+
import org.openqa.selenium.environment.webserver.Page;
2526
import org.openqa.selenium.net.PortProber;
2627
import org.openqa.selenium.net.UrlChecker;
2728
import org.openqa.selenium.os.CommandLine;
@@ -113,6 +114,11 @@ public String whereIsWithCredentials(String relativeUrl, String user, String pas
113114
throw new UnsupportedOperationException("whereIsWithCredentials");
114115
}
115116

117+
@Override
118+
public String create(Page page) {
119+
throw new UnsupportedOperationException("create");
120+
}
121+
116122
@Override
117123
public void start() {
118124
// no-op

java/client/test/org/openqa/selenium/environment/webserver/AppServer.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,10 @@ public interface AppServer {
3131

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

34+
String create(Page page);
35+
3436
void start();
3537

3638
void stop();
39+
3740
}

java/client/test/org/openqa/selenium/environment/webserver/JettyAppServer.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,9 @@
4444
import org.seleniumhq.jetty9.util.ssl.SslContextFactory;
4545

4646
import java.io.File;
47+
import java.io.FileWriter;
48+
import java.io.IOException;
49+
import java.io.Writer;
4750
import java.nio.file.Files;
4851
import java.nio.file.Path;
4952
import java.util.EnumSet;
@@ -63,6 +66,7 @@ public class JettyAppServer implements AppServer {
6366
private static final int DEFAULT_HTTPS_PORT = 2410;
6467
private static final String DEFAULT_CONTEXT_PATH = "/common";
6568
private static final String JS_SRC_CONTEXT_PATH = "/javascript";
69+
private static final String TEMP_SRC_CONTEXT_PATH = "/temp";
6670
private static final String CLOSURE_CONTEXT_PATH = "/third_party/closure/goog";
6771
private static final String THIRD_PARTY_JS_CONTEXT_PATH = "/third_party/js";
6872

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

7579
private ContextHandlerCollection handlers;
7680
private final String hostName;
81+
private File tempPagesDir;
7782

7883
public JettyAppServer() {
7984
this(detectHostname(), getHttpPort(), getHttpsPort());
@@ -102,6 +107,10 @@ public JettyAppServer(String hostName, int httpPort, int httpsPort) {
102107
DEFAULT_CONTEXT_PATH, locate("common/src/web"));
103108
ServletContextHandler jsContext = addResourceHandler(
104109
JS_SRC_CONTEXT_PATH, locate("javascript"));
110+
TemporaryFilesystem tempFs = TemporaryFilesystem.getDefaultTmpFS();
111+
tempPagesDir = tempFs.createTempDir("pages", "test");
112+
ServletContextHandler tempContext = addResourceHandler(
113+
TEMP_SRC_CONTEXT_PATH, tempPagesDir.toPath());
105114
addResourceHandler(CLOSURE_CONTEXT_PATH, locate("third_party/closure/goog"));
106115
addResourceHandler(THIRD_PARTY_JS_CONTEXT_PATH, locate("third_party/js"));
107116

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

181+
@Override
182+
public String create(Page page) {
183+
try {
184+
Path target = Files.createTempFile(tempPagesDir.toPath(), "page", ".html");
185+
try (Writer out = new FileWriter(target.toFile())) {
186+
out.write(page.toString());
187+
}
188+
return String.format("http://%s:%d%s/%s",
189+
getHostName(), port, TEMP_SRC_CONTEXT_PATH, target.getFileName());
190+
} catch (IOException e) {
191+
throw new RuntimeException("Can't create page on test server", e);
192+
}
193+
}
194+
172195
protected String getMainContextPath(String relativeUrl) {
173196
if (!relativeUrl.startsWith("/")) {
174197
relativeUrl = DEFAULT_CONTEXT_PATH + "/" + relativeUrl;
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
// Licensed to the Software Freedom Conservancy (SFC) under one
2+
// or more contributor license agreements. See the NOTICE file
3+
// distributed with this work for additional information
4+
// regarding copyright ownership. The SFC licenses this file
5+
// to you under the Apache License, Version 2.0 (the
6+
// "License"); you may not use this file except in compliance
7+
// with the License. You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing,
12+
// software distributed under the License is distributed on an
13+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
// KIND, either express or implied. See the License for the
15+
// specific language governing permissions and limitations
16+
// under the License.
17+
18+
package org.openqa.selenium.environment.webserver;
19+
20+
import java.util.stream.Collectors;
21+
import java.util.stream.Stream;
22+
23+
public class Page {
24+
25+
private String title = "";
26+
private String[] scripts = {};
27+
private String[] styles = {};
28+
private String[] bodyParts = {};
29+
private String onLoad;
30+
private String onBeforeUnload;
31+
32+
public Page withTitle(String title) {
33+
this.title = title;
34+
return this;
35+
}
36+
37+
public Page withScripts(String... scripts) {
38+
this.scripts = scripts;
39+
return this;
40+
}
41+
42+
public Page withStyles(String... styles) {
43+
this.styles = styles;
44+
return this;
45+
}
46+
47+
public Page withBody(String... bodyParts) {
48+
this.bodyParts = bodyParts;
49+
return this;
50+
}
51+
52+
public Page withOnLoad(String onLoad) {
53+
this.onLoad = onLoad;
54+
return this;
55+
}
56+
57+
public Page withOnBeforeUnload(String onBeforeUnload) {
58+
this.onBeforeUnload = onBeforeUnload;
59+
return this;
60+
}
61+
62+
public String toString() {
63+
return Stream.of(
64+
"<html>",
65+
"<head>",
66+
String.format("<title>%s</title>", title),
67+
"</head>",
68+
"<script type='text/javascript'>",
69+
Stream.of(scripts).collect(Collectors.joining("\n")),
70+
"</script>",
71+
"<style>",
72+
Stream.of(styles).collect(Collectors.joining("\n")),
73+
"</style>",
74+
String.format(
75+
"<body %s %s>",
76+
onLoad == null ? "" : String.format("onload='%s'", onLoad),
77+
onBeforeUnload == null ? "" : String.format("onbeforeunload='%s'", onBeforeUnload)),
78+
Stream.of(bodyParts).collect(Collectors.joining("\n")),
79+
"</body>",
80+
"</html>")
81+
.collect(Collectors.joining("\n"));
82+
}
83+
}

0 commit comments

Comments
 (0)