Gitlab CI-Django功能测试-splinter

时间:2018-12-01 21:48:18

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

我想在一个带有Django框架的我的项目上的github上运行一些自动化测试。因此,我正在使用Django功能测试。在我的本地PC上执行测试可以正常工作时,我的管道始终无法通过这些测试。

我以为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

要在chromelab上使用chromedriver,我需要修改什么?

1 个答案:

答案 0 :(得分:2)

我不认为google-chrome软件包可以完成您认为的工作。 Looking at its source code,它是围绕MacOS上的Chrome浏览器的一组AppleScript命令的Python包装器,并且肯定不会在Linux上安装该浏览器。

作为参考,这是我们与Django和Selenium一起使用的(剥离的)Gitlab CI管道,用于在Firefox和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

一些注意事项:

  • 仔细研究一下工作,除了最后两个步骤以外,所有步骤都是准备步骤;将它们移动到自定义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)