Skip to content

Commit

Permalink
Update setup-chromedriver.sh
Browse files Browse the repository at this point in the history
  • Loading branch information
rasa authored Oct 18, 2023
1 parent 7c62d0e commit ff16296
Showing 1 changed file with 29 additions and 26 deletions.
55 changes: 29 additions & 26 deletions lib/setup-chromedriver.sh
Original file line number Diff line number Diff line change
@@ -1,44 +1,47 @@
#!/bin/bash
#!/usr/bin/env bash

set -eo pipefail

CURL="curl --silent --location --fail --retry 10"
JSON_URL=https://googlechromelabs.github.io/chrome-for-testing/known-good-versions-with-downloads.json

VERSION=$1
ARCH=$2
VERSION="${1:-}"
ARCH="${2:-linux64}"

sudo=$(command -v sudo)

if [[ "${ARCH}" =~ ^linux64 ]]; then
CHROMEAPP=google-chrome
sudo=$(command -v sudo)
APP="${CHROMEAPP}"
if ! dpkg -s "${APP}" >/dev/null; then
${sudo} apt-key adv --keyserver keyserver.ubuntu.com --recv-keys A040830F7FAC5991
echo "deb [arch=amd64] https://dl.google.com/linux/chrome/deb/ stable main" | ${sudo} tee /etc/apt/sources.list.d/google.list
APP=google-chrome-stable
fi
apps=()
test -z "${sudo}" && apps+=(sudo)
type -a curl > /dev/null 2>&1 || apps+=(curl)
type -a "${CHROMEAPP}" > /dev/null 2>&1 || apps+=("${APP}")
type -a jq > /dev/null 2>&1 || apps+=(jq)
type -a unzip > /dev/null 2>&1 || apps+=(unzip)
if (("${#apps[@]}")); then
echo "Installing ${apps[*]}..."
export DEBIAN_FRONTEND=noninteractive
${sudo} apt-get update
${sudo} apt-get install -y "${apps[@]}"
if command -v dpkg &>/dev/null; then
if ! dpkg -s "${APP}" >/dev/null; then
${sudo} apt-key adv --keyserver keyserver.ubuntu.com --recv-keys A040830F7FAC5991
echo "deb [arch=amd64] https://dl.google.com/linux/chrome/deb/ stable main" | ${sudo} tee /etc/apt/sources.list.d/google.list >/dev/null
APP=google-chrome-stable
fi
apps=()
test -z "${sudo}" && apps+=(sudo)
type -a curl > /dev/null 2>&1 || apps+=(curl)
type -a "${CHROMEAPP}" > /dev/null 2>&1 || apps+=("${APP}")
type -a jq > /dev/null 2>&1 || apps+=(jq)
type -a unzip > /dev/null 2>&1 || apps+=(unzip)
if (("${#apps[@]}")); then
echo "Installing ${apps[*]}..."
export DEBIAN_FRONTEND=noninteractive
${sudo} apt-get update
${sudo} apt-get install -y --no-install-recommends "${apps[@]}"
fi
fi
fi

if [[ "${ARCH}" =~ ^mac64 ]]; then
CHROMEAPP="/Applications/Google Chrome.app/Contents/MacOS/Google Chrome"
fi

if [[ "${VERSION}" ]]; then
if [[ -n "${VERSION}" ]]; then
CHROME_VERSION=$(cut -d '.' -f 1 <<<"${VERSION}")
else
CHROME_VERSION=$("${CHROMEAPP}" --version | cut -f 3 -d ' ' | cut -d '.' -f 1)
CHROME_VERSION=$("${CHROMEAPP}" --version | cut -d ' ' -f 3 | cut -d '.' -f 1)
fi
echo "CHROME_VERSION=${CHROME_VERSION}"

Expand All @@ -54,13 +57,13 @@ if ((CHROME_VERSION < 115)); then
echo "Downloading ${URL}..."
${CURL} -o chromedriver.zip "${URL}"
unzip -o -q chromedriver.zip
sudo mv chromedriver /usr/local/bin/chromedriver
${sudo} mv chromedriver /usr/local/bin/chromedriver
rm -f chromedriver.zip
exit
fi

if [[ -z "${VERSION}" ]]; then
VERSION=$("${CHROMEAPP}" --version | cut -f 3 -d ' ')
VERSION=$("${CHROMEAPP}" --version | cut -d ' ' -f 3)
echo "VERSION=${VERSION}"
fi
if [[ "${ARCH}" =~ ^mac64 ]]; then
Expand All @@ -73,7 +76,7 @@ JQ=".versions[] | select(.version == \"${VERSION}\") | .downloads.chromedriver[]
URL=$(jq -r "${JQ}" <<<"${JSON}")
if [[ -z "${URL}" ]]; then
echo "Falling back to latest version of ChromeDriver for ${ARCH}"
VERSION3=$(cut -d '.' -f1-3 <<<"${VERSION}")
VERSION3=$(cut -d '.' -f 1-3 <<<"${VERSION}")
echo "VERSION3=${VERSION3}"
JQ2="[ .versions[] | select(.version | startswith(\"${VERSION3}.\")) ] | last | .version"
VERSION=$(jq -r "${JQ2}" <<<"${JSON}")
Expand All @@ -85,5 +88,5 @@ echo "Installing ChromeDriver ${VERSION} for ${ARCH}"
echo "Downloading ${URL}..."
${CURL} -o chromedriver.zip "${URL}"
unzip -o -q chromedriver.zip
sudo mv "chromedriver-${ARCH}/chromedriver" /usr/local/bin/chromedriver
${sudo} mv "chromedriver-${ARCH}/chromedriver" /usr/local/bin/chromedriver
rm -fr chromedriver.zip chromedriver-*

0 comments on commit ff16296

Please sign in to comment.