Skip to content

Commit

Permalink
[grid] Using HTTP client instead of HtmlUnit to verify grid console text
Browse files Browse the repository at this point in the history
  • Loading branch information
barancev committed Apr 8, 2019
1 parent 88fb475 commit fad45d9
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ static void addShutdownAction(Runnable action) {
return options;
})
.put(Browser.EDGE, EdgeOptions::new)
.put(Browser.HTMLUNIT, DesiredCapabilities::htmlUnit)
.put(Browser.HTMLUNIT, () -> new DesiredCapabilities(BrowserType.HTMLUNIT, "", Platform.ANY))
.put(Browser.OPERABLINK, OperaOptions::new)
.put(Browser.SAFARI, () -> {
SafariOptions options = new SafariOptions();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,15 @@

import org.junit.After;
import org.junit.Test;
import org.openqa.grid.e2e.utils.GridTestHelper;
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;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.openqa.selenium.remote.http.HttpClient;
import org.openqa.selenium.remote.http.HttpMethod;
Expand Down Expand Up @@ -116,9 +115,9 @@ public void canPrintNodeHelp() {

@Test
public void canRedirectLogToFile() throws Exception {
Integer port = PortProber.findFreePort();
int port = PortProber.findFreePort();
Path tempLog = Files.createTempFile("test", ".log");
String[] args = {"-log", tempLog.toString(), "-port", port.toString()};
String[] args = {"-log", tempLog.toString(), "-port", Integer.toString(port)};

server = new GridLauncherV3().launch(args);
assertNotNull(server);
Expand All @@ -130,9 +129,9 @@ public void canRedirectLogToFile() throws Exception {

@Test
public void canLaunchStandalone() throws Exception {
Integer port = PortProber.findFreePort();
int port = PortProber.findFreePort();
ByteArrayOutputStream outSpy = new ByteArrayOutputStream();
String[] args = {"-role", "standalone", "-port", port.toString()};
String[] args = {"-role", "standalone", "-port", Integer.toString(port)};

server = new GridLauncherV3(new PrintStream(outSpy)).launch(args);
assertNotNull(server);
Expand All @@ -149,9 +148,9 @@ public void canLaunchStandalone() throws Exception {

@Test
public void launchesStandaloneByDefault() throws Exception {
Integer port = PortProber.findFreePort();
int port = PortProber.findFreePort();
ByteArrayOutputStream outSpy = new ByteArrayOutputStream();
String[] args = {"-port", port.toString()};
String[] args = {"-port", Integer.toString(port)};

server = new GridLauncherV3(new PrintStream(outSpy)).launch(args);
assertNotNull(server);
Expand All @@ -161,30 +160,30 @@ public void launchesStandaloneByDefault() throws Exception {

@Test
public void canGetDebugLogFromStandalone() throws Exception {
Integer port = PortProber.findFreePort();
int port = PortProber.findFreePort();
Path tempLog = Files.createTempFile("test", ".log");
String[] args = {"-debug", "-log", tempLog.toString(), "-port", port.toString()};
String[] args = {"-debug", "-log", tempLog.toString(), "-port", Integer.toString(port)};

server = new GridLauncherV3().launch(args);
assertNotNull(server);

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

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

server = new GridLauncherV3().launch(args);
assertNotNull(server);

WebDriver driver = new RemoteWebDriver(new URL(String.format("http://localhost:%d/wd/hub", port)),
DesiredCapabilities.htmlUnit());
GridTestHelper.getDefaultBrowserCapability());
long start = System.currentTimeMillis();
new FluentWait<>(tempLog).withTimeout(Duration.ofSeconds(100))
.until(file -> readAll(file).contains("Removing session"));
Expand All @@ -211,20 +210,20 @@ public void cannotStartHtmlSuite() {

@Test
public void testRegisterNodeToHub() throws Exception {
Integer hubPort = PortProber.findFreePort();
String[] hubArgs = {"-role", "hub", "-host", "localhost", "-port", hubPort.toString()};
int hubPort = PortProber.findFreePort();
String[] hubArgs = {"-role", "hub", "-host", "localhost", "-port", Integer.toString(hubPort)};

server = new GridLauncherV3().launch(hubArgs);
waitUntilServerIsAvailableOnPort(hubPort);

Integer nodePort = PortProber.findFreePort();
int nodePort = PortProber.findFreePort();
String[] nodeArgs = {"-role", "node", "-host", "localhost", "-hub", "http://localhost:" + hubPort,
"-browser", "browserName=htmlunit,maxInstances=1", "-port", nodePort.toString()};
"-browser", "browserName=htmlunit,maxInstances=1", "-port", Integer.toString(nodePort)};
node = new GridLauncherV3().launch(nodeArgs);
waitUntilServerIsAvailableOnPort(nodePort);

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

/*
Expand All @@ -234,8 +233,8 @@ public void testRegisterNodeToHub() throws Exception {
*/
@Test
public void testThrowOnCapabilityNotPresentFlagIsUsed() {
Integer hubPort = PortProber.findFreePort();
String[] hubArgs = {"-role", "hub", "-host", "localhost", "-port", hubPort.toString(),
int hubPort = PortProber.findFreePort();
String[] hubArgs = {"-role", "hub", "-host", "localhost", "-port", Integer.toString(hubPort),
"-throwOnCapabilityNotPresent", "true"};

server = new GridLauncherV3().launch(hubArgs);
Expand All @@ -249,7 +248,7 @@ public void testThrowOnCapabilityNotPresentFlagIsUsed() {

// Stopping the hub and starting it with a new throwOnCapabilityNotPresent value
hub.stop();
hubArgs = new String[]{"-role", "hub", "-host", "localhost", "-port", hubPort.toString(),
hubArgs = new String[]{"-role", "hub", "-host", "localhost", "-port", Integer.toString(hubPort),
"-throwOnCapabilityNotPresent", "false"};
server = new GridLauncherV3().launch(hubArgs);
hub = (Hub) server;
Expand All @@ -263,7 +262,7 @@ public void testThrowOnCapabilityNotPresentFlagIsUsed() {

@Test
public void canStartHubUsingConfigFile() throws Exception {
Integer hubPort = PortProber.findFreePort();
int hubPort = PortProber.findFreePort();
Path hubConfig = Files.createTempFile("hub", ".json");
String hubJson = String.format(
"{ \"port\": %s,\n"
Expand Down Expand Up @@ -292,24 +291,24 @@ public void canStartHubUsingConfigFile() throws Exception {
assertEquals(30000, realHubConfig.browserTimeout.intValue());
assertEquals(3600, realHubConfig.timeout.intValue());

Integer nodePort = PortProber.findFreePort();
int nodePort = PortProber.findFreePort();
String[] nodeArgs = {"-role", "node", "-host", "localhost", "-hub", "http://localhost:" + hubPort,
"-browser", "browserName=htmlunit,maxInstances=1", "-port", nodePort.toString()};
"-browser", "browserName=htmlunit,maxInstances=1", "-port", Integer.toString(nodePort)};
node = new GridLauncherV3().launch(nodeArgs);
waitUntilServerIsAvailableOnPort(nodePort);

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

@Test
public void canStartNodeUsingConfigFile() throws Exception {
Integer hubPort = PortProber.findFreePort();
String[] hubArgs = {"-role", "hub", "-port", hubPort.toString()};
int hubPort = PortProber.findFreePort();
String[] hubArgs = {"-role", "hub", "-port", Integer.toString(hubPort)};
server = new GridLauncherV3().launch(hubArgs);
waitUntilServerIsAvailableOnPort(hubPort);

Integer nodePort = PortProber.findFreePort();
int nodePort = PortProber.findFreePort();
Path nodeConfig = Files.createTempFile("node", ".json");
String nodeJson = String.format(
"{\n"
Expand All @@ -336,7 +335,7 @@ public void canStartNodeUsingConfigFile() throws Exception {
waitUntilServerIsAvailableOnPort(nodePort);

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

private void waitForTextOnHubConsole(Integer hubPort, String text) throws MalformedURLException {
Expand Down Expand Up @@ -369,20 +368,23 @@ private String getContentOf(int port, String path) throws Exception {

}

private void checkPresenceOfElementOnHubConsole(Integer hubPort, By locator)
throws MalformedURLException {
WebDriver driver = new RemoteWebDriver(
new URL(String.format("http://localhost:%d/wd/hub", hubPort)),
DesiredCapabilities.htmlUnit());
private int countTextFragmentsOnConsole(int hubPort, String target) throws Exception {
String gridConsole = getContentOf(hubPort, "/grid/console");
return countSubstring(gridConsole, target);
}

try {
driver.get(String.format("http://localhost:%d/grid/console", hubPort));
assertEquals("Should only have one htmlunit registered to the hub",
1, driver.findElements(locator).size());
} finally {
try {
driver.quit();
} catch (Exception ignore) {}
private int countSubstring(String s, String target) {
int lastIndex = 0;
int count = 0;

while(lastIndex != -1){
lastIndex = s.indexOf(target, lastIndex);
if(lastIndex != -1){
count++;
lastIndex += target.length();
}
}

return count;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@
import org.openqa.grid.internal.utils.configuration.GridNodeConfiguration;
import org.openqa.grid.web.Hub;
import org.openqa.selenium.Capabilities;
import org.openqa.selenium.Platform;
import org.openqa.selenium.net.PortProber;
import org.openqa.selenium.remote.BrowserType;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.openqa.selenium.remote.server.SeleniumServer;
Expand Down Expand Up @@ -74,7 +76,7 @@ public static DesiredCapabilities getDefaultBrowserCapability() {
caps.setBrowserName(browser);
return caps;
}
return DesiredCapabilities.htmlUnit();
return new DesiredCapabilities(BrowserType.HTMLUNIT, "", Platform.ANY);
}

public static Hub getHub() {
Expand Down

0 comments on commit fad45d9

Please sign in to comment.