Skip to content

Commit

Permalink
Hook up the new Core Runner (HTMLLauncher) to the main method.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
shs96c committed Jun 25, 2016
1 parent 01d9bc7 commit 09a8a51
Show file tree
Hide file tree
Showing 3 changed files with 121 additions and 50 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package org.openqa.grid.internal.utils.configuration;

import com.beust.jcommander.Parameter;
import com.beust.jcommander.converters.StringConverter;

import java.util.List;

public class CoreRunnerConfiguration extends StandaloneConfiguration {
@Parameter(
names = "-htmlSuite",
arity = 4, // browser string, base URL, test suite URL, results file
description = "Run your tests as a core runner. Parameters are browser string, " +
"base test url, test suite url, path to results file.",
listConverter = StringConverter.class
)
public List<String> htmlSuite;
}

146 changes: 98 additions & 48 deletions java/server/src/org/openqa/grid/selenium/GridLauncherV3.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import org.openqa.grid.common.GridRole;
import org.openqa.grid.common.RegistrationRequest;
import org.openqa.grid.internal.utils.SelfRegisteringRemote;
import org.openqa.grid.internal.utils.configuration.CoreRunnerConfiguration;
import org.openqa.grid.internal.utils.configuration.GridHubConfiguration;
import org.openqa.grid.internal.utils.configuration.GridNodeConfiguration;
import org.openqa.grid.internal.utils.configuration.StandaloneConfiguration;
Expand All @@ -35,7 +36,7 @@

