Skip to content

Commit 77c187a

Browse files
committed
[bazel] Allow pinned browsers to be used
This has only been tested on macOS for now, but the approach should work on other platforms too. To enable a pinned browser test, pass the flag `--//common:pin_browsers`. As an example: bazel test --//common:pin_browsers --test_filter=ClickTest \ java/client/test/org/openqa/selenium:LargeTests-edge Using a pinned browser means that you won't need to have the browser and the driver installed locally. Bazel will download them for you.
1 parent ebee17a commit 77c187a

File tree

7 files changed

+409
-31
lines changed

7 files changed

+409
-31
lines changed

common/BUILD.bazel

+33
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,19 @@
1+
load("@bazel_skylib//rules:common_settings.bzl", "bool_flag")
2+
13
package(default_visibility = ["//visibility:public"])
24

5+
bool_flag(
6+
name = "pin_browsers",
7+
build_setting_default = False
8+
)
9+
10+
config_setting(
11+
name = "use_pinned_browser",
12+
flag_values = {
13+
":pin_browsers": "true",
14+
},
15+
)
16+
317
config_setting(
418
name = "linux",
519
constraint_values = ["@platforms//os:linux"],
@@ -30,6 +44,7 @@ config_setting(
3044
name = "use_local_chromedriver",
3145
flag_values = {
3246
"@local_drivers//:use_chromedriver": "true",
47+
":pin_browsers": "false",
3348
},
3449
)
3550

@@ -42,6 +57,7 @@ config_setting(
4257
name = "use_local_geckodriver",
4358
flag_values = {
4459
"@local_drivers//:use_geckodriver": "true",
60+
":pin_browsers": "false",
4561
},
4662
)
4763

@@ -54,5 +70,22 @@ config_setting(
5470
name = "use_local_msedgedriver",
5571
flag_values = {
5672
"@local_drivers//:use_msedgedriver": "true",
73+
":pin_browsers": "false",
5774
},
5875
)
76+
77+
[
78+
[
79+
config_setting(
80+
name = "use_pinned_%s_%s" % (platform, browser),
81+
flag_values = {
82+
":pin_browsers": "true",
83+
},
84+
constraint_values = [
85+
"@platforms//os:%s" % platform,
86+
],
87+
)
88+
89+
for platform in ["linux", "macos", "windows"]]
90+
91+
for browser in ["chrome", "edge", "firefox"]]

common/private/convert_dmg.sh

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#!/bin/bash
2+
# Copyright 2018 Google Inc.
3+
#
4+
# Licensed under the Apache License, Version 2.0 (the "License");
5+
# you may not use this file except in compliance with the License.
6+
# You may obtain a copy of the License at
7+
#
8+
# http://www.apache.org/licenses/LICENSE-2.0
9+
#
10+
# Unless required by applicable law or agreed to in writing, software
11+
# distributed under the License is distributed on an "AS IS" BASIS,
12+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
# See the License for the specific language governing permissions and
14+
# limitations under the License.
15+
#
16+
################################################################################
17+
#
18+
19+
DMGFILE=$1
20+
OUTFILE=$2
21+
mkdir -p tmp
22+
VOLUME=$(hdiutil attach "${DMGFILE}" | tail -1 | awk '{print $3}')
23+
cp -r "${VOLUME}/"*.app tmp
24+
hdiutil detach "${VOLUME}" >/dev/null
25+
cd tmp
26+
zip -r "../${OUTFILE}" *
27+
cd ..
28+
rm -rf tmp
29+
rm "${DMGFILE}"

common/private/dmg_archive.bzl

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
2+
def _dmg_archive_impl(repository_ctx):
3+
url = repository_ctx.attr.url
4+
(ignored, ignored, dmg_name) = url.rpartition("/")
5+
dmg_name = dmg_name.replace("%20", "_")
6+
7+
attrs = {
8+
"output": dmg_name
9+
}
10+
if repository_ctx.attr.sha256:
11+
attrs.update({"sha256": repository_ctx.attr.sha256})
12+
13+
repository_ctx.download(
14+
url,
15+
**attrs,
16+
)
17+
18+
zip_name = dmg_name.replace(".dmg", ".zip")
19+
repository_ctx.execute([
20+
repository_ctx.path(Label("//common/private:convert_dmg.sh")), dmg_name, zip_name])
21+
22+
repository_ctx.extract(
23+
archive = zip_name,
24+
stripPrefix = repository_ctx.attr.strip_prefix,
25+
)
26+
27+
repository_ctx.file(
28+
"BUILD.bazel",
29+
repository_ctx.attr.build_file_content,
30+
)
31+
32+
dmg_archive = repository_rule(
33+
_dmg_archive_impl,
34+
attrs = {
35+
"url": attr.string(
36+
mandatory = True,
37+
),
38+
"sha256": attr.string(),
39+
"strip_prefix": attr.string(),
40+
"build_file_content": attr.string(),
41+
"build_file": attr.label(),
42+
}
43+
)

common/private/pkg_archive.bzl

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
2+
def _pkg_archive_impl(repository_ctx):
3+
url = repository_ctx.attr.url
4+
(ignored, ignored, pkg_name) = url.rpartition("/")
5+
idx = pkg_name.find("?")
6+
if idx != -1:
7+
pkg_name = pkg_name[0:idx]
8+
pkg_name = pkg_name.replace("%20", "_")
9+
10+
attrs = {
11+
"output": pkg_name + ".download"
12+
}
13+
if repository_ctx.attr.sha256:
14+
attrs.update({"sha256": repository_ctx.attr.sha256})
15+
16+
repository_ctx.download(
17+
url,
18+
**attrs,
19+
)
20+
21+
repository_ctx.execute([
22+
repository_ctx.which("pkgutil"), "--expand-full", pkg_name + ".download", pkg_name])
23+
24+
for (key, value) in repository_ctx.attr.move.items():
25+
repository_ctx.execute(["mv", pkg_name + "/" + key, value])
26+
27+
repository_ctx.file("BUILD.bazel", repository_ctx.attr.build_file_content)
28+
29+
pkg_archive = repository_rule(
30+
_pkg_archive_impl,
31+
attrs = {
32+
"url": attr.string(
33+
mandatory = True,
34+
),
35+
"sha256": attr.string(),
36+
"move": attr.string_dict(),
37+
"build_file_content": attr.string(),
38+
"build_file": attr.label(),
39+
}
40+
)

common/repositories.bzl

+196-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,201 @@
11
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
2+
load("//common/private:dmg_archive.bzl", "dmg_archive")
23
load("//common/private:drivers.bzl", "local_drivers")
4+
load("//common/private:pkg_archive.bzl", "pkg_archive")
5+
6+
_edge_version = "89.0.713.0"
7+
8+
_versions = {
9+
# The chrome version number is found by visiting http://omahaproxy.appspot.com,
10+
# looking for the current stable version for any platform, and using the "lookup"
11+
# feature to find out "Version information". The "Branch Base Position" gives you
12+
# the version.
13+
# 800218 = 86.0.4240.198
14+
"chrome": {
15+
"linux": {
16+
"url": "https://storage.googleapis.com/chromium-browser-snapshots/Linux_x64/800218/chrome-linux.zip",
17+
"sha256": "01140ec2c7f397023541f65ecd26cf8c5b9a59af90c887dbf7c3ee0344870077",
18+
},
19+
"mac": {
20+
"url": "https://storage.googleapis.com/chromium-browser-snapshots/Mac/800218/chrome-mac.zip",
21+
"sha256": "2e0a534510dba96a4e52effb31bede2cababc9328e624c8c538867f8e71b6292",
22+
},
23+
"windows": {
24+
"url": "https://storage.googleapis.com/chromium-browser-snapshots/Win_x64/800218/chrome-win.zip",
25+
"sha256": None,
26+
},
27+
},
28+
# Versions found by visiting https://chromedriver.chromium.org/downloads
29+
"chromedriver": {
30+
"linux": {
31+
"url": "https://chromedriver.storage.googleapis.com/86.0.4240.22/chromedriver_linux64.zip",
32+
"sha256": "d498eaacc414adbaf638333b59390cdfea5d780f941f57f41fd90280df78b159",
33+
},
34+
"mac": {
35+
"url": "https://chromedriver.storage.googleapis.com/86.0.4240.22/chromedriver_mac64.zip",
36+
"sha256": "351ae30e9feab7ca6ccb94a549afcd62d6355561b78b8386cd4271d480a2fdc6",
37+
},
38+
"windows": {
39+
"url": "https://chromedriver.storage.googleapis.com/86.0.4240.22/chromedriver_win32.zip",
40+
"sha256": None,
41+
},
42+
},
43+
# Ultimately, this will is determined by visiting https://www.microsoft.com/en-us/edge
44+
"edge": {
45+
"linux": {
46+
"url": None,
47+
"sha256": None,
48+
},
49+
"mac": {
50+
"url": "https://officecdn-microsoft-com.akamaized.net/pr/C1297A47-86C4-4C1F-97FA-950631F94777/MacAutoupdate/MicrosoftEdgeCanary-89.0.713.0.pkg?platform=Mac&Consent=0&channel=Canary",
51+
"sha256": "25dfe56b00d5f0af1f9d7ed3d84442fd9d783aa2abe1be93c9ea8e7088f5e5c6",
52+
},
53+
"windows": {
54+
"url": None,
55+
"sha256": None,
56+
},
57+
},
58+
# Versions found by visiting https://developer.microsoft.com/en-us/microsoft-edge/tools/webdriver/
59+
"edgedriver": {
60+
"linux": {
61+
"url": None,
62+
"sha256": "d498eaacc414adbaf638333b59390cdfea5d780f941f57f41fd90280df78b159",
63+
},
64+
"mac": {
65+
"url": "https://msedgedriver.azureedge.net/%s/edgedriver_mac64.zip" % _edge_version,
66+
"sha256": "0282fa65d7f303f59fc4f8001b3d1ce25a44b0394a449f18291f68b6d0f9e691",
67+
},
68+
"windows": {
69+
"url": "https://msedgedriver.azureedge.net/87.0.669.0/edgedriver_win64.zip",
70+
"sha256": None,
71+
},
72+
},
73+
# Versions found by visiting https://ftp.mozilla.org/pub/firefox/releases/
74+
"firefox": {
75+
"linux": {
76+
"url": "https://ftp.mozilla.org/pub/firefox/releases/83.0/linux-x86_64/en-US/firefox-83.0.tar.bz2",
77+
"sha256": "93ff827fdcba92ddb71851c46ac8192a727ed61402e896c6262943e382f92412",
78+
},
79+
"mac": {
80+
"url": "https://ftp.mozilla.org/pub/firefox/releases/83.0/mac/en-US/Firefox%2083.0.dmg",
81+
"sha256": "7e527884e40039c6c97929591754b92394aa965fd61d42158fea5df075636ec6",
82+
},
83+
"windows": {
84+
"url": None,
85+
"sha256": None,
86+
},
87+
},
88+
# Versions found by visiting https://github.com/mozilla/geckodriver/releases
89+
"geckodriver": {
90+
"linux": {
91+
"url": "https://github.com/mozilla/geckodriver/releases/download/v0.28.0/geckodriver-v0.28.0-linux64.tar.gz",
92+
"sha256": "61bfc547a623d7305256611a81ecd24e6bf9dac555529ed6baeafcf8160900da",
93+
},
94+
"mac": {
95+
"url": "https://github.com/mozilla/geckodriver/releases/download/v0.28.0/geckodriver-v0.28.0-macos.tar.gz",
96+
"sha256": "c288ff6db39adfd5eea0e25b4c3e71bfd9fb383eccf521cdd65f67ea78eb1761",
97+
},
98+
"windows": {
99+
"url": None,
100+
"sha256": None,
101+
},
102+
},
103+
}
104+
105+
def _chrome():
106+
http_archive(
107+
name = "linux_chromedriver",
108+
url = _versions["chromedriver"]["linux"]["url"],
109+
sha256 = _versions["chromedriver"]["linux"]["sha256"],
110+
build_file_content = "exports_files([\"chromedriver\"])",
111+
)
112+
113+
http_archive(
114+
name = "linux_chrome",
115+
url = _versions["chrome"]["linux"]["url"],
116+
sha256 = _versions["chrome"]["linux"]["sha256"],
117+
build_file_content = "exports_files([\"chrome\"])",
118+
)
119+
120+
http_archive(
121+
name = "mac_chromedriver",
122+
url = _versions["chromedriver"]["mac"]["url"],
123+
sha256 = _versions["chromedriver"]["mac"]["sha256"],
124+
build_file_content = "exports_files([\"chromedriver\"])",
125+
)
126+
127+
http_archive(
128+
name = "mac_chrome",
129+
url = _versions["chrome"]["mac"]["url"],
130+
sha256 = _versions["chrome"]["mac"]["sha256"],
131+
strip_prefix = "chrome-mac",
132+
build_file_content = "exports_files([\"Chromium.app\"])",
133+
)
134+
135+
def _edge():
136+
http_archive(
137+
name = "mac_edgedriver",
138+
url = _versions["edgedriver"]["mac"]["url"],
139+
sha256 = _versions["edgedriver"]["mac"]["sha256"],
140+
build_file_content = "exports_files([\"msedgedriver\"])",
141+
)
142+
143+
pkg_archive(
144+
name = "mac_edge",
145+
url = _versions["edge"]["mac"]["url"],
146+
sha256 = _versions["edge"]["mac"]["sha256"],
147+
move = {
148+
"MicrosoftEdgeCanary-%s.pkg/Payload/Microsoft Edge Canary.app" % _edge_version: "Edge.app",
149+
},
150+
build_file_content = "exports_files([\"Edge.app\"])",
151+
)
152+
153+
def _firefox():
154+
http_archive(
155+
name = "linux_geckodriver",
156+
url = _versions["geckodriver"]["linux"]["url"],
157+
sha256 = _versions["geckodriver"]["linux"]["sha256"],
158+
build_file_content = "exports_files([\"geckodriver\"])",
159+
)
160+
161+
http_archive(
162+
name = "linux_firefox",
163+
url = _versions["firefox"]["linux"]["url"],
164+
sha256 = _versions["firefox"]["linux"]["sha256"],
165+
build_file_content = "exports_files([\"firefox\"])",
166+
)
167+
168+
http_archive(
169+
name = "mac_geckodriver",
170+
url = _versions["geckodriver"]["mac"]["url"],
171+
sha256 = _versions["geckodriver"]["mac"]["sha256"],
172+
build_file_content = "exports_files([\"geckodriver\"])",
173+
)
174+
175+
dmg_archive(
176+
name = "mac_firefox",
177+
url = _versions["firefox"]["mac"]["url"],
178+
sha256 = _versions["firefox"]["mac"]["sha256"],
179+
build_file_content = "exports_files([\"Firefox.app\"])",
180+
)
181+
182+
# TODO: figure out how to unpack the firefox exe on Windows
183+
# http_archive(
184+
# name = "windows_geckodriver",
185+
# url = "https://github.com/mozilla/geckodriver/releases/download/v0.28.0/geckodriver-v0.28.0-win64.zip",
186+
# sha256 = "49f991b4f25565a5b7008936698f189debc755e6023789adba0c7440b6c960ac",
187+
# build_file_content = "alias(name = \"geckodriver\", actual = \"geckodriver.exe\", visibility = [\"//visibility:public\"])",
188+
# )
189+
#
190+
#
191+
# http_archive(
192+
# name = "windows_firefox",
193+
# url = "https://ftp.mozilla.org/pub/firefox/releases/83.0/win64/en-US/Firefox%20Setup%2083.0.exe",
194+
#
195+
# )
3196

4197
def pin_browsers():
5198
local_drivers()
6-
199+
_chrome()
200+
_edge()
201+
_firefox()

0 commit comments

Comments
 (0)