Skip to content

Commit

Permalink
[bazel] Allow pinned browsers to be used
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
shs96c committed Dec 7, 2020
1 parent ebee17a commit 77c187a
Show file tree
Hide file tree
Showing 7 changed files with 409 additions and 31 deletions.
33 changes: 33 additions & 0 deletions common/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,5 +1,19 @@
load("@bazel_skylib//rules:common_settings.bzl", "bool_flag")

package(default_visibility = ["//visibility:public"])

bool_flag(
name = "pin_browsers",
build_setting_default = False
)

config_setting(
name = "use_pinned_browser",
flag_values = {
":pin_browsers": "true",
},
)

config_setting(
name = "linux",
constraint_values = ["@platforms//os:linux"],
Expand Down Expand Up @@ -30,6 +44,7 @@ config_setting(
name = "use_local_chromedriver",
flag_values = {
"@local_drivers//:use_chromedriver": "true",
":pin_browsers": "false",
},
)

Expand All @@ -42,6 +57,7 @@ config_setting(
name = "use_local_geckodriver",
flag_values = {
"@local_drivers//:use_geckodriver": "true",
":pin_browsers": "false",
},
)

Expand All @@ -54,5 +70,22 @@ config_setting(
name = "use_local_msedgedriver",
flag_values = {
"@local_drivers//:use_msedgedriver": "true",
":pin_browsers": "false",
},
)

[
[
config_setting(
name = "use_pinned_%s_%s" % (platform, browser),
flag_values = {
":pin_browsers": "true",
},
constraint_values = [
"@platforms//os:%s" % platform,
],
)

for platform in ["linux", "macos", "windows"]]

for browser in ["chrome", "edge", "firefox"]]
29 changes: 29 additions & 0 deletions common/private/convert_dmg.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#!/bin/bash
# Copyright 2018 Google Inc.
#
# 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.
#
################################################################################
#

DMGFILE=$1
OUTFILE=$2
mkdir -p tmp
VOLUME=$(hdiutil attach "${DMGFILE}" | tail -1 | awk '{print $3}')
cp -r "${VOLUME}/"*.app tmp
hdiutil detach "${VOLUME}" >/dev/null
cd tmp
zip -r "../${OUTFILE}" *
cd ..
rm -rf tmp
rm "${DMGFILE}"
43 changes: 43 additions & 0 deletions common/private/dmg_archive.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@

def _dmg_archive_impl(repository_ctx):
url = repository_ctx.attr.url
(ignored, ignored, dmg_name) = url.rpartition("/")
dmg_name = dmg_name.replace("%20", "_")

attrs = {
"output": dmg_name
}
if repository_ctx.attr.sha256:
attrs.update({"sha256": repository_ctx.attr.sha256})

repository_ctx.download(
url,
**attrs,
)

zip_name = dmg_name.replace(".dmg", ".zip")
repository_ctx.execute([
repository_ctx.path(Label("//common/private:convert_dmg.sh")), dmg_name, zip_name])

repository_ctx.extract(
archive = zip_name,
stripPrefix = repository_ctx.attr.strip_prefix,
)

repository_ctx.file(
"BUILD.bazel",
repository_ctx.attr.build_file_content,
)

dmg_archive = repository_rule(
_dmg_archive_impl,
attrs = {
"url": attr.string(
mandatory = True,
),
"sha256": attr.string(),
"strip_prefix": attr.string(),
"build_file_content": attr.string(),
"build_file": attr.label(),
}
)
40 changes: 40 additions & 0 deletions common/private/pkg_archive.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@

def _pkg_archive_impl(repository_ctx):
url = repository_ctx.attr.url
(ignored, ignored, pkg_name) = url.rpartition("/")
idx = pkg_name.find("?")
if idx != -1:
pkg_name = pkg_name[0:idx]
pkg_name = pkg_name.replace("%20", "_")

attrs = {
"output": pkg_name + ".download"
}
if repository_ctx.attr.sha256:
attrs.update({"sha256": repository_ctx.attr.sha256})

repository_ctx.download(
url,
**attrs,
)

repository_ctx.execute([
repository_ctx.which("pkgutil"), "--expand-full", pkg_name + ".download", pkg_name])

for (key, value) in repository_ctx.attr.move.items():
repository_ctx.execute(["mv", pkg_name + "/" + key, value])

repository_ctx.file("BUILD.bazel", repository_ctx.attr.build_file_content)

pkg_archive = repository_rule(
_pkg_archive_impl,
attrs = {
"url": attr.string(
mandatory = True,
),
"sha256": attr.string(),
"move": attr.string_dict(),
"build_file_content": attr.string(),
"build_file": attr.label(),
}
)
197 changes: 196 additions & 1 deletion common/repositories.bzl
Original file line number Diff line number Diff line change
@@ -1,6 +1,201 @@
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
load("//common/private:dmg_archive.bzl", "dmg_archive")
load("//common/private:drivers.bzl", "local_drivers")
load("//common/private:pkg_archive.bzl", "pkg_archive")

_edge_version = "89.0.713.0"

_versions = {
# The chrome version number is found by visiting http://omahaproxy.appspot.com,
# looking for the current stable version for any platform, and using the "lookup"
# feature to find out "Version information". The "Branch Base Position" gives you
# the version.
# 800218 = 86.0.4240.198
"chrome": {
"linux": {
"url": "https://storage.googleapis.com/chromium-browser-snapshots/Linux_x64/800218/chrome-linux.zip",
"sha256": "01140ec2c7f397023541f65ecd26cf8c5b9a59af90c887dbf7c3ee0344870077",
},
"mac": {
"url": "https://storage.googleapis.com/chromium-browser-snapshots/Mac/800218/chrome-mac.zip",
"sha256": "2e0a534510dba96a4e52effb31bede2cababc9328e624c8c538867f8e71b6292",
},
"windows": {
"url": "https://storage.googleapis.com/chromium-browser-snapshots/Win_x64/800218/chrome-win.zip",
"sha256": None,
},
},
# Versions found by visiting https://chromedriver.chromium.org/downloads
"chromedriver": {
"linux": {
"url": "https://chromedriver.storage.googleapis.com/86.0.4240.22/chromedriver_linux64.zip",
"sha256": "d498eaacc414adbaf638333b59390cdfea5d780f941f57f41fd90280df78b159",
},
"mac": {
"url": "https://chromedriver.storage.googleapis.com/86.0.4240.22/chromedriver_mac64.zip",
"sha256": "351ae30e9feab7ca6ccb94a549afcd62d6355561b78b8386cd4271d480a2fdc6",
},
"windows": {
"url": "https://chromedriver.storage.googleapis.com/86.0.4240.22/chromedriver_win32.zip",
"sha256": None,
},
},
# Ultimately, this will is determined by visiting https://www.microsoft.com/en-us/edge
"edge": {
"linux": {
"url": None,
"sha256": None,
},
"mac": {
"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",
"sha256": "25dfe56b00d5f0af1f9d7ed3d84442fd9d783aa2abe1be93c9ea8e7088f5e5c6",
},
"windows": {
"url": None,
"sha256": None,
},
},
# Versions found by visiting https://developer.microsoft.com/en-us/microsoft-edge/tools/webdriver/
"edgedriver": {
"linux": {
"url": None,
"sha256": "d498eaacc414adbaf638333b59390cdfea5d780f941f57f41fd90280df78b159",
},
"mac": {
"url": "https://msedgedriver.azureedge.net/%s/edgedriver_mac64.zip" % _edge_version,
"sha256": "0282fa65d7f303f59fc4f8001b3d1ce25a44b0394a449f18291f68b6d0f9e691",
},
"windows": {
"url": "https://msedgedriver.azureedge.net/87.0.669.0/edgedriver_win64.zip",
"sha256": None,
},
},
# Versions found by visiting https://ftp.mozilla.org/pub/firefox/releases/
"firefox": {
"linux": {
"url": "https://ftp.mozilla.org/pub/firefox/releases/83.0/linux-x86_64/en-US/firefox-83.0.tar.bz2",
"sha256": "93ff827fdcba92ddb71851c46ac8192a727ed61402e896c6262943e382f92412",
},
"mac": {
"url": "https://ftp.mozilla.org/pub/firefox/releases/83.0/mac/en-US/Firefox%2083.0.dmg",
"sha256": "7e527884e40039c6c97929591754b92394aa965fd61d42158fea5df075636ec6",
},
"windows": {
"url": None,
"sha256": None,
},
},
# Versions found by visiting https://github.com/mozilla/geckodriver/releases
"geckodriver": {
"linux": {
"url": "https://github.com/mozilla/geckodriver/releases/download/v0.28.0/geckodriver-v0.28.0-linux64.tar.gz",
"sha256": "61bfc547a623d7305256611a81ecd24e6bf9dac555529ed6baeafcf8160900da",
},
"mac": {
"url": "https://github.com/mozilla/geckodriver/releases/download/v0.28.0/geckodriver-v0.28.0-macos.tar.gz",
"sha256": "c288ff6db39adfd5eea0e25b4c3e71bfd9fb383eccf521cdd65f67ea78eb1761",
},
"windows": {
"url": None,
"sha256": None,
},
},
}

def _chrome():
http_archive(
name = "linux_chromedriver",
url = _versions["chromedriver"]["linux"]["url"],
sha256 = _versions["chromedriver"]["linux"]["sha256"],
build_file_content = "exports_files([\"chromedriver\"])",
)

http_archive(
name = "linux_chrome",
url = _versions["chrome"]["linux"]["url"],
sha256 = _versions["chrome"]["linux"]["sha256"],
build_file_content = "exports_files([\"chrome\"])",
)

http_archive(
name = "mac_chromedriver",
url = _versions["chromedriver"]["mac"]["url"],
sha256 = _versions["chromedriver"]["mac"]["sha256"],
build_file_content = "exports_files([\"chromedriver\"])",
)

http_archive(
name = "mac_chrome",
url = _versions["chrome"]["mac"]["url"],
sha256 = _versions["chrome"]["mac"]["sha256"],
strip_prefix = "chrome-mac",
build_file_content = "exports_files([\"Chromium.app\"])",
)

def _edge():
http_archive(
name = "mac_edgedriver",
url = _versions["edgedriver"]["mac"]["url"],
sha256 = _versions["edgedriver"]["mac"]["sha256"],
build_file_content = "exports_files([\"msedgedriver\"])",
)

pkg_archive(
name = "mac_edge",
url = _versions["edge"]["mac"]["url"],
sha256 = _versions["edge"]["mac"]["sha256"],
move = {
"MicrosoftEdgeCanary-%s.pkg/Payload/Microsoft Edge Canary.app" % _edge_version: "Edge.app",
},
build_file_content = "exports_files([\"Edge.app\"])",
)

def _firefox():
http_archive(
name = "linux_geckodriver",
url = _versions["geckodriver"]["linux"]["url"],
sha256 = _versions["geckodriver"]["linux"]["sha256"],
build_file_content = "exports_files([\"geckodriver\"])",
)

http_archive(
name = "linux_firefox",
url = _versions["firefox"]["linux"]["url"],
sha256 = _versions["firefox"]["linux"]["sha256"],
build_file_content = "exports_files([\"firefox\"])",
)

http_archive(
name = "mac_geckodriver",
url = _versions["geckodriver"]["mac"]["url"],
sha256 = _versions["geckodriver"]["mac"]["sha256"],
build_file_content = "exports_files([\"geckodriver\"])",
)

dmg_archive(
name = "mac_firefox",
url = _versions["firefox"]["mac"]["url"],
sha256 = _versions["firefox"]["mac"]["sha256"],
build_file_content = "exports_files([\"Firefox.app\"])",
)

# TODO: figure out how to unpack the firefox exe on Windows
# http_archive(
# name = "windows_geckodriver",
# url = "https://github.com/mozilla/geckodriver/releases/download/v0.28.0/geckodriver-v0.28.0-win64.zip",
# sha256 = "49f991b4f25565a5b7008936698f189debc755e6023789adba0c7440b6c960ac",
# build_file_content = "alias(name = \"geckodriver\", actual = \"geckodriver.exe\", visibility = [\"//visibility:public\"])",
# )
#
#
# http_archive(
# name = "windows_firefox",
# url = "https://ftp.mozilla.org/pub/firefox/releases/83.0/win64/en-US/Firefox%20Setup%2083.0.exe",
#
# )

def pin_browsers():
local_drivers()

_chrome()
_edge()
_firefox()
Loading

0 comments on commit 77c187a

Please sign in to comment.