import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.lang.reflect.Method;
import java.util.function.Supplier;
import java.util.logging.ConsoleHandler;
import java.util.logging.FileHandler;
Expand All @@ -46,6 +47,8 @@
public class GridLauncherV3 {

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

private static abstract class GridItemLauncher {
protected StandaloneConfiguration configuration;
Expand All @@ -57,53 +60,7 @@ void printUsage() {
}
}

private static ImmutableMap<GridRole, Supplier<GridItemLauncher>> LAUNCHERS =
ImmutableMap.<GridRole, Supplier<GridItemLauncher>>builder()
.put(GridRole.NOT_GRID, () -> new GridItemLauncher() {
public void setConfiguration(String[] args) {
configuration = new StandaloneConfiguration();
new JCommander(configuration, args);
helpRequested = configuration.help;
}

public void launch() throws Exception {
log.info("Launching a standalone Selenium Server");
SeleniumServer server = new SeleniumServer(configuration);
server.boot();
log.info("Selenium Server is up and running");
}
})
.put(GridRole.HUB, () -> new GridItemLauncher() {
public void setConfiguration(String[] args) {
configuration = new GridHubConfiguration();
new JCommander(configuration, args);
helpRequested = configuration.help;
}
public void launch() throws Exception {
log.info("Launching Selenium Grid hub");
Hub h = new Hub((GridHubConfiguration) configuration);
h.start();
log.info("Nodes should register to " + h.getRegistrationURL());
log.info("Selenium Grid hub is up and running");
}
})
.put(GridRole.NODE, () -> new GridItemLauncher() {
public void setConfiguration(String[] args) {
configuration = new GridNodeConfiguration();
new JCommander(configuration, args);
helpRequested = configuration.help;
}
public void launch() throws Exception {
log.info("Launching a Selenium Grid node");
RegistrationRequest c = RegistrationRequest.build((GridNodeConfiguration) configuration);
SelfRegisteringRemote remote = new SelfRegisteringRemote(c);
remote.setRemoteServer(new SeleniumServer(configuration));
remote.startRemoteServer();
log.info("Selenium Grid node is up and ready to register to the hub");
remote.startRegistrationProcess();
}
})
.build();
private static ImmutableMap<String, Supplier<GridItemLauncher>> LAUNCHERS = buildLaunchers();

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

for (int i = 0; i < args.length; i++) {
if (args[i].equals("-htmlSuite")) {
GridItemLauncher launcher = LAUNCHERS.get("corerunner").get();
launcher.setConfiguration(args);
return launcher;
}
if (args[i].startsWith("-role=")) {
role = args[i].substring("-role=".length());
} else if (args[i].equals("-role")) {
Expand Down Expand Up @@ -224,4 +186,92 @@ private static void configureLogging(StandaloneConfiguration configuration) {
}
}
}

private static ImmutableMap<String, Supplier<GridItemLauncher>> buildLaunchers() {
ImmutableMap.Builder<String, Supplier<GridItemLauncher>> launchers =
ImmutableMap.<String, Supplier<GridItemLauncher>>builder()
.put(GridRole.NOT_GRID.toString(), () -> new GridItemLauncher() {
public void setConfiguration(String[] args) {
configuration = new StandaloneConfiguration();
new JCommander(configuration, args);
helpRequested = configuration.help;
}

public void launch() throws Exception {
log.info("Launching a standalone Selenium Server");
SeleniumServer server = new SeleniumServer(configuration);
server.boot();
log.info("Selenium Server is up and running");
}
})
.put(GridRole.HUB.toString(), () -> new GridItemLauncher() {
public void setConfiguration(String[] args) {
configuration = new GridHubConfiguration();
new JCommander(configuration, args);
helpRequested = configuration.help;
}

public void launch() throws Exception {
log.info("Launching Selenium Grid hub");
Hub h = new Hub((GridHubConfiguration) configuration);
h.start();
log.info("Nodes should register to " + h.getRegistrationURL());
log.info("Selenium Grid hub is up and running");
}
})
.put(GridRole.NODE.toString(), () -> new GridItemLauncher() {
public void setConfiguration(String[] args) {
configuration = new GridNodeConfiguration();
new JCommander(configuration, args);
helpRequested = configuration.help;
}

public void launch() throws Exception {
log.info("Launching a Selenium Grid node");
RegistrationRequest
c =
RegistrationRequest.build((GridNodeConfiguration) configuration);
SelfRegisteringRemote remote = new SelfRegisteringRemote(c);
remote.setRemoteServer(new SeleniumServer(configuration));
remote.startRemoteServer();
log.info("Selenium Grid node is up and ready to register to the hub");
remote.startRegistrationProcess();
}
});

try {
Class.forName(CORE_RUNNER_CLASS, false, GridLauncherV3.class.getClassLoader());

launchers.put("corerunner", () -> new GridItemLauncher() {
@Override
void setConfiguration(String[] args) {
configuration = new CoreRunnerConfiguration();
new JCommander(configuration, args);
helpRequested = configuration.help;
}

@Override
void launch() throws Exception {
Class<?> coreRunnerClass = Class.forName(CORE_RUNNER_CLASS);
Object coreRunner = coreRunnerClass.newInstance();
Method mainInt = coreRunnerClass.getMethod("mainInt", String[].class);

CoreRunnerConfiguration runnerConfig = (CoreRunnerConfiguration) this.configuration;
String[] args = new String[] {
/* Results file */ runnerConfig.htmlSuite.get(3),
/* suite */ runnerConfig.htmlSuite.get(2),
/* start url */ runnerConfig.htmlSuite.get(1),
/* multi window */ "true",
/* browser string */ runnerConfig.htmlSuite.get(0),
};
Integer result = (Integer) mainInt.invoke(coreRunner, (Object) args);
System.exit(result);
}
});
} catch (ReflectiveOperationException e) {
// Do nothing. It's fine.
}

return launchers.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,11 @@ public String runHTMLSuite(String browser, String browserURL, String suiteURL, F
*/
private String runHTMLSuite(String browser, String browserURL, String suiteURL, File outputFile,
long timeoutInSeconds, boolean multiWindow, String defaultLogLevel) throws IOException {
outputFile.createNewFile();
if (!outputFile.canWrite()) {
File parent = outputFile.getParentFile();
if (parent != null && !parent.exists()) {
parent.mkdirs();
}
if (outputFile.exists() && !outputFile.canWrite()) {
throw new IOException("Can't write to outputFile: " + outputFile.getAbsolutePath());
}
long timeoutInMs = 1000L * timeoutInSeconds;
Expand Down

0 comments on commit 09a8a51

Please sign in to comment.