From 7e9d4971c3567c6e528856735b7e73169376befc Mon Sep 17 00:00:00 2001 From: Viet Nguyen Duc Date: Wed, 4 Sep 2024 12:50:38 +0000 Subject: [PATCH] Add test and doc for changing Firefox browser language and locale Fixes https://github.com/SeleniumHQ/docker-selenium/issues/2361 Signed-off-by: Viet Nguyen Duc --- .gitignore | 1 + Makefile | 14 ++++++-- NodeBase/Dockerfile | 1 - NodeChrome/Dockerfile | 1 - NodeEdge/Dockerfile | 1 - NodeFirefox/Dockerfile | 27 +++++++------- NodeFirefox/get_lang_package.sh | 37 +++++++++++++++++++ README.md | 31 ++++++++++++++++ tests/SeleniumTests/__init__.py | 17 +++++++++ tests/docker-compose-v3-test-standalone.yml | 1 + tests/docker-compose-v3-test-video.yml | 1 + tests/test.py | 39 +++++++++++++-------- 12 files changed, 140 insertions(+), 31 deletions(-) create mode 100755 NodeFirefox/get_lang_package.sh diff --git a/.gitignore b/.gitignore index b31c0b11d2..8742e77d3f 100644 --- a/.gitignore +++ b/.gitignore @@ -44,6 +44,7 @@ pip-selfcheck.json # End of https://www.gitignore.io/api/virtualenv tests/tests/* +tests/target/* # Created by https://www.gitignore.io/api/python diff --git a/Makefile b/Makefile index 31e1ce4775..3fa4b0099d 100644 --- a/Makefile +++ b/Makefile @@ -584,8 +584,13 @@ test_edge_standalone: ;; \ esac -test_firefox: - PLATFORMS=$(PLATFORMS) VERSION=$(TAG_VERSION) NAMESPACE=$(NAMESPACE) BASE_RELEASE=$(BASE_RELEASE) BASE_VERSION=$(BASE_VERSION) BINDING_VERSION=$(BINDING_VERSION) SKIP_BUILD=true ./tests/bootstrap.sh NodeFirefox +test_firefox_download_lang_packs: + FIREFOX_VERSION=$$(docker run --rm $(NAME)/node-firefox:$(TAG_VERSION) firefox --version | awk '{print $$3}') ; \ + ./NodeFirefox/get_lang_package.sh $$FIREFOX_VERSION ./tests/target/firefox_lang_packs + +test_firefox: test_firefox_download_lang_packs + PLATFORMS=$(PLATFORMS) VERSION=$(TAG_VERSION) NAMESPACE=$(NAMESPACE) BASE_RELEASE=$(BASE_RELEASE) BASE_VERSION=$(BASE_VERSION) BINDING_VERSION=$(BINDING_VERSION) SKIP_BUILD=true \ + TEST_FIREFOX_INSTALL_LANG_PACKAGE=true ./tests/bootstrap.sh NodeFirefox test_firefox_standalone: PLATFORMS=$(PLATFORMS) VERSION=$(TAG_VERSION) NAMESPACE=$(NAMESPACE) BASE_RELEASE=$(BASE_RELEASE) BASE_VERSION=$(BASE_VERSION) BINDING_VERSION=$(BINDING_VERSION) SKIP_BUILD=true ./tests/bootstrap.sh StandaloneFirefox @@ -645,6 +650,10 @@ test_video: video hub chrome firefox edge chromium docker_compose_file=$(or $(DOCKER_COMPOSE_FILE), docker-compose-v3-test-video.yml) ; \ list_of_tests_amd64=$(or $(LIST_OF_TESTS_AMD64), "NodeChrome NodeChromium NodeFirefox NodeEdge") ; \ list_of_tests_arm64=$(or $(LIST_OF_TESTS_ARM64), "NodeChromium NodeFirefox") ; \ + TEST_FIREFOX_INSTALL_LANG_PACKAGE=$(or $(TEST_FIREFOX_INSTALL_LANG_PACKAGE), "true") ; \ + if [ "$${TEST_FIREFOX_INSTALL_LANG_PACKAGE}" = "true" ]; then \ + make test_firefox_download_lang_packs ; \ + fi ; \ if [ "$(PLATFORMS)" = "linux/amd64" ]; then \ list_nodes="$${list_of_tests_amd64}" ; \ else \ @@ -659,6 +668,7 @@ test_video: video hub chrome firefox edge chromium echo BINDING_VERSION=$(BINDING_VERSION) >> .env ; \ echo TEST_DELAY_AFTER_TEST=$(or $(TEST_DELAY_AFTER_TEST), 2) >> .env ; \ echo SELENIUM_ENABLE_MANAGED_DOWNLOADS=$(or $(SELENIUM_ENABLE_MANAGED_DOWNLOADS), "true") >> .env ; \ + echo TEST_FIREFOX_INSTALL_LANG_PACKAGE=$${TEST_FIREFOX_INSTALL_LANG_PACKAGE} >> .env ; \ echo BASIC_AUTH_USERNAME=$(or $(BASIC_AUTH_USERNAME), "admin") >> .env ; \ echo BASIC_AUTH_PASSWORD=$(or $(BASIC_AUTH_PASSWORD), "admin") >> .env ; \ echo SUB_PATH=$(or $(SUB_PATH), "/selenium") >> .env ; \ diff --git a/NodeBase/Dockerfile b/NodeBase/Dockerfile index e4c8e5ac2d..6ae9fe89fc 100644 --- a/NodeBase/Dockerfile +++ b/NodeBase/Dockerfile @@ -16,7 +16,6 @@ ARG LANG_WHICH=en ARG LANG_WHERE=US ARG ENCODING=UTF-8 ARG LANGUAGE=${LANG_WHICH}_${LANG_WHERE}.${ENCODING} -ARG TARGETARCH USER root diff --git a/NodeChrome/Dockerfile b/NodeChrome/Dockerfile index c7225083ae..f91981e49c 100644 --- a/NodeChrome/Dockerfile +++ b/NodeChrome/Dockerfile @@ -15,7 +15,6 @@ USER root # google-chrome-unstable #============================================ ARG CHROME_VERSION="google-chrome-stable" -ARG TARGETARCH RUN wget -q -O - https://dl-ssl.google.com/linux/linux_signing_key.pub | gpg --dearmor | tee /etc/apt/trusted.gpg.d/google.gpg >/dev/null \ && echo "deb http://dl.google.com/linux/chrome/deb/ stable main" >> /etc/apt/sources.list.d/google-chrome.list \ && apt-get update -qqy \ diff --git a/NodeEdge/Dockerfile b/NodeEdge/Dockerfile index 3eaa2684ed..177b6b4814 100644 --- a/NodeEdge/Dockerfile +++ b/NodeEdge/Dockerfile @@ -13,7 +13,6 @@ USER root # e.g. microsoft-edge-beta=88.0.692.0-1 #============================================ ARG EDGE_VERSION="microsoft-edge-stable" -ARG TARGETARCH RUN wget -q -O - https://packages.microsoft.com/keys/microsoft.asc | gpg --dearmor | tee /etc/apt/trusted.gpg.d/microsoft.gpg >/dev/null \ && echo "deb https://packages.microsoft.com/repos/edge stable main" >> /etc/apt/sources.list.d/microsoft-edge.list \ && apt-get update -qqy \ diff --git a/NodeFirefox/Dockerfile b/NodeFirefox/Dockerfile index f589ca578d..3e11b96829 100644 --- a/NodeFirefox/Dockerfile +++ b/NodeFirefox/Dockerfile @@ -17,15 +17,15 @@ RUN if [ "$(dpkg --print-architecture)" = "amd64" ]; then \ FIREFOX_DOWNLOAD_URL="https://download.mozilla.org/?product=firefox-nightly-latest-ssl&os=linux64-aarch64&lang=en-US" ; \ fi \ && apt-get update -qqy \ - && apt-get -qqy --no-install-recommends install libavcodec-extra \ - libgtk-3-dev libdbus-glib-1-dev \ - && rm -rf /var/lib/apt/lists/* /var/cache/apt/* \ + && apt-get -qqy --no-install-recommends install libavcodec-extra libgtk-3-dev libdbus-glib-1-dev \ + && rm -rf /var/lib/apt/lists/* /var/cache/apt/* ${HOME}/firefox \ && wget --no-verbose -O /tmp/firefox.tar.bz2 $FIREFOX_DOWNLOAD_URL \ - && rm -rf /opt/firefox \ - && tar -C /opt -xjf /tmp/firefox.tar.bz2 \ + && tar -C ${HOME} -xjf /tmp/firefox.tar.bz2 \ && rm /tmp/firefox.tar.bz2 \ - && mv /opt/firefox /opt/firefox-$FIREFOX_VERSION \ - && ln -fs /opt/firefox-$FIREFOX_VERSION/firefox /usr/bin/firefox + && mkdir -p ${HOME}/firefox/distribution/extensions \ + && setfacl -Rm u:${SEL_USER}:rwx ${HOME}/firefox \ + && setfacl -Rm g:${SEL_GROUP}:rwx ${HOME}/firefox \ + && ln -fs ${HOME}/firefox/firefox /usr/bin/firefox #============ # GeckoDriver @@ -46,16 +46,19 @@ RUN LATEST_VERSION=$(curl -sk https://api.github.com/repos/mozilla/geckodriver/r #============================================ # Firefox cleanup script and supervisord file #============================================ -COPY firefox-cleanup.sh /opt/bin/firefox-cleanup.sh -COPY firefox-cleanup.conf /etc/supervisor/conf.d/firefox-cleanup.conf +COPY --chown="${SEL_UID}:${SEL_GID}" firefox-cleanup.sh get_lang_package.sh /opt/bin/ +COPY --chown="${SEL_UID}:${SEL_GID}" firefox-cleanup.conf /etc/supervisor/conf.d/firefox-cleanup.conf +RUN chmod +x /opt/bin/firefox-cleanup.sh /opt/bin/get_lang_package.sh USER ${SEL_UID} #============================================ # Dumping Browser information for config #============================================ -RUN echo "firefox" > /opt/selenium/browser_name -RUN firefox --version | awk '{print $3}' > /opt/selenium/browser_version -RUN echo "\"moz:firefoxOptions\": {\"binary\": \"/usr/bin/firefox\"}" > /opt/selenium/browser_binary_location +RUN echo "firefox" > /opt/selenium/browser_name \ + && firefox --version | awk '{print $3}' > /opt/selenium/browser_version \ + && echo "\"moz:firefoxOptions\": {\"binary\": \"/usr/bin/firefox\"}" > /opt/selenium/browser_binary_location \ + # Download the language pack for Firefox + && /opt/bin/get_lang_package.sh ENV SE_OTEL_SERVICE_NAME="selenium-node-firefox" diff --git a/NodeFirefox/get_lang_package.sh b/NodeFirefox/get_lang_package.sh new file mode 100755 index 0000000000..75092169f1 --- /dev/null +++ b/NodeFirefox/get_lang_package.sh @@ -0,0 +1,37 @@ +#!/bin/bash + +function on_exit() { + local exit_code=$? + rm -f /tmp/xpi_files.txt + exit $exit_code +} +trap on_exit EXIT ERR + +# Script is used to download language packs for a specific version of Firefox. +# It requires the version number as the first argument and the target directory as the second argument. + +VERSION=${1:-$(firefox --version | awk '{print $3}')} +TARGET_DIR="${2:-$(dirname $(readlink -f $(which firefox)))/distribution/extensions}" +BASE_URL="https://ftp.mozilla.org/pub/firefox/releases/$VERSION/linux-x86_64/xpi/" + +# Create target directory if it doesn't exist +mkdir -p "${TARGET_DIR}" + +# Download the list of files +wget -q -O - "${BASE_URL}" | grep -oP '(?<=href=")[^"]*.xpi' >/tmp/xpi_files.txt + +echo "Downloading language packs for Firefox version $VERSION to $TARGET_DIR ..." + +# Loop through each file and download it +while IFS= read -r file; do + file=$(basename "${file}") + echo "Downloading "${BASE_URL}${file}" ..." + curl -sk -o "${TARGET_DIR}/${file}" "${BASE_URL}${file}" + target_file="${TARGET_DIR}/langpack-${file%.xpi}@firefox.mozilla.org.xpi" + mv "${TARGET_DIR}/${file}" "${target_file}" + if [ -f "${target_file}" ]; then + echo "Downloaded ${target_file}" + fi +done