我想在我的一个项目上用Django框架在github上运行一些自动化测试。因此,我使用的是Django功能测试。虽然在我的本地pc上执行测试工作正常,但我的管道总是在这些测试中失败。
我认为chromedriver不能正常工作,在互联网上进行了一些研究后,我发现,我需要安装chrome作为浏览器,所以我修改了我的requirements.txt作为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
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
我需要修改什么才能为gitlab使用chromedriver?
我不认为google-chrome
包能做到你认为的那样。从其源代码来看,它是一个Python包装器,用于MacOS上Chrome浏览器周围的一组AppleScript命令,当然不会在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)