【发布时间】:2020-12-05 14:01:26
【问题描述】:
我已经绝望地尝试为我由 gitlab 托管的个人项目设置 pytest 管道 CI/CD。
我尝试建立一个包含两个基本文件的简单项目:
文件test_core.py,为了简单起见,没有任何其他依赖项:
# coding: utf-8
# !/usr/bin/python3
import pytest
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.firefox.options import Options
def test_basic_headless_selenium_example():
"""Test selenium installation by opening python website.
(inspired by https://selenium-python.readthedocs.io/getting-started.html)
"""
opts = Options()
opts.headless = True
driver = webdriver.Firefox(options=opts)
driver.get("http://www.python.org")
driver.close()
文件 .gitlab-ci.yml,用于 CI/CD 自动测试:
stages:
- tests
pytest:python3.7:
image: python:3.7
stage: tests
services:
- selenium/standalone-firefox:latest
script:
# - apt-get update && apt-get upgrade --assume-yes
- wget -O ~/FirefoxSetup.tar.bz2 "https://download.mozilla.org/?product=firefox-latest&os=linux64"
- tar xjf ~/FirefoxSetup.tar.bz2 -C /opt/
- ln -s /opt/firefox/firefox /usr/lib/firefox
- export PATH=$PATH:/opt/firefox/
- wget -O ~/geckodriver.tar.gz "https://github.com/mozilla/geckodriver/releases/download/v0.28.0/geckodriver-v0.28.0-linux64.tar.gz"
- tar -zxvf ~/geckodriver.tar.gz -C /opt/
- export PATH=$PATH:/opt/
- pip install selenium pytest
- pytest
在我的笔记本电脑上,pytestcommand 可以 100% 正常工作。
当我将提交推送到 gitlab 时,我会遇到错误:
> raise exception_class(message, screen, stacktrace)
E selenium.common.exceptions.WebDriverException: Message: Process unexpectedly closed with status 255
/usr/local/lib/python3.7/site-packages/selenium/webdriver/remote/errorhandler.py:242: WebDriverException
=========================== short test summary info ============================
FAILED test_selenium.py::test_basic_headless_selenium_example - selenium.comm...
============================== 1 failed in 1.29s ===============================
Cleaning up file based variables
00:01
ERROR: Job failed: exit code 1
我创建了一个简单的项目:https://gitlab.com/OlivierLuG/selenium_firefox,它重现了这个例子。失败的管道可以直接在这里找到: https://gitlab.com/OlivierLuG/selenium_firefox/-/pipelines/225711127
有人知道如何解决这个错误吗?
【问题讨论】:
-
要么安装
Xvfb并使用xvfb-run pytest,要么配置firefoxdriver以无头模式运行。 -
谢谢,pytest 已经处于无头模式。我已经设法让这个项目开始工作。我会发布一个答案。
标签: python selenium gitlab pytest gitlab-ci