在 gitlab CI/CD 中使用 selenium [英] Using selenium inside gitlab CI/CD

查看:181
本文介绍了在 gitlab CI/CD 中使用 selenium的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经绝望地尝试为由 gitlab 托管的我的个人项目设置一个 pytest 管道 CI/CD.

I've desperetaly tried to set a pytest pipeline CI/CD for my personal projet hosted by gitlab.

我尝试用两个基本文件建立一个简单的项目:

I tried to set up a simple project with two basic files:

file test_core.py,为了简单起见,不包含任何其他依赖项:

file test_core.py, witout any other dependencies for the sake of simplicity:

# 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 自动测试:

File .gitlab-ci.yml, for CI/CD automatic tests:

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

在我的笔记本电脑上,pytest 命令可以 100% 正常工作.当我向 gitlab 推送提交时,我绝望地收到错误:

On my laptop, the pytestcommand works fine 100% of time. When I push a commit to gitlab, I deseperately get errors:

>       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

I've created a simple project: https://gitlab.com/OlivierLuG/selenium_firefox that reproduce this example. The failed pipeline can be directely found here : https://gitlab.com/OlivierLuG/selenium_firefox/-/pipelines/225711127

有人知道如何解决这个错误吗?

Does anybody have a clue how to fix this error ?

推荐答案

我终于设法使用下面的 .gitlab-ci.yml 文件在绿色上 ping gitlab CI.

I've finally managed to ping gitlab CI on green with the below .gitlab-ci.yml file.

请注意,我不是 yaml 语言的粉丝.为了使文件更短,我使用了一个名为 install_firefox_geckodriver 的共享代码块.然后,我用 python 3.7 和 3.8 配置了 2 个作业,它们调用了这个块.使这种测试起作用的关键是:_ 在无头模式下运行(这对我来说已经是这种情况)_ 用命令行安装 firefox 和 geckodriver_ 安装火狐依赖_ 使用 gitlab selenium 服务

Note that I'm not a fan of yaml language. To make the file shorter, I've used a shared block of code, named install_firefox_geckodriver. Then, I've configured 2 jobs with python 3.7 and 3.8, that call this block. The keys to make this kind of test to work are: _ run in headless mode (this was already the case for me) _ install firefox and geckodriver with command lines _ install firefox dependencies _ use gitlab selenium service

这是我的 yaml 文件.可以在这里找到成功的管道:https://gitlab.com/OlivierLuG/selenium_firefox/-/pipelines/225756742

Here is my yaml file. The sucessful pipeline can be found here : https://gitlab.com/OlivierLuG/selenium_firefox/-/pipelines/225756742

stages:
  - tests

.install_firefox_geckodriver: &install_firefox_geckodriver
  - apt-get update && apt-get upgrade --assume-yes
  - apt-get install gconf-service libasound2 libatk1.0-0 libc6 libcairo2 libcups2 libdbus-1-3 libexpat1 libfontconfig1 libgcc1 libgconf-2-4 libgdk-pixbuf2.0-0 libglib2.0-0 libgtk-3-0 libnspr4 libpango-1.0-0 libpangocairo-1.0-0 libstdc++6 libx11-6 libx11-xcb1 libxcb1 libxcomposite1 libxcursor1 libxdamage1 libxext6 libxfixes3 libxi6 libxrandr2 libxrender1 libxss1 libxtst6 ca-certificates fonts-liberation libappindicator1 libnss3 lsb-release xdg-utils --assume-yes
  - wget -nv -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 -nv -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/


pytest:python3.7:
  image: python:3.7
  stage: tests
  services:
    - selenium/standalone-firefox:latest
  script:
    - *install_firefox_geckodriver
    - pip install selenium pytest
    - pytest

pytest:python3.8:
  image: python:3.8
  stage: tests
  services:
    - selenium/standalone-firefox:latest
  script:
    - *install_firefox_geckodriver
    - pip install selenium pytest
    - pytest

这篇关于在 gitlab CI/CD 中使用 selenium的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