Skip to content

Commit

Permalink
Get our closure-based javascript tests running with bazel.
Browse files Browse the repository at this point in the history
  • Loading branch information
jleyba committed Jul 12, 2019
1 parent 6674a23 commit fb4030e
Show file tree
Hide file tree
Showing 13 changed files with 235 additions and 14 deletions.
8 changes: 4 additions & 4 deletions java/client/test/org/openqa/selenium/build/InProject.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,10 @@ public static Path locate(String... paths) {
});
}

private static Path findProjectRoot() {
public static Path findProjectRoot() {
Path dir = findRunfilesRoot();
if (dir != null) {
return dir;
return dir.resolve("selenium").normalize();
}

// Find the rakefile first
Expand All @@ -79,12 +79,12 @@ private static Path findProjectRoot() {
return dir.normalize();
}

private static Path findRunfilesRoot() {
public static Path findRunfilesRoot() {
String srcdir = System.getenv("TEST_SRCDIR");
if (srcdir == null || srcdir.isEmpty()) {
return null;
}
Path dir = Paths.get(srcdir).toAbsolutePath().resolve("selenium").normalize();
Path dir = Paths.get(srcdir).toAbsolutePath().normalize();
if (Files.exists(dir) && Files.isDirectory(dir)) {
return dir;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ public class JettyAppServer implements AppServer {
private static final int DEFAULT_HTTP_PORT = 2310;
private static final int DEFAULT_HTTPS_PORT = 2410;
private static final String DEFAULT_CONTEXT_PATH = "/common";
private static final String FILEZ_CONTEXT_PATH = "/filez";
private static final String JS_SRC_CONTEXT_PATH = "/javascript";
private static final String TEMP_SRC_CONTEXT_PATH = "/temp";
private static final String CLOSURE_CONTEXT_PATH = "/third_party/closure/goog";
Expand Down Expand Up @@ -117,6 +118,12 @@ public JettyAppServer(String hostName, int httpPort, int httpsPort) {
ServletContextHandler defaultContext = addResourceHandler(
DEFAULT_CONTEXT_PATH, webSrc);

// Only non-null when running with bazel test.
Path runfiles = InProject.findRunfilesRoot();
if (runfiles != null) {
addResourceHandler(FILEZ_CONTEXT_PATH, runfiles);
}

addJsResourceHandler(JS_SRC_CONTEXT_PATH, "javascript");
addJsResourceHandler(CLOSURE_CONTEXT_PATH, "third_party/closure/goog");
addJsResourceHandler(THIRD_PARTY_JS_CONTEXT_PATH, "third_party/js");
Expand Down
15 changes: 15 additions & 0 deletions java/client/test/org/openqa/selenium/javascript/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
java_library(
name = "javascript",
srcs = glob(["*.java"]),
testonly = 1,
visibility = ["//javascript:__subpackages__"],
deps = [
"//java/client/src/org/openqa/selenium:core",
"//java/client/test/org/openqa/selenium/build",
"//java/client/test/org/openqa/selenium/environment",
"//java/client/test/org/openqa/selenium/testing:test-base",
"//java/client/test/org/openqa/selenium/testing/drivers",
"//third_party/java/guava",
"//third_party/java/junit",
],
)
Original file line number Diff line number Diff line change
Expand Up @@ -63,14 +63,22 @@ public JavaScriptTestSuite(Class<?> testClass) throws InitializationError, IOExc

children = createChildren(driverSupplier, timeout);
}

private static boolean isBazel() {
return InProject.findRunfilesRoot() != null;
}

private static ImmutableList<Runner> createChildren(
final Supplier<WebDriver> driverSupplier, final long timeout) throws IOException {
final Path baseDir = InProject.locate("Rakefile").getParent();
final Path baseDir = InProject.findProjectRoot();
final Function<String, URL> pathToUrlFn = s -> {
AppServer appServer = GlobalTestEnvironment.get().getAppServer();
try {
return new URL(appServer.whereIs("/" + s));
String url = "/" + s;
if (isBazel() && !url.startsWith("/common/generated/")) {
url = "/filez/selenium" + url;
}
return new URL(appServer.whereIs(url));
} catch (MalformedURLException e) {
throw new RuntimeException(e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,11 @@ private static List<Path> findTestFiles(Path directory, ImmutableSet<Path> exclu
Integer.MAX_VALUE,
(path, basicFileAttributes) -> {
String name = path.getFileName().toString();
Path sibling = path.resolveSibling(name.replace(".js", ".html"));
return name.endsWith("_test.html")
|| (name.endsWith("_test.js") && !Files.exists(sibling));
return name.endsWith("_test.html");
// TODO: revive support for _test.js files.
// Path sibling = path.resolveSibling(name.replace(".js", ".html"));
// return name.endsWith("_test.html")
// || (name.endsWith("_test.js") && !Files.exists(sibling));
})
.filter(path -> !excludedFiles.contains(path))
.collect(Collectors.toList());
Expand All @@ -68,8 +70,17 @@ private static Path getTestDirectory() {
String testDirName = checkNotNull(getProperty(TEST_DIRECTORY_PROPERTY),
"You must specify the test directory with the %s system property",
TEST_DIRECTORY_PROPERTY);

Path runfiles = InProject.findRunfilesRoot();
Path testDir;
if (runfiles != null) {
// Running with bazel.
testDir = runfiles.resolve("selenium").resolve(testDirName);
} else {
// Legacy.
testDir = InProject.locate(testDirName);
}

Path testDir = InProject.locate(testDirName);
checkArgument(Files.exists(testDir), "Test directory does not exist: %s",
testDirName);
checkArgument(Files.isDirectory(testDir));
Expand Down
48 changes: 47 additions & 1 deletion javascript/atoms/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
load("@io_bazel_rules_closure//closure:defs.bzl", "closure_js_library")
load("@io_bazel_rules_closure//closure:defs.bzl", "closure_js_deps", "closure_js_library")
load("//javascript:rules.bzl", "closure_test_suite")

package(default_visibility = ["//visibility:public"])

Expand Down Expand Up @@ -241,3 +242,48 @@ closure_js_library(
"@io_bazel_rules_closure//closure/library",
],
)

closure_js_library(
name = "test_util",
srcs = ["test/test_util.js"],
testonly = 1,
deps = [
":useragent",
"@io_bazel_rules_closure//closure/library",
],
)

closure_js_deps(
name = "deps",
testonly = 1,
visibility = [
"//javascript:__pkg__",
"//javascript/webdriver:__pkg__",
],
deps = [
":action",
":bot",
":color",
":devices",
":domcore",
":dom",
":errors",
":events",
":html5",
":inject",
":json",
":locators",
":useragent",
":window",
":xpath",
":test_util",
],
)

closure_test_suite(
name = "test",
data = [
":atoms",
":deps",
],
)
8 changes: 8 additions & 0 deletions javascript/atoms/test/test_bootstrap.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,14 @@
'../../deps.js'
];

if (location.pathname.lastIndexOf('/filez/selenium/javascript/', 0) === 0) {
directoryPath = '';
files = [
'/filez/com_google_javascript_closure_library/closure/goog/base.js',
'/filez/selenium/javascript/atoms/deps.js',
];
}

for (var j = 0; j < files.length; j++) {
document.write('<script type="text/javascript" src="' +
directoryPath + files[j] + '"></script>');
Expand Down
40 changes: 39 additions & 1 deletion javascript/rules.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,42 @@ def closure_fragment(name, **kwargs):
args = dict(**kwargs)
args["defines"] = defs
fragment_name = name + "-firefox"
native.closure_fragment(name = fragment_name, **args)
native.closure_fragment(name = fragment_name, **args)


def closure_test_suite(name, data):
browsers = {
"firefox": ("ff", "//java/client/src/org/openqa/selenium/firefox",),
"chrome": ("chrome", "//java/client/src/org/openqa/selenium/chrome",),
"ie": ("ie", "//java/client/src/org/openqa/selenium/ie",),
"safari": ("safari", "//java/client/src/org/openqa/selenium/safari",),
}

tests = []
for browser in browsers.keys():
spec = browsers[browser]
test_name = "%s-%s" % (name, browser)
native.java_test(
name = test_name,
test_class = "org.openqa.selenium.javascript.ClosureTestSuite",
jvm_flags = [
"-Dselenium.browser=%s" % spec[0],
"-Djs.test.timeout=20",
"-Djs.test.dir=%s" % native.package_name(),
],
data = data + [
"@com_google_javascript_closure_library//:com_google_javascript_closure_library",
],
runtime_deps = [
"//java/client/test/org/openqa/selenium/javascript:javascript",
spec[1],
],
tags = ["no-sandbox"],
)
tests.append(":" + test_name)

native.test_suite(
name = name,
tests = tests,
tags = ["manual", "no-sandbox"],
)
25 changes: 24 additions & 1 deletion javascript/selenium-atoms/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
load("@io_bazel_rules_closure//closure:defs.bzl", "closure_js_binary", "closure_js_library")
load("@io_bazel_rules_closure//closure:defs.bzl", "closure_js_binary", "closure_js_deps", "closure_js_library")
load("//javascript:fragment.bzl", "closure_fragment")
load("//javascript:rules.bzl", "closure_test_suite")

package_group(
name = "webdriver_backed_selenium_implementations",
Expand Down Expand Up @@ -150,3 +151,25 @@ closure_fragment(
visibility = [":webdriver_backed_selenium_implementations"],
deps = [":deps"],
)

filegroup(
name = "files",
srcs = glob([
"**/*.html",
"**/*.js",
]),
)

closure_js_deps(
name = "jsdeps",
testonly = 1,
deps = [":deps"],
)

closure_test_suite(
name = "test",
data = [
":files",
":jsdeps",
],
)
8 changes: 8 additions & 0 deletions javascript/selenium-atoms/test/test_bootstrap.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,14 @@
'../../deps.js'
];

if (location.pathname.lastIndexOf('/filez/selenium/javascript/', 0) === 0) {
directoryPath = '';
files = [
'/filez/com_google_javascript_closure_library/closure/goog/base.js',
'/filez/selenium/javascript/selenium-atoms/jsdeps.js',
];
}

for (var j = 0; j < files.length; j++) {
document.write('<script type="text/javascript" src="' +
directoryPath + files[j] + '"></script>');
Expand Down
35 changes: 34 additions & 1 deletion javascript/webdriver/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
load("@io_bazel_rules_closure//closure:defs.bzl", "closure_js_library")
load("@io_bazel_rules_closure//closure:defs.bzl", "closure_js_deps", "closure_js_library")
load("//javascript:rules.bzl", "closure_test_suite")

closure_js_library(
name = "http",
Expand Down Expand Up @@ -27,3 +28,35 @@ closure_js_library(
srcs = ["key.js"],
visibility = ["//javascript:__subpackages__"],
)

closure_js_library(
name = "all_js_for_testing",
testonly = 1,
srcs = glob(["**/*.js"]),
visibility = ["//javascript:__pkg__"],
deps = ["@io_bazel_rules_closure//closure/library"],
)

filegroup(
name = "all_files",
testonly = 1,
srcs = glob(["**/*"], exclude = ["BUCK", "build.desc"]),
)

closure_js_deps(
name = "deps",
testonly = 1,
deps = [
":all_js_for_testing",
"//javascript/webdriver/atoms:all_js_for_testing",
],
)

closure_test_suite(
name = "test",
data = [
":all_files",
":deps",
"//javascript/atoms:deps",
],
)
13 changes: 13 additions & 0 deletions javascript/webdriver/atoms/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,16 @@ closure_fragment(
":atoms-lib",
],
)

closure_js_library(
name = "all_js_for_testing",
testonly = 1,
srcs = glob(["**/*.js"]),
visibility = [
"//javascript:__pkg__",
"//javascript/webdriver:__pkg__",
],
deps = [
"@io_bazel_rules_closure//closure/library",
],
)
11 changes: 11 additions & 0 deletions javascript/webdriver/test/test_bootstrap.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,17 @@
'../../deps.js'
];


if (location.pathname.lastIndexOf('/filez/selenium/javascript/', 0) === 0
|| location.pathname.lastIndexOf('/common/generated/javascript/', 0) === 0) {
directoryPath = '';
files = [
'/filez/com_google_javascript_closure_library/closure/goog/base.js',
'/filez/selenium/javascript/atoms/deps.js',
'/filez/selenium/javascript/webdriver/deps.js',
];
}

for (var j = 0; j < files.length; j++) {
document.write('<script type="text/javascript" src="' +
directoryPath + files[j] + '"></script>');
Expand Down

0 comments on commit fb4030e

Please sign in to comment.