Skip to content

Commit

Permalink
Refactoring tests for grid launcher
Browse files Browse the repository at this point in the history
  • Loading branch information
barancev committed Mar 1, 2018
1 parent 9537053 commit 02ebcc1
Showing 1 changed file with 71 additions and 50 deletions.
121 changes: 71 additions & 50 deletions java/server/test/org/openqa/grid/e2e/misc/GridViaCommandLineTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

import static org.hamcrest.CoreMatchers.containsString;
import static org.hamcrest.CoreMatchers.instanceOf;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.startsWith;
import static org.hamcrest.Matchers.greaterThan;
import static org.hamcrest.Matchers.lessThan;
Expand All @@ -28,13 +29,15 @@

import com.google.common.base.Function;

import org.junit.After;
import org.junit.Test;
import org.openqa.grid.internal.utils.configuration.GridHubConfiguration;
import org.openqa.grid.selenium.GridLauncherV3;
import org.openqa.grid.shared.Stoppable;
import org.openqa.grid.web.Hub;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.json.Json;
import org.openqa.selenium.net.PortProber;
import org.openqa.selenium.net.UrlChecker;
import org.openqa.selenium.remote.DesiredCapabilities;
Expand All @@ -57,6 +60,7 @@
import java.nio.file.Files;
import java.nio.file.Path;
import java.time.Duration;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.TimeUnit;

