Home > Mobile >  Unable to run selenium pytest on gitlab-ci
Unable to run selenium pytest on gitlab-ci

Time:04-01

I have the following .gitlab-ci yaml with one test stage and 2 services the remote browser and the docker service.

image: python:3.8
stages:
  - test

variables:
  BROWSER: chrome
  DOCKER_HOST: tcp://docker:2375
  DOCKER_DRIVER: overlay2


.job_template:
  before_script:
    - apt update
    - apt install -y curl docker.io 
    - docker run -d -p 80:80 nginx
    - pip3 install -r requirements.txt
  script:
    - curl http://docker
    - pytest -vs --browser=$BROWSER --local='false'

e2e:remote:chrome:
  extends: .job_template
  services:
    - selenium/standalone-chrome
    - docker:dind
  only:
    variables:
      - $BROWSER == 'chrome'

On the docker service I am running an nginx server at port 80, curl http://docker command is returning the nginx welcome's page source code, but when it comes to pytest command with the following test

from pytest import raises
from selenium.common.exceptions import NoSuchElementException


def test_query_window_is_visible(remote_browser):
    remote_browser.get('http://docker')
    #query_window = remote_browser.find_element_by_name('q')
    print(remote_browser.page_source.encode("utf-8"))
    assert query_window.is_displayed()

I am taking the following error:

response = {'status': 500, 'value': '{"value":{"error":"unknown error","message":"unknown error: net::ERR_NAME_NOT_RESOLVED\n (...\n#22 0x55df1954a60c \u003Cunknown>\n#23 0x55df19563c6d \u003Cunknown>\n#24 0x7fd4ebddb609 \u003Cunknown>\n"}}'}

CodePudding user response:

Short answer

You should add this to the job variables FF_NETWORK_PER_BUILD: "true".

variables:
  FF_NETWORK_PER_BUILD: "true" # Define it here if you have many e2e test jobs.  

...

e2e:remote:chrome:
  extends: .job_template
  variables:
    FF_NETWORK_PER_BUILD: 1 # you can use "true" or 1 to enable it
  services:
    - selenium/standalone-chrome
    - docker:dind
  only:
    variables:
      - $BROWSER == 'chrome'

Long answer

By default GitLab runner (Docker executors) uses docker network links to connect the services with the container running the job, creating a one-to-many relationship from the job container (called build by default). You can learn more about this in the services page in the official docs

This means that containers defined in services cannot access each other by default.

To enable services to connect with each other and with the build job you should enable the FF_NETWORK_PER_BUILD feature flag. Having this feature flag enabled GitLab runner creates a docker bridge network for the job and uses it instead of links to connects the containers defined in that job (main build container and the services containers.)

You can learn more here and here.

  • Related