【问题标题】:Gitlab CI - Django functional tests - splinterGitlab CI - Django 功能测试 - 分裂
【发布时间】:2019-05-03 15:51:23
【问题描述】:

我想在我的一个项目上使用 Django 框架在 github 上运行一些自动化测试。因此我正在使用 Django 功能测试。虽然在我的本地电脑上执行测试工作正常,但我的管道总是在这些测试中失败。

我认为 chromedriver 无法正常工作,在互联网上进行了一些研究后,我发现我需要安装 chrome 作为浏览器,所以我修改了 pip 的 requirements.txt,如下所示:

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)
        [..]

有了这个,chrome浏览器已经安装了,但现在我得到这个错误:

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

为了使用 chromedriver for gitlab,我需要修改什么?

【问题讨论】:

    标签: python django google-chrome gitlab selenium-chromedriver


    【解决方案1】:

    我不认为 google-chrome 包做你认为它做的事。 Looking at its source code,它是围绕 MacOS 上的 Chrome 浏览器的一组 AppleScript 命令的 Python 包装器,肯定不会在 Linux 上安装浏览器。

    作为参考,这里是我们使用 Django 和 Selenium 使用 Firefox 和 Chrome 运行测试的(剥离的)Gitlab CI 管道:

    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
    

    一些注意事项:

    • 仔细看作业,除了最后两步之外的所有步骤都是准备步骤;将它们移动到自定义 Docker 映像将减少测试运行时间和管道中的样板数量。
    • 这里,xvfb 用于在虚拟显示中运行浏览器;现代浏览器能够在无头模式下运行(将--headless 添加到 chromedriver 选项),从而不需要虚拟显示。如果不需要支持旧浏览器版本,可以省略xvfb的安装和xvfb-run的使用。
    • 测试将在容器中以root 运行;起初,我们得到了错误

      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)
      

      如果您遇到这种情况,您需要将附加标志 --no-sandbox 传递给 Chrome,因为没有它它拒绝以 root 运行:

      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)
      

    【讨论】:

    • 谢谢,这帮助很大!我也做了一些其他的修改,但这给了我正确的方向!
    • 很高兴能帮上忙!
    猜你喜欢
    • 2019-07-30
    • 2014-07-07
    • 1970-01-01
    • 1970-01-01
    • 2023-03-22
    • 2018-06-05
    • 2019-09-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多