I'm attempting to run the test cases using Selenium Python
via Docker and am encountering several difficulties.
Here is tech stack information,
Selenium :4.11.2
Python Version: 3.10
Chrome driver: 116.0.5845.96
Chrome binary: 116.0.5845.96
Docker file:
RUN pip install logging-formatter-anticrlf
FROM --platform=linux/amd64 python:3.9
# Chrome dependency
WORKDIR /usr/bin
RUN apt-get update && apt-get install -y \
fonts-liberation \
libasound2 \
libatk-bridge2.0-0 \
libatk1.0-0 \
libatspi2.0-0 \
libcups2 \
libdbus-1-3 \
libdrm2 \
libgbm1 \
libgtk-3-0 \
libgtk-4-1 \
libnspr4 \
libnss3 \
libwayland-client0 \
libxcomposite1 \
libxdamage1 \
libxfixes3 \
libxkbcommon0 \
libxrandr2 \
xdg-utils \
libu2f-udev \
libvulkan1 \
software-properties-common \
unzip \
curl \
xvfb
# Chrome instalation
RUN wget -q https://dl.google.com/linux/chrome/deb/pool/main/g/google-chrome-stable/google-chrome-stable_116.0.5845.96-1_amd64.deb
RUN apt --fix-broken install
RUN apt-get install ./google-chrome-stable_116.0.5845.96-1_amd64.deb -y
RUN echo "Chrome: " && google-chrome --version
WORKDIR /module
COPY . .
COPY setup.py setup.py
COPY entrypoint.sh entrypoint.sh
COPY README.md README.md
RUN pip install webdriver-manager==4.0.0
RUN pip install selenium==4.11.2
RUN pip install chromedriver-autoinstaller
RUN pip install selenium --user
RUN pip install py3chrome
RUN wget https://edgedl.me.gvt1.com/edgedl/chrome/chrome-for-testing/116.0.5845.96/linux64/chromedriver-linux64.zip
RUN unzip chromedriver-linux64.zip
RUN mv chromedriver-linux64/chromedriver /usr/bin/chromedriver
RUN chmod 777 /usr/bin/chromedriver
RUN chmod 777 /opt/google/chrome/google-chrome
RUN pytest -k test_case
Test Case:
def test_case():from selenium import webdriver
from selenium.webdriver.chrome.service import Service as ChromeService, Service
from selenium.webdriver.common.by import By chrome_options = webdriver.ChromeOptions()
#s = Service(executable_path=driver_path)
chrome_options.add_argument('--headless')
chrome_options.add_argument('--privileged')
chrome_options.add_argument('--no-sandbox')
chrome_options.add_argument("--disable-setuid-sandbox")
chrome_options.add_argument('--ignore-certificate-errors')
chrome_options.add_argument('--disable-dev-shm-usage')# overcome limited resource problems
chrome_options.add_argument('--disable-extensions')
chrome_options.add_argument('--disable-gpu')
chrome_options.add_argument("--remote-allow-origins=*")
chrome_options.add_argument("--remote-debugging-port=9222") # this
chrome_options.add_experimental_option(
"prefs", {"profile.default_content_setting_values.notifications": 1}
)
driver = webdriver.Chrome(options=chrome_options)
driver.get(“www.google.com”)
When I run the test case in local it is working fine without any issues
But same test case is failing when running in the docker:
I also tried the following methods:
1. Avoid pre-installation of driver and binary; instead, let Selenium Manager handle the installation; the installation took place in the root directory and still produced the same issue. 2. Downloaded the Chrome drivers into a folder and entered the executable path in service (same issue as before) I experimented with different options and capabilities, as well as lowering the selenium and driver versions.Is there anything I'm missing in the docker file to support the chrome binary file? Any assistance would be greatly appreciated.