GitlabCI Docker in Docker and Selenium/ChromeDriver : AttributError

57 Views Asked by At

I'm having a problem using docker in docker and selenium/chromedriver to create a Gitlab pipeline.

.gitlab-ci.yml

stages:
  - build
  - test

default:
  image: docker:24.0.5
  services:
    - docker:24.0.5-dind
  before_script:
    - docker info
    - docker login -u ********* -p ************* $CI_REGISTRY  

build:
  stage: build
  script:
    - docker build -t **************/my-docker-image:latest .
    - docker push ***************/my-docker-image:latest

test:
  stage: test
  script:
    - docker-compose up -d
    - apk add --no-cache python3
    - apk add --no-cache py3-pip
    - apk add --no-cache chromium
    - pip3 install pytest
    - pip3 install selenium
    - pip3 install --upgrade webdriver_manager

    
    # Launch Tests
    - pytest tests_selenium/tests_selenium.py
    - docker-compose down

When I push my project into Gitlab, I get this error :

    def browser():
        options = Options()
        options.add_argument('--headless')
        options.add_argument('--no-sandbox')
        options.add_argument('--disable-dev-shm-usage')
        options.add_argument('--incognito')
>       driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()), options=options)
tests_selenium/tests_selenium.py:21: 
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 
/usr/lib/python3.11/site-packages/webdriver_manager/chrome.py:40: in install
    driver_path = self._get_driver_binary_path(self.driver)
/usr/lib/python3.11/site-packages/webdriver_manager/core/manager.py:40: in _get_driver_binary_path
    file = self._download_manager.download_file(driver.get_driver_download_url(os_type))
/usr/lib/python3.11/site-packages/webdriver_manager/drivers/chrome.py:32: in get_driver_download_url
    driver_version_to_download = self.get_driver_version_to_download()
/usr/lib/python3.11/site-packages/webdriver_manager/core/driver.py:48: in get_driver_version_to_download
    return self.get_latest_release_version()
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 
self = <webdriver_manager.drivers.chrome.ChromeDriver object at 0x7f828bcfaed0>
    def get_latest_release_version(self):
        determined_browser_version = self.get_browser_version_from_os()
        log(f"Get LATEST {self._name} version for {self._browser_type}")
        if determined_browser_version is not None and version.parse(determined_browser_version) >= version.parse("115"):
            url = "https://googlechromelabs.github.io/chrome-for-testing/latest-patch-versions-per-build.json"
            response = self._http_client.get(url)
            response_dict = json.loads(response.text)
            determined_browser_version = response_dict.get("builds").get(determined_browser_version).get("version")
            return determined_browser_version
        # Remove the build version (the last segment) from determined_browser_version for version < 113
>       determined_browser_version = ".".join(determined_browser_version.split(".")[:3])
E       AttributeError: 'NoneType' object has no attribute 'split'
/usr/lib/python3.11/site-packages/webdriver_manager/drivers/chrome.py:64: AttributeError```

Here is my dockerfile.

FROM gitlab/gitlab-runner:latest

#Update and install dependencies
RUN apt-get update -y && apt-get install -y \
    python3 \
    python3-pip \
    pip3 install pytest \
    pip3 install selenium \
    pip3 install --upgrade webdriver_manager


#Use an official Python runtime as a parent image
FROM python:3.10.12-slim

#Set the working directory in the container
WORKDIR /app

#Copy the current directory contents into the container at /app
COPY . /app

#Install build tools and any needed packages specified in requirements.txt
RUN apt-get update && \
    apt-get install -y build-essential

#Update and install dependencies
RUN apt-get update && \
    apt-get install -y \
    curl \
    unzip \
    build-essential

ENV CHROMEDRIVER_VERSION="95.0.4638.54"

#Install Chrome WebDriver
RUN mkdir -p /opt/chromedriver-$CHROMEDRIVER_VERSION && \
    curl -sS -o /tmp/chromedriver_linux64.zip http://chromedriver.storage.googleapis.com/$CHROMEDRIVER_VERSION/chromedriver_linux64.zip && \
    unzip -qq /tmp/chromedriver_linux64.zip -d /opt/chromedriver-$CHROMEDRIVER_VERSION && \
    rm /tmp/chromedriver_linux64.zip && \
    chmod +x /opt/chromedriver-$CHROMEDRIVER_VERSION/chromedriver && \
    ln -fs /opt/chromedriver-$CHROMEDRIVER_VERSION/chromedriver /usr/local/bin/chromedriver

#Install Google Chrome
RUN curl -sS -o - https://dl-ssl.google.com/linux/linux_signing_key.pub | apt-key add - && \
    echo "deb http://dl.google.com/linux/chrome/deb/ stable main" >> /etc/apt/sources.list.d/google-chrome.list && \
    apt-get update && \
    apt-get -yqq install google-chrome-stable && \
    rm -rf /var/lib/apt/lists/*


#Install Python 3.10.12 and create a virtual environment
RUN python3.10 -m venv venv

#Activate the virtual environment
ENV PATH="/app/venv/bin:$PATH"

#Install additional packages
RUN apt-get update && \
    apt-get install -y python3-flask && \
    apt-get install -y python3-pip && \ 
    pip install flask_login firebase_admin pyrebase && \
    pip uninstall -y requests && \
    pip uninstall -y google-cloud-storage && \
    pip uninstall -y google-api-core && \
    pip uninstall -y cachecontrol && \
    pip install pyrebase==3.0.27 && \
    pip install requests==2.11.1 && \
    pip install google-cloud-storage==1.37.1 && \
    pip install google-api-core==1.22.1 && \
    pip install cachecontrol==0.12.6 && \
    pip install google-cloud-firestore && \
    pip install urllib3 && \
    pip install urllib3==1.26.15 requests-toolbelt==0.10.1 && \
    pip install pycryptodome==3.10.1
    
#Make port 80 available
EXPOSE 80

#Define environment variable
ENV NAME cdt

#Run app.py when the container launches
CMD ["flask", "run", "--host=0.0.0.0", "--port=80"]

I think the problem is with the ChromeDriver installation, but I have no idea how else to do it.

Does anyone have a solution? Thank you !

0

There are 0 best solutions below