3

I want to run some automation tests on github on a project of mine with a Django framework. Therefore I am using a Django functional test. While executing the test on my local pc works fine, my pipeline is always failing with those tests.

I assumed, that chromedriver wasn't working correctly and after some research on the internet, I found out, that I need to install chrome as browser, so I modified my requirements.txt for pip like this:

applescript==2018.11.19
astroid==2.1.0
autopep8==1.4.3
chromedriver==2.24.1
decorator==4.3.0
detect==2018.11.19
Django==2.1.3
flake8==3.6.0
google-chrome==2018.11.19
google-chrome-cli==2018.11.19
isort==4.3.4
lazy-object-proxy==1.3.1
mccabe==0.6.1
only==2018.11.20
psutil==5.4.8
public==2018.11.20
pycodestyle==2.4.0
pyflakes==2.0.0
pylint==2.2.1
pytz==2018.7
runcmd==2018.11.20
selenium==3.141.0
six==1.11.0
splinter==0.10.0
temp==2018.11.20
urllib3==1.24.1
wrapt==1.10.11

.gitlab-ci.yml

image: python:latest

before_script:
  - pip install virtualenv
  - virtualenv --python=python3 venv/
  - source venv/bin/activate
  - pip install -r requirements.txt
  - cd src/
  - python manage.py migrate

stages:
  - quality
  - tests

flake8:
  stage: quality
  script:
    - flake8 ./

test:
  stage: tests
  script:
    - python manage.py test

test_functional.py

def setUp(self):

        # LINUX x64
        executable_path = {'executable_path': settings.CHROMEDRIVER_PATH_LINUX64}

        # chrome
        self.browser_chrome = Browser('chrome', **executable_path)
        [..]

With this, a chrome browser has been installed, but now I get this error:

selenium.common.exceptions.WebDriverException: 
Message: Service /builds/mitfahrzentrale/mitfahrzentrale/venv/chromedriver unexpectedly exited. 
Status code was: 127

What do I need to modify in order to use chromedriver for gitlab?

dnsiv
  • 520
  • 4
  • 20

1 Answers1

2

I don't think the google-chrome package does what you think it does. Looking at its source code, it's a Python wrapper for a set of AppleScript commands around the Chrome browser on MacOS and will certainly not install the browser on Linux.

For reference, here is the (stripped) Gitlab CI pipeline we're using with Django and Selenium to run tests with Firefox and Chrome:

stages:
    - test

.test:
    coverage: '/TOTAL.*\s+(\d+%)$/'

test-linux_x86_64:
    extends: .test
    image: python:3.7.1-stretch
    stage: test
    tags:
        - linux_x86_64
    script:
        - apt -qq update
        - DEBIAN_FRONTEND=noninteractive apt -qq -y install xvfb firefox-esr chromium chromedriver
        # download geckodriver as no distro offers a package
        - apt install -qq -y jq  # I don't want to parse JSON with regexes
        - curl -s https://api.github.com/repos/mozilla/geckodriver/releases/latest | jq -r '.assets[].browser_download_url | select(contains("linux64"))' | xargs -n1 curl -sL | tar -xz -C /usr/local/bin
        - chmod +x /usr/local/bin/geckodriver
        # prepare Django installation
        - python -m venv /opt/testing
        # bundled pip and setuptools are outdated
        - /opt/testing/bin/pip install --quiet --upgrade pip setuptools
        - /opt/testing/bin/pip install --quiet -r requirements.txt
        - xvfb-run /opt/testing/bin/python manage.py test

Some notes:

  • taking a closer look at the job, all the steps besides the last two are preparation steps; moving them to a custom Docker image will reduce the test running time and the amount of boilerplate in your pipeline.
  • here, xvfb is used to run the browser in a virtual display; the modern browsers are able to run in headless mode (add --headless to chromedriver options), making the virtual display unnecessary. If you don't need to support old browser versions, you can omit the xvfb installation and xvfb-run usage.
  • The tests will run as root in container; at first, we got the error

    E       selenium.common.exceptions.WebDriverException: Message: unknown error: Chrome failed to start: exited abnormally
    E         (unknown error: DevToolsActivePort file doesn't exist)
    E         (The process started from chrome location /usr/bin/chromium is no longer running, so ChromeDriver is assuming that Chrome has crashed.)
    E         (Driver info: chromedriver=2.41,platform=Linux 4.15.10-300.fc27.x86_64 x86_64)
    

    If you face this, you need to pass the additional flag --no-sandbox to Chrome because it refuses to run as root without it:

    chrome_options = webdriver.ChromeOptions()
    chrome_options.add_argument('--no-sandbox')
    ds = DesiredCapabilities.CHROME
    ds['loggingPrefs'] = {'browser': 'ALL'}
    driver = webdriver.Chrome(desired_capabilities=ds, options=chrome_options)
    
hoefling
  • 59,418
  • 12
  • 147
  • 194