简体   繁体   English

Gitlab CI - Django功能测试 - 分裂

[英]Gitlab CI - Django functional tests - splinter

I want to run some automation tests on github on a project of mine with a Django framework. 我想在github上使用Django框架在我的项目上运行一些自动化测试。 Therefore I am using a Django functional test. 因此我正在使用Django功能测试。 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: 我假设,chromedriver工作不正常,经过对互联网的一些研究,我发现,我需要安装chrome作为浏览器,所以我修改了我的requirements.txt for pip,如下所示:

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 .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 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: 有了这个,已经安装了一个chrome浏览器,但是现在我收到了这个错误:

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? 为了将chromedriver用于gitlab,我需要修改什么?

I don't think the google-chrome package does what you think it does. 我不认为google-chrome软件包可以满足您的需求。 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. 查看其源代码 ,它是MacOS上Chrome浏览器周围的一组AppleScript命令的Python包装器,肯定不会在Linux上安装浏览器。

For reference, here is the (stripped) Gitlab CI pipeline we're using with Django and Selenium to run tests with Firefox and Chrome: 作为参考,这里是我们与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

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. 将它们移动到自定义Docker镜像将减少测试运行时间和管道中的样板量。
  • here, xvfb is used to run the browser in a virtual display; 这里, xvfb用于在虚拟显示器中运行浏览器; the modern browsers are able to run in headless mode (add --headless to chromedriver options), making the virtual display unnecessary. 现代浏览器能够以无头模式运行(添加 - --headless控制器选项),使虚拟显示不必要。 If you don't need to support old browser versions, you can omit the xvfb installation and xvfb-run usage. 如果您不需要支持旧的浏览器版本,则可以省略xvfb安装和xvfb-run用法。
  • The tests will run as root in container; 测试将在容器中以root身份运行; 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: 如果你面对这个,你需要将额外的标志--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) 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM