Skip to content

Commit

Permalink
test(cdk/testing): start the e2e-app and run webdriver tests in bazel
Browse files Browse the repository at this point in the history
This solution is temporary and linux-only. We should eventually replace
it with a more robust solution
  • Loading branch information
mmalerba committed Mar 30, 2021
1 parent 368cd38 commit c1fbb2d
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 12 deletions.
27 changes: 18 additions & 9 deletions src/cdk/testing/tests/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ ts_library(
)

jasmine_node_test(
name = "webdriver_node_test",
name = "webdriver_jasmine_node_test",
data = [
"@npm//:node_modules",
],
Expand All @@ -103,15 +103,24 @@ jasmine_node_test(
)

web_test(
name = "webdriver_test_chrome",
name = "webdriver_web_test_chrome",
browser = "@npm//@angular/dev-infra-private/browsers/chromium:chromium",
tags = ["e2e"],
test = ":webdriver_node_test",
tags = ["manual"],
test = ":webdriver_jasmine_node_test",
)

web_test(
name = "webdriver_test_firefox",
browser = "@npm//@angular/dev-infra-private/browsers/firefox:firefox",
tags = ["e2e"],
test = ":webdriver_node_test",
sh_test(
name = "webdriver_test_chrome",
testonly = 1,
srcs = ["webdriver-test.sh"],
args = [
"$(location //src/e2e-app:devserver)",
"$(location :webdriver_web_test_chrome)",
],
data = [
":webdriver_web_test_chrome",
"//src/e2e-app:devserver",
],
tags = ["manual"], # TODO(mmalerba): Change to "e2e" once this works on CI.
deps = ["@bazel_tools//tools/bash/runfiles"],
)
45 changes: 45 additions & 0 deletions src/cdk/testing/tests/webdriver-test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# Starts the e2e app on an open port and runs the tests against it.
# TODO(mmalerba): Replace this with something that works on windows as well.

# --- begin runfiles.bash initialization v2 ---
# Copy-pasted from the Bazel Bash runfiles library v2.
# https://github.com/bazelbuild/bazel/blob/master/tools/bash/runfiles/runfiles.bash#L54
set -uo pipefail; f=bazel_tools/tools/bash/runfiles/runfiles.bash
source "${RUNFILES_DIR:-/dev/null}/$f" 2>/dev/null || \
source "$(grep -sm1 "^$f " "${RUNFILES_MANIFEST_FILE:-/dev/null}" | cut -f2- -d' ')" \
2>/dev/null || \
source "$0.runfiles/$f" 2>/dev/null || \
source "$(grep -sm1 "^$f " "$0.runfiles_manifest" | cut -f2- -d' ')" 2>/dev/null || \
source "$(grep -sm1 "^$f " "$0.exe.runfiles_manifest" | cut -f2- -d' ')" 2>/dev/null || \
{ echo>&2 "ERROR: cannot find $f"; exit 1; }; f=; set -e
# --- end runfiles.bash initialization v2 ---

# Save the commands passed from Bazel to start the server and run the tests.
start_server="$1"
run_tests="$2"

# Find a random open port.
# https://unix.stackexchange.com/questions/55913/whats-the-easiest-way-to-find-an-unused-local-port
read lower_port upper_port < /proc/sys/net/ipv4/ip_local_port_range
while :; do
port="$(shuf -i $lower_port-$upper_port -n 1)"
ss -lpn | grep -q ":$port " || break
done

# Start the e2e app server.
"$(rlocation "angular_material/$start_server")" --port "$port" &
server_pid="$!"

# Wait for the server to bind to the port.
while ! lsof "-i:$port" > /dev/null; do sleep 1; done

# Run the tests.
export E2E_APP_PORT="$port"
"$(rlocation "angular_material/$run_tests")"
result="$?"

# Shutdown the server.
kill -9 "$server_pid"

# Exit with the test result.
exit "$result"
7 changes: 4 additions & 3 deletions src/cdk/testing/tests/webdriver.e2e.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,14 @@ interface WebTestMetadata {
}

if (process.env['WEB_TEST_METADATA'] === undefined) {
console.log(`Test running outside of a "web_test" target. No browser found.`);
console.error(`Test running outside of a "web_test" target. No browser found.`);
process.exit(1);
}

const runfiles = require(process.env['BAZEL_NODE_RUNFILES_HELPER']!);
const webTestMetadata: WebTestMetadata =
require(runfiles.resolve(process.env['WEB_TEST_METADATA']));
const port = process.env['E2E_APP_PORT'];

describe('Webdriver test', () => {
let wd: WebDriver;
Expand All @@ -34,8 +35,8 @@ describe('Webdriver test', () => {
});

it('works', async () => {
await wd.get('data:text/html,Test');
await wd.get(`http://localhost:${port}`);
const body = await wd.findElement(By.css('body'));
expect(await body.getText()).toBe('Test');
expect(await body.getText()).toBe('Toggle Navigation Links\ne2e website!\nI am a sibling!');
});
});

0 comments on commit c1fbb2d

Please sign in to comment.