gpt4 book ai didi

python - Gitlab CI - Django 功能测试 - split

转载 作者:行者123 更新时间:2023-11-28 17:02:20 24 4
gpt4 key购买 nike

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

我假设 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

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

测试函数.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

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

最佳答案

我不认为 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 模式运行(将 --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)

关于python - Gitlab CI - Django 功能测试 - split ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53575348/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com