Skip to content

Commit 09a8a51

Browse files
committed
Hook up the new Core Runner (HTMLLauncher) to the main method.
Do this using oodles of reflection so that the use of the Core Runner doesn't require the user to have the leg-rc package installed.
1 parent 01d9bc7 commit 09a8a51

File tree

3 files changed

+121
-50
lines changed

3 files changed

+121
-50
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package org.openqa.grid.internal.utils.configuration;
2+
3+
import com.beust.jcommander.Parameter;
4+
import com.beust.jcommander.converters.StringConverter;
5+
6+
import java.util.List;
7+
8+
public class CoreRunnerConfiguration extends StandaloneConfiguration {
9+
@Parameter(
10+
names = "-htmlSuite",
11+
arity = 4, // browser string, base URL, test suite URL, results file
12+
description = "Run your tests as a core runner. Parameters are browser string, " +
13+
"base test url, test suite url, path to results file.",
14+
listConverter = StringConverter.class
15+
)
16+
public List<String> htmlSuite;
17+
}
18+

java/server/src/org/openqa/grid/selenium/GridLauncherV3.java

Lines changed: 98 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import org.openqa.grid.common.GridRole;
2525
import org.openqa.grid.common.RegistrationRequest;
2626
import org.openqa.grid.internal.utils.SelfRegisteringRemote;
27+
import org.openqa.grid.internal.utils.configuration.CoreRunnerConfiguration;
2728
import org.openqa.grid.internal.utils.configuration.GridHubConfiguration;
2829
import org.openqa.grid.internal.utils.configuration.GridNodeConfiguration;
2930
import org.openqa.grid.internal.utils.configuration.StandaloneConfiguration;
@@ -35,7 +36,7 @@
3536

