Skip to content

Commit

Permalink
Adding the first .NET Bazel test target
Browse files Browse the repository at this point in the history
This target will run the tests in the Bazel build environment. At
present, the tests run against Chrome. A future commit will enable
executing tests against any of the supported browser configurations.
  • Loading branch information
jimevans committed May 3, 2019
1 parent 8f1821d commit 490f2d5
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 16 deletions.
5 changes: 4 additions & 1 deletion common/src/web/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,8 @@ filegroup(
"*",
"**/*",
]),
visibility = ["//java/client/test/org/openqa/selenium/environment:__pkg__"],
visibility = [
"//java/client/test/org/openqa/selenium/environment:__pkg__",
"//dotnet/test/common:__pkg__"
],
)
40 changes: 40 additions & 0 deletions dotnet/test/common/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
load("@io_bazel_rules_dotnet//dotnet:defs.bzl", "net_nunit3_test")

net_nunit3_test(
name = "chrome",
srcs = glob([
"*.cs",
"CustomTestAttributes/*.cs",
"Environment/*.cs",
"HTML5/*.cs",
"Interactions/*.cs",
]),
deps = [
"@io_bazel_rules_dotnet//dotnet/stdlib.net:system.dll",
"@io_bazel_rules_dotnet//dotnet/stdlib.net:system.core.dll",
"@io_bazel_rules_dotnet//dotnet/stdlib.net:system.data.dll",
"@io_bazel_rules_dotnet//dotnet/stdlib.net:system.drawing.dll",
"@io_bazel_rules_dotnet//dotnet/stdlib.net:system.xml.dll",
"//dotnet/src/webdriver:net47assembly",
"@json.net//:net45",
"@moq//:net45",
"@nunit//:net45",
"@benderproxy//:net47",
],
data = [
"appconfig.json",
"//java/client/test/org/openqa/selenium/environment:WebServer",
"//java/client/test/org/openqa/selenium/environment:WebServer_deploy.jar",
"//common/src/web",
],
size = "enormous",
args = [
"--agents=1",
"--params=ConfigFile=$(location appconfig.json)",
"--params=ActiveWebsiteConfig=HostsFileRedirect",
"--params=ActiveDriverConfig=Chrome",
],
visibility = ["//visibility:public"],
out = "WebDriver.Common.Tests.dll",
dotnet_context_data = "@io_bazel_rules_dotnet//:net_context_data_net47"
)
17 changes: 10 additions & 7 deletions dotnet/test/common/Environment/EnvironmentManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,7 @@ public class EnvironmentManager
private EnvironmentManager()
{
string currentDirectory = this.CurrentDirectory;
string configFile = TestContext.Parameters.Get("ConfigFile", string.Empty);
if (string.IsNullOrEmpty(configFile))
{
configFile = Path.Combine(currentDirectory, "appconfig.json");
}
string configFile = Path.Combine(currentDirectory, "appconfig.json");

string content = File.ReadAllText(configFile);
TestEnvironment env = JsonConvert.DeserializeObject<TestEnvironment>(content);
Expand All @@ -45,7 +41,10 @@ private EnvironmentManager()

urlBuilder = new UrlBuilder(websiteConfig);

string projectRoot = TestContext.Parameters.Get("RootPath", string.Empty);
// When run using the `bazel test` command, the following environment
// variable will be set. If not set, we're running from a build system
// outside Bazel, and need to locate the directory containing the jar.
string projectRoot = System.Environment.GetEnvironmentVariable("TEST_SRCDIR");
if (string.IsNullOrEmpty(projectRoot))
{
DirectoryInfo info = new DirectoryInfo(currentDirectory);
Expand All @@ -55,7 +54,11 @@ private EnvironmentManager()
}

info = info.Parent;
projectRoot = info.FullName;
projectRoot = Path.Combine(info.FullName, "bazel-bin");
}
else
{
projectRoot += "/selenium";
}

webServer = new TestWebServer(projectRoot);
Expand Down
41 changes: 33 additions & 8 deletions dotnet/test/common/Environment/TestWebServer.cs
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Diagnostics;
using System.Text;

namespace OpenQA.Selenium.Environment
{
public class TestWebServer
{
private Process webserverProcess;

private string standaloneTestJar = @"buck-out/gen/java/client/test/org/openqa/selenium/environment/webserver.jar";
private string webserverClassName = "org.openqa.selenium.environment.webserver.JettyAppServer";
private string standaloneTestJar = @"java/client/test/org/openqa/selenium/environment/WebServer_deploy.jar";
private string projectRootPath;

private StringBuilder outputData = new StringBuilder();

public TestWebServer(string projectRoot)
{
projectRootPath = projectRoot;
Expand All @@ -29,7 +32,7 @@ public void Start()
string.Format(
"Test webserver jar at {0} didn't exist. Project root is {2}. Please build it using something like {1}.",
standaloneTestJar,
"go //java/client/test/org/openqa/selenium/environment:webserver",
"bazel build //java/client/test/org/openqa/selenium/environment:WebServer_deploy.jar",
projectRootPath));
}

Expand All @@ -39,16 +42,37 @@ public void Start()
javaExecutableName = javaExecutableName + ".exe";
}

string processArgs = string.Format("-Duser.dir={0} -cp {1} {2}", projectRootPath, standaloneTestJar, webserverClassName);
List<string> javaSystemProperties = new List<string>();
javaSystemProperties.Add("org.openqa.selenium.environment.webserver.ignoreMissingJsRoots=true");

StringBuilder processArgsBuilder = new StringBuilder();
foreach (string systemProperty in javaSystemProperties)
{
if (processArgsBuilder.Length > 0)
{
processArgsBuilder.Append(" ");
}

processArgsBuilder.AppendFormat("-D{0}", systemProperty);
}

if (processArgsBuilder.Length > 0)
{
processArgsBuilder.Append(" ");
}

processArgsBuilder.AppendFormat("-jar {0}", standaloneTestJar);

webserverProcess = new Process();
webserverProcess.StartInfo.FileName = javaExecutableName;
webserverProcess.StartInfo.Arguments = processArgs;
webserverProcess.StartInfo.Arguments = processArgsBuilder.ToString();
webserverProcess.StartInfo.WorkingDirectory = projectRootPath;
webserverProcess.Start();
DateTime timeout = DateTime.Now.Add(TimeSpan.FromSeconds(30));

TimeSpan timeout = TimeSpan.FromSeconds(30);
DateTime endTime = DateTime.Now.Add(TimeSpan.FromSeconds(30));
bool isRunning = false;
while (!isRunning && DateTime.Now < timeout)
while (!isRunning && DateTime.Now < endTime)
{
// Poll until the webserver is correctly serving pages.
HttpWebRequest request = WebRequest.Create(EnvironmentManager.Instance.UrlBuilder.LocalWhereIs("simpleTest.html")) as HttpWebRequest;
Expand All @@ -67,7 +91,8 @@ public void Start()

if (!isRunning)
{
throw new TimeoutException("Could not start the test web server in 15 seconds");
string errorMessage = string.Format("Could not start the test web server in {0} seconds. Process Args: {1}", timeout.TotalSeconds, processArgsBuilder);
throw new TimeoutException(errorMessage);
}
}
}
Expand Down

0 comments on commit 490f2d5

Please sign in to comment.