forked from bazelbuild/rules_closure
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwebdriver_test.bzl
More file actions
125 lines (111 loc) · 3.98 KB
/
webdriver_test.bzl
File metadata and controls
125 lines (111 loc) · 3.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# Copyright 2022 The Closure Rules Authors. All rights reserved.
#
# Licensed 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.
"""Macro for running webtest with a test driver."""
load("@rules_webtesting//web:web.bzl", "web_test", "web_test_suite")
load("//closure:webfiles/web_library.bzl", "web_library")
def webdriver_test(
name,
browsers,
test_file_js,
data = None,
tags = [],
visibility = None,
**kwargs):
""" Macro for running Closure JavaScript binary on browsers.
Args:
test_file_js: JavaScipt binary output from closure_js_binary
**kwargs: Additional arguments for web_test rules.
To run the test target, use `bazel test :<name>`.
To debug the test locally on the browser, add '_debug' to the name of your test. E.g.: `bazel run :<name>_debug`.
Open the URL printed in the console and click on the html link to the generated testsuite.
"""
html = "gen_html_%s" % name
_gen_test_html(
name = html,
test_file_js = test_file_js,
)
path = "/"
html_webpath = "%s%s.html" % (path, html)
web_library(
name = "%s_test_files" % name,
srcs = [html, test_file_js],
path = path,
testonly = True,
)
# set up a development web server that links to the test for debugging purposes.
web_library(
name = "%s_debug" % name,
srcs = data if data else [],
deps = [":%s_test_files" % name],
path = path,
testonly = True,
use_full_path = True,
)
web_library(
name = "%s_test_runner" % name,
srcs = data if data else [],
deps = [":%s_test_files" % name],
path = path,
server = Label("//java/io/bazel/rules/closure/testing:webdriver_test_bin"),
testonly = True,
use_full_path = True,
)
if len(browsers) == 1:
web_test(
name = name,
data = [test_file_js, html],
browser = str(browsers[0]),
test = ":%s_test_runner" % name,
args = [html_webpath],
tags = tags + ["no-sandbox", "native"],
visibility = visibility,
**kwargs
)
else:
web_test_suite(
name = name,
data = [test_file_js, html],
browsers = browsers,
test = ":%s_test_runner" % name,
args = [html_webpath],
tags = tags + ["no-sandbox", "native"],
visibility = visibility,
**kwargs
)
def _gen_test_html_impl(ctx):
"""Implementation of the gen_test_html rule."""
ctx.actions.expand_template(
template = ctx.file._template,
output = ctx.outputs.html_file,
substitutions = {
"{{TEST_FILE_JS}}": ctx.attr.test_file_js,
},
)
runfiles = ctx.runfiles(files = [ctx.outputs.html_file], collect_default = True)
return [DefaultInfo(runfiles = runfiles)]
# Used to generate default test.html file for running Closure-based JS tests.
# The test_file_js argument specifies the name of the JS file containing tests,
# typically created with closure_js_binary.
# The output is created from gen_test_html.template file.
_gen_test_html = rule(
implementation = _gen_test_html_impl,
attrs = {
"test_file_js": attr.string(mandatory = True),
"_template": attr.label(
default = Label("//closure/testing:gen_webtest_html.template"),
allow_single_file = True,
),
},
outputs = {"html_file": "%{name}.html"},
)