3637
import java.io.File;
3738
import java.io.IOException;
38-
import java.util.Arrays;
39+
import java.lang.reflect.Method;
3940
import java.util.function.Supplier;
4041
import java.util.logging.ConsoleHandler;
4142
import java.util.logging.FileHandler;
@@ -46,6 +47,8 @@
4647
public class GridLauncherV3 {
4748

4849
private static final Logger log = Logger.getLogger(GridLauncherV3.class.getName());
50+
private static final String CORE_RUNNER_CLASS =
51+
"org.openqa.selenium.server.htmlrunner.HTMLLauncher";
4952

5053
private static abstract class GridItemLauncher {
5154
protected StandaloneConfiguration configuration;
@@ -57,53 +60,7 @@ void printUsage() {
5760
}
5861
}
5962

60-
private static ImmutableMap<GridRole, Supplier<GridItemLauncher>> LAUNCHERS =
61-
ImmutableMap.<GridRole, Supplier<GridItemLauncher>>builder()
62-
.put(GridRole.NOT_GRID, () -> new GridItemLauncher() {
63-
public void setConfiguration(String[] args) {
64-
configuration = new StandaloneConfiguration();
65-
new JCommander(configuration, args);
66-
helpRequested = configuration.help;
67-
}
68-
69-
public void launch() throws Exception {
70-
log.info("Launching a standalone Selenium Server");
71-
SeleniumServer server = new SeleniumServer(configuration);
72-
server.boot();
73-
log.info("Selenium Server is up and running");
74-
}
75-
})
76-
.put(GridRole.HUB, () -> new GridItemLauncher() {
77-
public void setConfiguration(String[] args) {
78-
configuration = new GridHubConfiguration();
79-
new JCommander(configuration, args);
80-
helpRequested = configuration.help;
81-
}
82-
public void launch() throws Exception {
83-
log.info("Launching Selenium Grid hub");
84-
Hub h = new Hub((GridHubConfiguration) configuration);
85-
h.start();
86-
log.info("Nodes should register to " + h.getRegistrationURL());
87-
log.info("Selenium Grid hub is up and running");
88-
}
89-
})
90-
.put(GridRole.NODE, () -> new GridItemLauncher() {
91-
public void setConfiguration(String[] args) {
92-
configuration = new GridNodeConfiguration();
93-
new JCommander(configuration, args);
94-
helpRequested = configuration.help;
95-
}
96-
public void launch() throws Exception {
97-
log.info("Launching a Selenium Grid node");
98-
RegistrationRequest c = RegistrationRequest.build((GridNodeConfiguration) configuration);
99-
SelfRegisteringRemote remote = new SelfRegisteringRemote(c);
100-
remote.setRemoteServer(new SeleniumServer(configuration));
101-
remote.startRemoteServer();
102-
log.info("Selenium Grid node is up and ready to register to the hub");
103-
remote.startRegistrationProcess();
104-
}
105-
})
106-
.build();
63+
private static ImmutableMap<String, Supplier<GridItemLauncher>> LAUNCHERS = buildLaunchers();
10764

10865
public static void main(String[] args) throws Exception {
10966
GridItemLauncher launcher = buildLauncher(args);
@@ -135,6 +92,11 @@ private static GridItemLauncher buildLauncher(String[] args) {
13592
String role = "standalone";
13693

13794
for (int i = 0; i < args.length; i++) {
95+
if (args[i].equals("-htmlSuite")) {
96+
GridItemLauncher launcher = LAUNCHERS.get("corerunner").get();
97+
launcher.setConfiguration(args);
98+
return launcher;
99+
}
138100
if (args[i].startsWith("-role=")) {
139101
role = args[i].substring("-role=".length());
140102
} else if (args[i].equals("-role")) {
@@ -224,4 +186,92 @@ private static void configureLogging(StandaloneConfiguration configuration) {
224186
}
225187
}
226188
}
189+
190+
private static ImmutableMap<String, Supplier<GridItemLauncher>> buildLaunchers() {
191+
ImmutableMap.Builder<String, Supplier<GridItemLauncher>> launchers =
192+
ImmutableMap.<String, Supplier<GridItemLauncher>>builder()
193+
.put(GridRole.NOT_GRID.toString(), () -> new GridItemLauncher() {
194+
public void setConfiguration(String[] args) {
195+
configuration = new StandaloneConfiguration();
196+
new JCommander(configuration, args);
197+
helpRequested = configuration.help;
198+
}
199+
200+
public void launch() throws Exception {
201+
log.info("Launching a standalone Selenium Server");
202+
SeleniumServer server = new SeleniumServer(configuration);
203+
server.boot();
204+
log.info("Selenium Server is up and running");
205+
}
206+
})
207+
.put(GridRole.HUB.toString(), () -> new GridItemLauncher() {
208+
public void setConfiguration(String[] args) {
209+
configuration = new GridHubConfiguration();
210+
new JCommander(configuration, args);
211+
helpRequested = configuration.help;
212+
}
213+
214+
public void launch() throws Exception {
215+
log.info("Launching Selenium Grid hub");
216+
Hub h = new Hub((GridHubConfiguration) configuration);
217+
h.start();
218+
log.info("Nodes should register to " + h.getRegistrationURL());
219+
log.info("Selenium Grid hub is up and running");
220+
}
221+
})
222+
.put(GridRole.NODE.toString(), () -> new GridItemLauncher() {
223+
public void setConfiguration(String[] args) {
224+
configuration = new GridNodeConfiguration();
225+
new JCommander(configuration, args);
226+
helpRequested = configuration.help;
227+
}
228+
229+
public void launch() throws Exception {
230+
log.info("Launching a Selenium Grid node");
231+
RegistrationRequest
232+
c =
233+
RegistrationRequest.build((GridNodeConfiguration) configuration);
234+
SelfRegisteringRemote remote = new SelfRegisteringRemote(c);
235+
remote.setRemoteServer(new SeleniumServer(configuration));
236+
remote.startRemoteServer();
237+
log.info("Selenium Grid node is up and ready to register to the hub");
238+
remote.startRegistrationProcess();
239+
}
240+
});
241+
242+
try {
243+
Class.forName(CORE_RUNNER_CLASS, false, GridLauncherV3.class.getClassLoader());
244+
245+
launchers.put("corerunner", () -> new GridItemLauncher() {
246+
@Override
247+
void setConfiguration(String[] args) {
248+
configuration = new CoreRunnerConfiguration();
249+
new JCommander(configuration, args);
250+
helpRequested = configuration.help;
251+
}
252+
253+
@Override
254+
void launch() throws Exception {
255+
Class<?> coreRunnerClass = Class.forName(CORE_RUNNER_CLASS);
256+
Object coreRunner = coreRunnerClass.newInstance();
257+
Method mainInt = coreRunnerClass.getMethod("mainInt", String[].class);
258+
259+
CoreRunnerConfiguration runnerConfig = (CoreRunnerConfiguration) this.configuration;
260+
String[] args = new String[] {
261+
/* Results file */ runnerConfig.htmlSuite.get(3),
262+
/* suite */ runnerConfig.htmlSuite.get(2),
263+
/* start url */ runnerConfig.htmlSuite.get(1),
264+
/* multi window */ "true",
265+
/* browser string */ runnerConfig.htmlSuite.get(0),
266+
};
267+
Integer result = (Integer) mainInt.invoke(coreRunner, (Object) args);
268+
System.exit(result);
269+
}
270+
});
271+
} catch (ReflectiveOperationException e) {
272+
// Do nothing. It's fine.
273+
}
274+
275+
return launchers.build();
276+
}
227277
}

java/server/src/org/openqa/selenium/server/htmlrunner/HTMLLauncher.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -128,8 +128,11 @@ public String runHTMLSuite(String browser, String browserURL, String suiteURL, F
128128
*/
129129
private String runHTMLSuite(String browser, String browserURL, String suiteURL, File outputFile,
130130
long timeoutInSeconds, boolean multiWindow, String defaultLogLevel) throws IOException {
131-
outputFile.createNewFile();
132-
if (!outputFile.canWrite()) {
131+
File parent = outputFile.getParentFile();
132+
if (parent != null && !parent.exists()) {
133+
parent.mkdirs();
134+
}
135+
if (outputFile.exists() && !outputFile.canWrite()) {
133136
throw new IOException("Can't write to outputFile: " + outputFile.getAbsolutePath());
134137
}
135138
long timeoutInMs = 1000L * timeoutInSeconds;

0 commit comments

Comments
 (0)