Skip to content

Commit 9587249

Browse files
[build] Set up python for pinned browsers
This allows people to pass in --//common:pin_browsers to python tests and bazel will setup the browsers and use them. Work from pairing session with Simon Stewart
1 parent d39e89d commit 9587249

File tree

5 files changed

+153
-18
lines changed

5 files changed

+153
-18
lines changed

py/BUILD.bazel

+46-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ load("@rules_python//experimental/python:wheel.bzl", "py_package", "py_wheel")
33
load("@dev_requirements//:requirements.bzl", "requirement")
44
load("//common:defs.bzl", "copy_file")
55
load("//py:defs.bzl", "py_test_suite", "pytest_test")
6+
load("//py/private:browsers.bzl", "BROWSERS")
67

78
BROWSER_VERSIONS = [
89
"v85",
@@ -143,7 +144,7 @@ py_library(
143144
data = [
144145
"pytest.ini",
145146
"setup.cfg",
146-
"test/selenium/webdriver/common/test_file.txt"
147+
"test/selenium/webdriver/common/test_file.txt",
147148
],
148149
imports = ["."],
149150
deps = [
@@ -216,6 +217,50 @@ py_library(
216217
deps = [],
217218
)
218219

220+
[
221+
py_test_suite(
222+
name = "auto-%s" % browser,
223+
size = "large",
224+
srcs = glob(
225+
[
226+
"test/selenium/webdriver/common/**/*.py",
227+
"test/selenium/webdriver/support/**/*.py",
228+
],
229+
exclude = ["test/selenium/webdriver/common/print_pdf_tests.py"],
230+
),
231+
args = [
232+
"--instafail",
233+
] + BROWSERS[browser]["args"],
234+
data = BROWSERS[browser]["data"],
235+
tags = [
236+
"no-sandbox",
237+
] + BROWSERS[browser]["tags"],
238+
deps = [
239+
":init-tree",
240+
":selenium",
241+
":webserver",
242+
requirement("attrs"),
243+
requirement("debugpy"),
244+
requirement("idna"),
245+
requirement("iniconfig"),
246+
requirement("importlib_metadata"),
247+
requirement("h11"),
248+
requirement("more_itertools"),
249+
requirement("multidict"),
250+
requirement("outcome"),
251+
requirement("pluggy"),
252+
requirement("py"),
253+
requirement("pytest"),
254+
requirement("pytest-instafail"),
255+
requirement("pytest-trio"),
256+
requirement("sortedcontainers"),
257+
requirement("sniffio"),
258+
requirement("zipp"),
259+
],
260+
)
261+
for browser in BROWSERS.keys()
262+
]
263+
219264
py_test_suite(
220265
name = "test-chrome",
221266
size = "large",

py/conftest.py

+12-11
Original file line numberDiff line numberDiff line change
@@ -31,15 +31,15 @@
3131
from urllib.request import urlopen
3232

3333
drivers = (
34-
'Chrome',
35-
'Edge',
36-
'Firefox',
37-
'Ie',
38-
'Remote',
39-
'Safari',
40-
'WebKitGTK',
41-
'ChromiumEdge',
42-
'WPEWebKit',
34+
'chrome',
35+
'edge',
36+
'firefox',
37+
'ie',
38+
'remote',
39+
'safari',
40+
'webkitgtk',
41+
'chromiumedge',
42+
'wpewebkit',
4343
)
4444

4545

@@ -74,7 +74,7 @@ def driver(request):
7474
kwargs = {}
7575

7676
try:
77-
driver_class = request.param
77+
driver_class = request.param.capitalize()
7878
except AttributeError:
7979
raise Exception('This test requires a --driver to be specified.')
8080

@@ -122,14 +122,15 @@ def fin():
122122
options = get_options('Firefox', request.config)
123123
if driver_class == 'WebKitGTK':
124124
options = get_options(driver_class, request.config)
125-
if driver_class == 'ChromiumEdge':
125+
if driver_class == 'Edge':
126126
options = get_options(driver_class, request.config)
127127
if driver_class == 'WPEWebKit':
128128
options = get_options(driver_class, request.config)
129129
if driver_path is not None:
130130
kwargs['executable_path'] = driver_path
131131
if options is not None:
132132
kwargs['options'] = options
133+
133134
driver_instance = getattr(webdriver, driver_class)(**kwargs)
134135
yield driver_instance
135136

py/private/browsers.bzl

+84
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
load(
2+
"//common:browsers.bzl",
3+
"COMMON_TAGS",
4+
"chrome_data",
5+
"edge_data",
6+
"firefox_data",
7+
)
8+
9+
headless_args = select({
10+
"@selenium//common:use_headless_browser": [
11+
"--headless=true",
12+
],
13+
"//conditions:default": [],
14+
})
15+
16+
chrome_args = select({
17+
"@selenium//common:use_pinned_linux_chrome": [
18+
"--driver-binary=$(location @linux_chromedriver//:chromedriver)",
19+
"--browser-binary=$(location @linux_chrome//:chrome-linux)/linux-chrome/chrome",
20+
],
21+
"@selenium//common:use_pinned_macos_chrome": [
22+
"--driver-binary=$(location @mac_chromedriver//:chromedriver)",
23+
"--browser-binary=$(location @mac_chrome//:Chromium.app)/Contents/MacOS/Chromium",
24+
],
25+
"@selenium//common:use_local_chromedriver": [
26+
"--driver-binary=$(location @selenium//common:chromedriver)",
27+
],
28+
"//conditions:default": []
29+
}) + headless_args
30+
31+
edge_args = select({
32+
"@selenium//common:use_pinned_macos_edge": [
33+
"--driver-binary=$(location @mac_edgedriver//:msedgedriver)",
34+
"--browser-binary",
35+
"$(location @mac_edge//:Edge.app)/Contents/MacOS/Microsoft Edge",
36+
],
37+
"@selenium//common:use_local_msedgedriver": [
38+
"-Dwebdriver.edge.driver=$(location @selenium//common:msedgedriver)",
39+
],
40+
"//conditions:default": []
41+
}) + headless_args
42+
43+
firefox_args = select({
44+
"@selenium//common:use_pinned_linux_firefox": [
45+
"--driver-binary=$(location @linux_geckodriver//:geckodriver)",
46+
"--browser-binary=$(location @linux_firefox//:firefox)/firefox",
47+
],
48+
"@selenium//common:use_pinned_macos_firefox": [
49+
"--driver-binary=$(location @mac_geckodriver//:geckodriver)",
50+
"--browser-binary=$(location @mac_firefox//:Firefox.app)/Contents/MacOS/firefox",
51+
],
52+
"@selenium//common:use_local_geckodriver": [
53+
"--driver-binary=$(location @selenium//common:geckodriver)",
54+
],
55+
"//conditions:default": [],
56+
}) + headless_args
57+
58+
BROWSERS = {
59+
"chrome": {
60+
"args": ["--driver=chrome"] + chrome_args,
61+
"data": chrome_data,
62+
"tags": COMMON_TAGS + ["chrome"],
63+
},
64+
"edge": {
65+
"args": ["--driver=edge"] + edge_args,
66+
"data": edge_data,
67+
"tags": COMMON_TAGS + ["edge"],
68+
},
69+
"firefox": {
70+
"args": ["--driver=firefox"] + firefox_args,
71+
"data": firefox_data,
72+
"tags": COMMON_TAGS + ["firefox"],
73+
},
74+
"ie": {
75+
"args": ["--driver=ie"],
76+
"data": [],
77+
"tags": COMMON_TAGS + ["ie"],
78+
},
79+
"safari": {
80+
"args": ["--driver=safari"],
81+
"data": [],
82+
"tags": COMMON_TAGS + ["safari"],
83+
}
84+
}

py/private/pytest.bzl

+11-3
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
load("@rules_python//python:defs.bzl", "PyInfo", "py_test")
22

33
def _stringify(paths):
4-
return "[%s]" % (", ".join(["\"%s\"" % path for path in paths]))
4+
return repr(paths)
55

66
def _pytest_runner_impl(ctx):
77
if len(ctx.attr.srcs) == 0:
88
fail("No test files specified.")
99

10+
expanded_args = [ctx.expand_location(arg, ctx.attr.data) for arg in ctx.attr.args]
11+
1012
runner = ctx.actions.declare_file(ctx.attr.name)
1113
ctx.actions.write(
1214
runner,
@@ -17,13 +19,14 @@ if __name__ == "__main__":
1719
1820
args = ["-ra"] + %s + sys.argv[1:] + %s
1921
20-
sys.exit(pytest.main(args))""" % (_stringify(ctx.attr.args), _stringify([src.path for src in ctx.files.srcs])),
22+
sys.exit(pytest.main(args))""" % (_stringify(expanded_args), _stringify([src.path for src in ctx.files.srcs])),
2123
is_executable = True,
2224
)
2325

2426
return [
2527
DefaultInfo(
2628
files = depset([runner]),
29+
runfiles = ctx.runfiles(ctx.files.data),
2730
executable = runner,
2831
),
2932
]
@@ -42,14 +45,18 @@ _pytest_runner = rule(
4245
"args": attr.string_list(
4346
default = [],
4447
),
48+
"data": attr.label_list(
49+
allow_empty = True,
50+
allow_files = True,
51+
),
4552
"python_version": attr.string(
4653
values = ["PY2", "PY3"],
4754
default = "PY3",
4855
),
4956
},
5057
)
5158

52-
def pytest_test(name, srcs, deps = None, args = None, python_version = None, **kwargs):
59+
def pytest_test(name, srcs, deps = None, args = None, data = None, python_version = None, **kwargs):
5360
runner_target = "%s-runner.py" % name
5461

5562
_pytest_runner(
@@ -58,6 +65,7 @@ def pytest_test(name, srcs, deps = None, args = None, python_version = None, **k
5865
srcs = srcs,
5966
deps = deps,
6067
args = args,
68+
data = data,
6169
python_version = python_version,
6270
)
6371

py/selenium/webdriver/edge/webdriver.py

-3
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,6 @@ def __init__(self, executable_path='msedgedriver', port=DEFAULT_PORT,
5656
warnings.warn('executable_path has been deprecated, please pass in a Service object',
5757
DeprecationWarning, stacklevel=2)
5858

59-
if options and options.use_chromium:
60-
executable_path = "msedgedriver"
61-
6259
if not service:
6360
service = Service(executable_path, port, service_args, service_log_path)
6461

0 commit comments

Comments
 (0)