[Django]-Gitlab CI – Django functional tests – splinter

3πŸ‘

βœ…

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

Leave a comment