How to run Selenium Grid 4 network in GitLab CI?

Viewed 306

I want to run selenum grid containers in GitLab. They would be: one selenium hub and two chrome node containers connected by a shared network. I know how to deploy those using a yaml file, but I have no idea how to start those containers in gitlab.

Ideally, the 3 cotainers would be started in pipleine, and then I would execute a script that would run some Selenium tests using the grid.

There's my yml file that would start selenium grid locally:

version: '3.7'

services:    
  selenium-1:
    image: selenium/node-chrome:latest
    shm_size: '2gb'
    depends_on:
      - selenium-hub
    environment:
      - SE_EVENT_BUS_HOST=selenium-hub
      - SE_EVENT_BUS_PUBLISH_PORT=4442
      - SE_EVENT_BUS_SUBSCRIBE_PORT=4443

  selenium-2:
    image: selenium/node-chrome:latest
    shm_size: '2gb'
    depends_on:
      - selenium-hub
    environment:
      - SE_EVENT_BUS_HOST=selenium-hub
      - SE_EVENT_BUS_PUBLISH_PORT=4442
      - SE_EVENT_BUS_SUBSCRIBE_PORT=4443

  selenium-hub:
    image: selenium/hub:latest
    expose:
      - 4444

There's the script that would start the selenium tests:

#!/usr/bin/env bash
set -e


python -m pytest

The .gitlab-ci.yml I have so far:

stages:
  - e2e

e2e:
  stage: e2e
  interruptible: false
  services:
    - selenium/hub:latest
    - selenium/node-chrome:latest
  script:
    - bash run_test_page.sh
1 Answers

i basically use dind to run the compose file.
in the snippets below, i am trying to mention the essential bits, you can see a full project example here
also, i am using java and maven, not much change should be needed for python in your case


docker-compose used by the gitlab pipeline -

version: "3"

services:
  selenium-chrome-01: &selenium-chrome
    image: selenium/node-chrome:4.3.0-20220726
    shm_size: 2gb
    depends_on:
      - selenium-hub
    environment:
      SE_EVENT_BUS_HOST: ${SELENIUM_SERVER_NAME}
      SE_EVENT_BUS_PUBLISH_PORT: 4442
      SE_EVENT_BUS_SUBSCRIBE_PORT: 4443

  selenium-chrome-02:
    <<: *selenium-chrome

  selenium-hub:
    image: selenium/hub:4.3.0-20220726
    shm_size: 2gb
    container_name: ${SELENIUM_SERVER_NAME}

  ping:
    image: alpine/curl

  tests:
    image: openjdk:11
    working_dir: /app
    volumes:
      - ${CI_PROJECT_DIR}:/app
    environment:
      ENVIRONMENT: remote
      SELENIUM_SERVER_URL: ${SELENIUM_SERVER_URL}

the compose file has 4 containers - 2 worker nodes, 1 hub, 1 to check the status of the hub, and 1 to run the tests


gitlab pipeline file -

stages:
  - test

variables:
  SELENIUM_SERVER_NAME: selenium
  SELENIUM_SERVER_URL: http://${SELENIUM_SERVER_NAME}:4444
  DOCKER_HOST: tcp://docker:2375

services:
  - docker:20.10.16-dind

test:
  stage: test
  image: docker/compose
  before_script:
    - docker-compose up -d selenium-hub selenium-chrome-01 selenium-chrome-02
    - sleep 10
    - docker-compose run ping curl ${SELENIUM_SERVER_URL}/status
  script:
    - docker-compose run tests ./mvnw test -Dtest=TestClass

i think if you can configure shm_size on the gitlab runner's config, you can run the selenium grid as services which i feel is a better solution

Related