Skip to content

Commit

Permalink
[java] Using bazel instead of buck to build static resources and sele…
Browse files Browse the repository at this point in the history
…nium server for tests
  • Loading branch information
barancev committed Sep 15, 2019
1 parent d2a222c commit 2602ab2
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 31 deletions.
52 changes: 52 additions & 0 deletions java/client/test/org/openqa/selenium/build/BazelBuild.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// 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.build;

import com.google.common.collect.ImmutableList;

import org.openqa.selenium.WebDriverException;
import org.openqa.selenium.os.CommandLine;

import java.nio.file.Path;
import java.util.logging.Logger;

public class BazelBuild {
private static Logger log = Logger.getLogger(BazelBuild.class.getName());

public void build(String target) {
Path projectRoot = InProject.locate("Rakefile").getParent();

if (target == null || "".equals(target)) {
throw new IllegalStateException("No targets specified");
}
log.info("\nBuilding " + target + " ...");

ImmutableList.Builder<String> builder = ImmutableList.builder();
builder.add("bazel", "build", target);

ImmutableList<String> command = builder.build();
CommandLine commandLine = new CommandLine(command.toArray(new String[0]));
commandLine.setWorkingDirectory(projectRoot.toAbsolutePath().toString());
commandLine.copyOutputTo(System.err);
commandLine.execute();

if (!commandLine.isSuccessful()) {
throw new WebDriverException("Build failed! " + target + "\n" + commandLine.getStdOut());
}
}
}
38 changes: 16 additions & 22 deletions java/client/test/org/openqa/selenium/testing/StaticResources.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,7 @@

package org.openqa.selenium.testing;

import static org.openqa.selenium.build.DevMode.isInDevMode;

import org.openqa.selenium.build.BuckBuild;
import org.openqa.selenium.build.DevMode;
import org.openqa.selenium.build.BazelBuild;
import org.openqa.selenium.build.InProject;

import java.io.IOException;
Expand All @@ -31,40 +28,37 @@
class StaticResources {

static void ensureAvailable() {
if (!DevMode.isInDevMode()) {
return;
}

System.out.println("Copying resources");

BazelBuild bazel = new BazelBuild();

// W3C emulation
copy(
"//javascript/atoms/fragments:is-displayed",
"org/openqa/selenium/remote/isDisplayed.js");
copy(
"//javascript/webdriver/atoms:get-attribute",
"org/openqa/selenium/remote/getAttribute.js");
bazel.build("//javascript/atoms/fragments:is-displayed");
copy("javascript/atoms/fragments/is-displayed.js",
"org/openqa/selenium/remote/isDisplayed.js");
bazel.build("//javascript/webdriver/atoms:get-attribute");
copy("javascript/webdriver/atoms/get-attribute.js",
"org/openqa/selenium/remote/getAttribute.js");

bazel.build("//third_party/js/selenium:webdriver.json");
// Firefox XPI
copy(
"//third_party/js/selenium:webdriver_prefs",
"org/openqa/selenium/firefox/webdriver_prefs.json");
copy(
"//third_party/js/selenium:webdriver",
copy("third_party/js/selenium:webdriver.json",
"org/openqa/selenium/firefox/webdriver_prefs.json");
bazel.build("third_party/js/selenium:webdriver_xpi");
copy("third_party/js/selenium:webdriver_xpi",
"org/openqa/selenium/firefox/xpi/webdriver.xpi");
}

private static void copy(String buildTarget, String copyTo) {
private static void copy(String copyFrom, String copyTo) {
try {
Path source = InProject.locate("bazel-bin").resolve(copyFrom);
Path dest = InProject.locate("java/client/build/test").resolve(copyTo);

if (Files.exists(dest)) {
// Assume we're good.
return;
}

Path source = new BuckBuild().of(buildTarget).go(isInDevMode());

Files.createDirectories(dest.getParent());
Files.copy(source, dest);
} catch (IOException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

import static java.util.concurrent.TimeUnit.SECONDS;

import org.openqa.selenium.build.BuckBuild;
import org.openqa.selenium.build.BazelBuild;
import org.openqa.selenium.net.NetworkUtils;
import org.openqa.selenium.net.PortProber;
import org.openqa.selenium.net.UrlChecker;
Expand Down Expand Up @@ -53,7 +53,7 @@ public void enableLogCapture() {
*
* @return The new server.
*/
public OutOfProcessSeleniumServer start(String... extraFlags) throws IOException {
public OutOfProcessSeleniumServer start(String mode, String... extraFlags) throws IOException {
log.info("Got a request to start a new selenium server");
if (command != null) {
log.info("Server already started");
Expand All @@ -70,7 +70,8 @@ public OutOfProcessSeleniumServer start(String... extraFlags) throws IOException
cmdLine.add("java");
cmdLine.add("-jar");
cmdLine.add(serverJar);
cmdLine.add("-port");
cmdLine.add(mode);
cmdLine.add("--port");
cmdLine.add(String.valueOf(port));
cmdLine.addAll(Arrays.asList(extraFlags));
command = new CommandLine(cmdLine.toArray(new String[cmdLine.size()]));
Expand All @@ -84,7 +85,7 @@ public OutOfProcessSeleniumServer start(String... extraFlags) throws IOException
command.executeAsync();

try {
URL url = new URL(baseUrl + "/wd/hub/status");
URL url = new URL(baseUrl + "/status");
log.info("Waiting for server status on URL " + url);
new UrlChecker().waitUntilAvailable(30, SECONDS, url);
log.info("Server is ready");
Expand Down Expand Up @@ -113,14 +114,16 @@ public void stop() {
command = null;
}

private String buildServerAndClasspath() throws IOException {
Path serverJar = new BuckBuild().of("//java/server/src/org/openqa/grid/selenium:selenium").go(true);
return serverJar.toAbsolutePath().toString();
private String buildServerAndClasspath() {
new BazelBuild().build("//java/server/src/org/openqa/selenium/grid:selenium_server_deploy.jar");
return InProject.locate("bazel-bin")
.resolve("java/server/src/org/openqa/selenium/grid/selenium_server_deploy.jar")
.toAbsolutePath().toString();
}

public URL getWebDriverUrl() {
try {
return new URL(baseUrl + "/wd/hub");
return new URL(baseUrl);
} catch (MalformedURLException e) {
throw new RuntimeException(e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ private synchronized void startServer() {
}

try {
server.start();
server.start("standalone");
} catch (IOException e) {
throw new RuntimeException(e);
}
Expand Down

0 comments on commit 2602ab2

Please sign in to comment.