How to run Selenium Grid 4 network in GitLab CI? - selenium

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

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

Build failure while trying to use docker-compose build step

I am trying to execute my selenium test on Jenkins node (Ubuntu) which has docker already installed. I added docker-compose build step plugin to my Jenkins project. When i try to build the project, I am getting an error in console -
$ docker-compose -f /home/jenkins/workspace/OM/TestWDM/docker-compose.yml up -d
Build step 'Docker Compose Build Step' changed build result to FAILURE
I am able to execute the project successfully on my local machine. I do have docker-compose.yml file in the root directory. I tried docker ps -a command just to see if it's partially . working, but it's not.
docker-compose file:
version: "3"
services:
selenium-hub:
restart: always
image: selenium/hub:latest
ports:
- "4444:4444"
#selenium-chrome
selenium-chrome:
restart: always
image: selenium/node-chrome-debug
stdin_open: true
links:
- selenium-hub:hub
#selenium-firefox
selenium-firefox:
restart: always
image: selenium/node-firefox-debug
links:
- selenium-hub:hub
chrome:
image: selenium/node-chrome
depends_on:
- selenium-hub
environment:
- HUB_PORT_4444_TCP_ADDR=selenium-hub
- HUB_PORT_4444_TCP_PORT=4444
firefox:
image: selenium/node-firefox
depends_on:
- selenium-hub
environment:
- HUB_PORT_4444_TCP_ADDR=selenium-hub
- HUB_PORT_4444_TCP_PORT=4444
The reason I am trying to use docker here because I was facing an issue with chrome binary not found without it. My expectation over here was to have my test run successfully on Jenkins node.

Docker-The scale command is deprecated. Use the up command with the --scale flag instead

i want to launch 5 chrome and 5 firefox in docker container. I am using the below command but its giving the following error
The scale command is deprecated. Use the up command with the --scale flag instead.
ERROR: No such service: nodechrome
docker-compose scale nodechrome=5 nodefirefox=5
My docker-compose.yml file
version: '2'
services:
firefoxnode:
image: selenium/node-firefox-debug
volumes:
- /dev/shm:/dev/shm
depends_on:
- hub
environment:
HUB_HOST: hub
ports:
- "32772:5900"
chromenode:
image: selenium/node-chrome-debug
volumes:
- /dev/shm:/dev/shm
depends_on:
- hub
environment:
HUB_HOST: hub
ports:
- "32773:5900"
hub:
image: selenium/hub
ports:
- "4444:4444"
From where these names came? nodechrome and nodefirefox
Correct your command instead:
from:
docker-compose scale nodechrome=5 nodefirefox=5
to:
docker-compose scale chromenode=5 firefoxnode=5
Try this command:
docker-compose up --scale chrome=5
Verify the Grid by visiting: http://localhost:4444/ui/index.html#/
Refer: Documentation

How to setup maxSession in selenium docker

docker run -d -p 4444:4444 --name selenium-hub selenium/hub
docker run -d --link selenium-hub:hub -v /dev/shm:/dev/shm selenium/node-chrome
Ubuntu 16.04
after these 2 commands, I have successfully setup a selenium hub and a selenium node. however current maxSession of this node is set to 1, I need increase to 5. How can I do that?
Thanks.
I highly recommend you use a docker compose file.
Below is a simple example you can use.
Just change the MAX_SESSION and MAX_INSTANCES to what you need.
version: "3.1"
services:
hub:
image: selenium/hub
container_name: "hub"
ports:
- "4444:4444"
chrome:
image: selenium/node-chrome
volumes:
- /dev/shm:/dev/shm
shm_size: 2gb
depends_on:
- hub
environment:
- HUB_HOST=hub
- HUB_PORT=4444
- SCREEN_WIDTH=1920
- SCREEN_HEIGHT=1080
- NODE_MAX_INSTANCES=2
- NODE_MAX_SESSION=2

Selenium/hub can't recognize selenium/node-chrome-debug while in network_mode = "host"

I use Docker to start my Selenium grid in network_mode = "host" so that I can access my local development site.
Both the hub and node started, but when I check http://localhost:4444/grid/console, there's no node. I wonder if there is any way to solve it.
More info:
Image(s): hub, node-chrome-debug
Docker-Selenium Image Version(s): 3.14.0-arsenic
Docker Version: 18.06.0-ce, build 0ffa825
OS: Ubuntu 18.04
My docker-compose file:
version: "3"
services:
selenium-hub:
image: selenium/hub:latest
container_name: selenium-hub
network_mode: "host"
ports:
- "4444:4444"
chrome:
image: selenium/node-chrome-debug:latest
depends_on:
- selenium-hub
network_mode: "host"
ports:
- "5900:5900"
environment:
- HUB_HOST=selenium-hub
- HUB_PORT=4444
Diego Molina figured out why it did not work.
It is because Chrome container will try to register to the name selenium-hub, which does not exist in the "host" network, and would only exist in the network that docker-compose creates.
To make it works, just set HUB_HOST=localhost.

change selenium hub default port docker-compose

I have multiple jenkins jobs, each attempt to start a selenium standalone container, although when multiple jobs run in parallel in the same jenkins slave, it throws the following error:
0.0.0.0:4444 failed: port is already allocated
Which is fair enough because the default port for the selenium standalone is set to 4444. My question is how I override the selenium standalone port? I thought it was related to ports: ... but clearly not... any ideas?
Below is my docker-compose file:
maven:
build: "."
links:
- "selenium:selenium"
working_dir: /code/
volumes:
- ".:/code"
entrypoint:
- mvn
- verify
- -Dwebdriver.remote.url=http://selenium:4444/wd/hub
selenium:
image: selenium/standalone-chrome:latest
ports:
- "4444:4444"
I have for instance another docker-compose file, slightly different, for instance:
maven_2:
build: "."
links:
- "selenium_2:selenium_2"
working_dir: /code/
volumes:
- ".:/code"
entrypoint:
- mvn
- verify
- -Dsome.additional.tags=test
- -Dwebdriver.remote.url=http://selenium_2:4444/wd/hub
selenium_2:
image: selenium/standalone-chrome:latest
ports:
- "4444:4444"