Skip to content

Commit

Permalink
[grid + cdp]: add a smoke test for grid cdp propagation
Browse files Browse the repository at this point in the history
This is being used to guide development, and so isn't complete yet.
  • Loading branch information
shs96c committed May 19, 2020
1 parent aae7f24 commit c62241c
Show file tree
Hide file tree
Showing 6 changed files with 195 additions and 3 deletions.
1 change: 1 addition & 0 deletions java/client/test/org/openqa/selenium/testing/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ java_library(
],
visibility = [
"//java/client/test:__subpackages__",
"//java/server/test:__subpackages__",
],
deps = [
"//java/client/src/org/openqa/selenium:core",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ java_library(
resources = glob(["*.txt"]),
visibility = [
"//java/server/src/org/openqa/selenium/grid:__pkg__",
"//java/server/test/org/openqa/selenium/grid:__pkg__",
],
runtime_deps = [
"//java/server/src/org/openqa/selenium/events/local",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ java_library(
srcs = glob(["*.java"]),
visibility = [
"//java/server/src/org/openqa/selenium/grid:__pkg__",
"//java/server/test/org/openqa/selenium/grid:__pkg__",
],
deps = [
"//java:auto-service",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ java_library(
srcs = glob(["*.java"]),
visibility = [
"//java/server/src/org/openqa/selenium/grid:__pkg__",
"//java/server/test/org/openqa/selenium/grid:__pkg__",
],
deps = [
"//java:auto-service",
Expand Down
30 changes: 27 additions & 3 deletions java/server/test/org/openqa/selenium/grid/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,10 +1,34 @@
load("@rules_jvm_external//:defs.bzl", "artifact")
load("//java:defs.bzl", "java_test_suite")
load("//java:defs.bzl", "java_selenium_test_suite", "java_test_suite")

LARGE_TESTS = [
"SmokeTest.java",
]

java_selenium_test_suite(
name = "large-tests",
size = "large",
srcs = LARGE_TESTS,
deps = [
"//java/client/src/org/openqa/selenium:core",
"//java/client/src/org/openqa/selenium/json",
"//java/client/src/org/openqa/selenium/remote/http",
"//java/client/src/org/openqa/selenium/support",
"//java/client/test/org/openqa/selenium/testing:annotations",
"//java/server/src/org/openqa/selenium/grid",
"//java/server/src/org/openqa/selenium/grid/commands",
"//java/server/src/org/openqa/selenium/grid/distributor/httpd",
"//java/server/src/org/openqa/selenium/grid/sessionmap/httpd",
artifact("com.google.guava:guava"),
artifact("junit:junit"),
artifact("org.assertj:assertj-core"),
],
)

java_test_suite(
name = "MediumTests",
name = "medium-tests",
size = "medium",
srcs = glob(["*Test.java"]),
srcs = glob(["*Test.java"], exclude = LARGE_TESTS),
deps = [
"//java/server/src/org/openqa/selenium/grid",
artifact("com.beust:jcommander"),
Expand Down
164 changes: 164 additions & 0 deletions java/server/test/org/openqa/selenium/grid/SmokeTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
// Licensed to the Software Freedom Conservancy (SFC) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The SFC licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.

package org.openqa.selenium.grid;

import com.google.common.collect.ImmutableMap;
import org.junit.Ignore;
import org.junit.Test;
import org.openqa.selenium.grid.commands.MessageBusCommand;
import org.openqa.selenium.grid.config.MapConfig;
import org.openqa.selenium.grid.distributor.httpd.DistributorServer;
import org.openqa.selenium.grid.node.httpd.NodeServer;
import org.openqa.selenium.grid.router.httpd.RouterServer;
import org.openqa.selenium.grid.server.BaseServerOptions;
import org.openqa.selenium.grid.server.Server;
import org.openqa.selenium.grid.sessionmap.httpd.SessionMapServer;
import org.openqa.selenium.grid.web.Values;
import org.openqa.selenium.net.PortProber;
import org.openqa.selenium.netty.server.NettyServer;
import org.openqa.selenium.remote.http.Contents;
import org.openqa.selenium.remote.http.HttpClient;
import org.openqa.selenium.remote.http.HttpRequest;
import org.openqa.selenium.remote.http.HttpResponse;
import org.openqa.selenium.support.ui.FluentWait;
import org.openqa.selenium.testing.drivers.Browser;

import java.io.UncheckedIOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Map;
import java.util.Objects;

import static java.time.Duration.ofSeconds;
import static org.assertj.core.api.Assertions.fail;
import static org.assertj.core.api.Assumptions.assumeThat;
import static org.openqa.selenium.json.Json.MAP_TYPE;
import static org.openqa.selenium.remote.http.HttpMethod.GET;

@Ignore
public class SmokeTest {

@Test
public void ensureBasicFunctionality() throws MalformedURLException {
Browser browser = Objects.requireNonNull(Browser.detect());

assumeThat(browser.supportsCdp()).isTrue();

int messagePublishPort = PortProber.findFreePort();
int messageSubscribePort = PortProber.findFreePort();
String[] eventBusFlags = new String[]{"--publish-events", "tcp://*:" + messagePublishPort, "--subscribe-events", "tcp://*:" + messageSubscribePort};

int messageBusPort = PortProber.findFreePort();
new MessageBusCommand().configure(
System.out,
System.err,
mergeArgs(eventBusFlags, "--port", "" + messageBusPort)).run();
waitUntilUp(messageBusPort);

int sessionsPort = PortProber.findFreePort();
new SessionMapServer().configure(
System.out,
System.err,
mergeArgs(eventBusFlags, "--bind-bus", "false", "--port", "" + sessionsPort)).run();
waitUntilUp(sessionsPort);

int distributorPort = PortProber.findFreePort();
new DistributorServer().configure(
System.out,
System.err,
mergeArgs(eventBusFlags, "--bind-bus", "false", "--port", "" + distributorPort, "-s", "http://localhost:" + sessionsPort)).run();
waitUntilUp(distributorPort);

int routerPort = PortProber.findFreePort();
new RouterServer().configure(
System.out,
System.err,
"--port", "" + routerPort, "-s", "http://localhost:" + sessionsPort, "-d", "http://localhost:" + distributorPort).run();
waitUntilUp(routerPort);

int nodePort = PortProber.findFreePort();
new NodeServer().configure(
System.out,
System.err,
mergeArgs(eventBusFlags, "--port", "" + nodePort, "-I", getBrowserShortName(), "--public-url", "http://localhost:" + routerPort)).run();
waitUntilUp(nodePort);

HttpClient client = HttpClient.Factory.createDefault().createClient(new URL("http://localhost:" + routerPort));
new FluentWait<>(client).withTimeout(ofSeconds(10)).until(c -> {
HttpResponse res = c.execute(new HttpRequest(GET, "/status"));
if (!res.isSuccessful()) {
return false;
}
Map<String, Object> value = Values.get(res, MAP_TYPE);
if (value == null) {
return false;
}
return Boolean.TRUE.equals(value.get("ready"));
});

Server<?> server = new NettyServer(
new BaseServerOptions(new MapConfig(ImmutableMap.of())),
req -> new HttpResponse().setContent(Contents.utf8String("I like cheese")))
.start();

fail("Oh noes!");
}

private String[] mergeArgs(String[] baseFlags, String... allTheArgs) {
int length = baseFlags.length + allTheArgs.length;

String[] args = new String[length];

System.arraycopy(baseFlags, 0, args, 0, baseFlags.length);
System.arraycopy(allTheArgs, 0, args, baseFlags.length, allTheArgs.length);

return args;
}

private void waitUntilUp(int port) {
try {
HttpClient.Factory clientFactory = HttpClient.Factory.createDefault();
HttpClient client = clientFactory.createClient(new URL("http://localhost:" + port));

new FluentWait<>(client)
.ignoring(UncheckedIOException.class)
.withTimeout(ofSeconds(15))
.until(http -> http.execute(new HttpRequest(GET, "/status")).isSuccessful());
} catch (MalformedURLException e) {
throw new RuntimeException(e);
}
}

private String getBrowserShortName() {
switch (System.getProperty("selenium.browser")) {
case "chrome":
case "edge":
case "ie":
return System.getProperty("selenium.browser");

case "ff":
return "firefox";

case "safari":
return "Safari Technology Preview";

default:
throw new RuntimeException("Unknown browser: " + System.getProperty("selenium.browser"));
}
}
}

0 comments on commit c62241c

Please sign in to comment.