Using selenium inside gitlab CI/CD

2024/11/14 13:41:07

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, witout any other dependencies for the sake of simplicity:

# coding: utf-8
# !/usr/bin/python3import pytest
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.firefox.options import Optionsdef 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 = Truedriver = webdriver.Firefox(options=opts)driver.get("http://www.python.org")driver.close()

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

stages:- testspytest:python3.7:image: python:3.7stage: testsservices:- selenium/standalone-firefox:latestscript:#   - 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

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

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 ?

Answer

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

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

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.7stage: testsservices:- selenium/standalone-firefox:latestscript:- *install_firefox_geckodriver- pip install selenium pytest- pytestpytest:python3.8:image: python:3.8stage: testsservices:- selenium/standalone-firefox:latestscript:- *install_firefox_geckodriver- pip install selenium pytest- pytest
https://en.xdnf.cn/q/72095.html

Related Q&A

VSCode issue with Python versions and environments from Jupyter Notebooks

Issue: I am having issues with the environment and version of Python not matching the settings in VSCode, and causing issues with the packages I am trying to use in Jupyter notebooks. I am using a Wind…

weighted covariance matrix in numpy

I want to compute the covariance C of n measurements of p quantities, where each individual quantity measurement is given its own weight. That is, my weight array W has the same shape as my quantity ar…

How to implement biased random function?

My question is about random.choice function. As we know, when we run random.choice([apple,banana]), it will return either apple or banana with equal probabilities, what if I want to return biased resul…

Finding minimum value for each level of a multi-index dataframe

I have a DataFrame that looks like this:data a b 1 1 0.12 0.23 0.3 2 1 0.52 0.63 0.7and I want to find the minimum value for each level of a ignoring the b level, so as an output Im l…

Count occurrences of a list of substrings in a pyspark df column

I want to count the occurrences of list of substrings and create a column based on a column in the pyspark df which contains a long string.Input: ID History1 USA|UK|IND|DEN|MAL|SWE|AUS2…

What are screen units in tkinter?

I was reading the response in the link below and ran into screen units but I couldnt find what exactly was referred to by screen units in Jim Denneys response. I know they are not pixels. How do I use …

Python SUMPRODUCT of elements in nested list

I have two nested lists: a = [[1,2,3],[2,4,2]] b = [[5,5,5],[1,1,1]]I want to multiply and SUMPRODUCT each group of elements to get c = [[30],[8]]Which result from = [[1*5+2*5+3*5],[2*1,4*1,2*1]] Ive t…

Modifying viridis colormap (replacing some colors)

Ive searched around and found things that came close to working but nothing exactly suiting what I need. Basically, I really like the viridis colormap as a starting point. However, I would like to repl…

gtk minimum size

Is there an easy way to request that a GTK widget have a minimum width/height? I know you can do it on the column of a TreeView, but is it available for general widgets?

How do I convert a json file to a python class?

Consider this json file named h.json I want to convert this into a python dataclass. {"acc1":{"email":"[email protected]","password":"acc1","name&…