Expand All @@ -65,6 +69,19 @@
*/
public class GridViaCommandLineTest {

private Optional<Stoppable> server;
private Optional<Stoppable> node;

@After
public void stopServer() {
if (server != null) {
server.ifPresent(Stoppable::stop);
}
if (node != null) {
node.ifPresent(Stoppable::stop);
}
}

@Test
public void unrecognizedRole() {
ByteArrayOutputStream outSpy = new ByteArrayOutputStream();
Expand Down Expand Up @@ -114,63 +131,70 @@ public void canRedirectLogToFile() throws Exception {
Integer port = PortProber.findFreePort();
Path tempLog = Files.createTempFile("test", ".log");
String[] args = {"-log", tempLog.toString(), "-port", port.toString()};
Optional<Stoppable> server = new GridLauncherV3(args).launch();

server = new GridLauncherV3(args).launch();
assertTrue(server.isPresent());
waitUntilServerIsAvailableOnPort(port);

String log = String.join("", Files.readAllLines(tempLog));
assertThat(log, containsString("Selenium Server is up and running on port " + port));
server.get().stop();
}

@Test
public void canLaunchStandalone() throws IOException {
public void canLaunchStandalone() throws Exception {
Integer port = PortProber.findFreePort();
ByteArrayOutputStream outSpy = new ByteArrayOutputStream();
String[] args = {"-role", "standalone", "-port", port.toString()};
Optional<Stoppable> server = new GridLauncherV3(new PrintStream(outSpy), args).launch();

server = new GridLauncherV3(new PrintStream(outSpy), args).launch();
assertTrue(server.isPresent());
assertThat(server.get(), instanceOf(SeleniumServer.class));
waitUntilServerIsAvailableOnPort(port);

String url = "http://localhost:" + port;
HttpClient client = HttpClient.Factory.createDefault().createClient(new URL(url));
HttpRequest req = new HttpRequest(HttpMethod.GET, "/");
String content = client.execute(req).getContentString();
String content = getContentOf(port, "/");
assertThat(content, containsString("Whoops! The URL specified routes to this help page."));

server.get().stop();
String status = getContentOf(port, "/wd/hub/status");
Map<?, ?> statusMap = new Json().toType(status, Map.class);
assertThat(0L, is(statusMap.get("status")));
}

@Test
public void launchesStandaloneByDefault() {
public void launchesStandaloneByDefault() throws Exception {
Integer port = PortProber.findFreePort();
ByteArrayOutputStream outSpy = new ByteArrayOutputStream();
String[] args = {"-port", port.toString()};
Optional<Stoppable> server = new GridLauncherV3(new PrintStream(outSpy), args).launch();

server = new GridLauncherV3(new PrintStream(outSpy), args).launch();
assertTrue(server.isPresent());
assertThat(server.get(), instanceOf(SeleniumServer.class));
server.get().stop();
waitUntilServerIsAvailableOnPort(port);
}

@Test
public void canGetDebugLogFromStandalone() throws Exception {
Integer port = PortProber.findFreePort();
Path tempLog = Files.createTempFile("test", ".log");
String[] args = {"-debug", "-log", tempLog.toString(), "-port", port.toString()};
Optional<Stoppable> server = new GridLauncherV3(args).launch();

server = new GridLauncherV3(args).launch();
assertTrue(server.isPresent());

WebDriver driver = new RemoteWebDriver(new URL(String.format("http://localhost:%d/wd/hub", port)),
DesiredCapabilities.htmlUnit());
driver.quit();
assertThat(readAll(tempLog), containsString("DEBUG [WebDriverServlet.handle]"));
server.get().stop();
}

@Test(timeout = 20000L)
public void canSetSessionTimeoutForStandalone() throws Exception {
Integer port = PortProber.findFreePort();
Path tempLog = Files.createTempFile("test", ".log");
String[] args = {"-log", tempLog.toString(), "-port", port.toString(), "-timeout", "5"};
Optional<Stoppable> server = new GridLauncherV3(args).launch();

server = new GridLauncherV3(args).launch();
assertTrue(server.isPresent());

WebDriver driver = new RemoteWebDriver(new URL(String.format("http://localhost:%d/wd/hub", port)),
DesiredCapabilities.htmlUnit());
long start = System.currentTimeMillis();
Expand All @@ -179,7 +203,6 @@ public void canSetSessionTimeoutForStandalone() throws Exception {
long end = System.currentTimeMillis();
assertThat(end - start, greaterThan(5000L));
assertThat(end - start, lessThan(15000L));
server.get().stop();
}

private String readAll(Path file) {
Expand All @@ -194,6 +217,7 @@ private String readAll(Path file) {
public void cannotStartHtmlSuite() {
ByteArrayOutputStream outSpy = new ByteArrayOutputStream();
String[] args = {"-htmlSuite", "*quantum", "http://base.url", "suite.html", "report.html"};

new GridLauncherV3(new PrintStream(outSpy), args).launch();
assertThat(outSpy.toString(), containsString("Download the Selenium HTML Runner"));
}
Expand All @@ -202,24 +226,18 @@ public void cannotStartHtmlSuite() {
public void testRegisterNodeToHub() throws Exception {
Integer hubPort = PortProber.findFreePort();
String[] hubArgs = {"-role", "hub", "-port", hubPort.toString()};
Optional<Stoppable> hub = new GridLauncherV3(hubArgs).launch();
UrlChecker urlChecker = new UrlChecker();
urlChecker.waitUntilAvailable(10, TimeUnit.SECONDS, new URL(
String.format("http://localhost:%d/grid/console", hubPort)));

Integer nodePort = PortProber.findFreePort();
server = new GridLauncherV3(hubArgs).launch();
waitUntilServerIsAvailableOnPort(hubPort);

Integer nodePort = PortProber.findFreePort();
String[] nodeArgs = {"-role", "node", "-hub", "http://localhost:" + hubPort,
"-browser", "browserName=htmlunit,maxInstances=1", "-port", nodePort.toString()};
Optional<Stoppable> node = new GridLauncherV3(nodeArgs).launch();
urlChecker.waitUntilAvailable(10, TimeUnit.SECONDS, new URL(
String.format("http://localhost:%d/wd/hub/status", nodePort)));
node = new GridLauncherV3(nodeArgs).launch();
waitUntilServerIsAvailableOnPort(nodePort);

waitForTextOnHubConsole(hubPort, "htmlunit");
checkPresenceOfElementOnHubConsole(hubPort, By.cssSelector("img[src$='htmlunit.png']"));

node.ifPresent(Stoppable::stop);
hub.ifPresent(Stoppable::stop);
}

@Test
Expand All @@ -243,39 +261,31 @@ public void canStartHubUsingConfigFile() throws Exception {
+ "}", hubPort);
Files.write(hubConfig, hubJson.getBytes());
String[] hubArgs = {"-role", "hub", "-hubConfig", hubConfig.toString()};
Optional<Stoppable> hub = new GridLauncherV3(hubArgs).launch();
UrlChecker urlChecker = new UrlChecker();
urlChecker.waitUntilAvailable(10, TimeUnit.SECONDS, new URL(
String.format("http://localhost:%d/grid/console", hubPort)));
server = new GridLauncherV3(hubArgs).launch();
waitUntilServerIsAvailableOnPort(hubPort);

assertThat(hub.get(), instanceOf(Hub.class));
GridHubConfiguration realHubConfig = ((Hub) hub.get()).getConfiguration();
assertThat(server.get(), instanceOf(Hub.class));
GridHubConfiguration realHubConfig = ((Hub) server.get()).getConfiguration();
assertEquals(10000, realHubConfig.cleanUpCycle.intValue());
assertEquals(30000, realHubConfig.browserTimeout.intValue());
assertEquals(3600, realHubConfig.timeout.intValue());

Integer nodePort = PortProber.findFreePort();
String[] nodeArgs = {"-role", "node", "-hub", "http://localhost:" + hubPort,
"-browser", "browserName=htmlunit,maxInstances=1", "-port", nodePort.toString()};
Optional<Stoppable> node = new GridLauncherV3(nodeArgs).launch();
urlChecker.waitUntilAvailable(10, TimeUnit.SECONDS, new URL(
String.format("http://localhost:%d/wd/hub/status", nodePort)));
node = new GridLauncherV3(nodeArgs).launch();
waitUntilServerIsAvailableOnPort(nodePort);

waitForTextOnHubConsole(hubPort, "htmlunit");
checkPresenceOfElementOnHubConsole(hubPort, By.cssSelector("img[src$='htmlunit.png']"));

node.ifPresent(Stoppable::stop);
hub.ifPresent(Stoppable::stop);
}

@Test
public void canStartNodeUsingConfigFile() throws Exception {
Integer hubPort = PortProber.findFreePort();
String[] hubArgs = {"-role", "hub", "-port", hubPort.toString()};
Optional<Stoppable> hub = new GridLauncherV3(hubArgs).launch();
UrlChecker urlChecker = new UrlChecker();
urlChecker.waitUntilAvailable(10, TimeUnit.SECONDS, new URL(
String.format("http://localhost:%d/grid/console", hubPort)));
server = new GridLauncherV3(hubArgs).launch();
waitUntilServerIsAvailableOnPort(hubPort);

Integer nodePort = PortProber.findFreePort();
Path nodeConfig = Files.createTempFile("node", ".json");
Expand All @@ -299,17 +309,12 @@ public void canStartNodeUsingConfigFile() throws Exception {
+ " \"custom\": {}\n"
+ "}", nodePort, hubPort);
Files.write(nodeConfig, nodeJson.getBytes());

String[] nodeArgs = {"-role", "node", "-nodeConfig", nodeConfig.toString() };
Optional<Stoppable> node = new GridLauncherV3(nodeArgs).launch();
urlChecker.waitUntilAvailable(10, TimeUnit.SECONDS, new URL(
String.format("http://localhost:%d/wd/hub/status", nodePort)));
node = new GridLauncherV3(nodeArgs).launch();
waitUntilServerIsAvailableOnPort(nodePort);

waitForTextOnHubConsole(hubPort, "htmlunit");
checkPresenceOfElementOnHubConsole(hubPort, By.cssSelector("img[src$='htmlunit.png']"));

node.ifPresent(Stoppable::stop);
hub.ifPresent(Stoppable::stop);
}

private void waitForTextOnHubConsole(Integer hubPort, String text) throws MalformedURLException {
Expand All @@ -326,6 +331,22 @@ private void waitForTextOnHubConsole(Integer hubPort, String text) throws Malfor
});
}

private void waitUntilServerIsAvailableOnPort(int port) throws Exception {
waitUntilAvailable(String.format("http://localhost:%d/wd/hub/status", port));
}

private void waitUntilAvailable(String url) throws Exception {
new UrlChecker().waitUntilAvailable(10, TimeUnit.SECONDS, new URL(url));
}

private String getContentOf(int port, String path) throws Exception {
String baseUrl = String.format("http://localhost:%d", port);
HttpClient client = HttpClient.Factory.createDefault().createClient(new URL(baseUrl));
HttpRequest req = new HttpRequest(HttpMethod.GET, path);
return client.execute(req).getContentString();

}

private void checkPresenceOfElementOnHubConsole(Integer hubPort, By locator)
throws MalformedURLException {
WebDriver driver = new RemoteWebDriver(
Expand Down

0 comments on commit 02ebcc1

Please sign in to comment.