Exercise : 3 Local Selenium Grid Hub and Node Using Docker Compose - Raneesh02/ref_workshop_2 GitHub Wiki
This document explains the Docker Compose configuration for setting up Selenium with Chrome. The provided Docker Compose file defines services for Selenium Grid with Chrome as the browser for running tests.
Step 1 : Create a Docker Compose File with name : docker-compose-v3.yml
version: "3"
services:
chrome:
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:4.7.2-20221219
container_name: selenium-hub
ports:
- "4442:4442"
- "4443:4443"
- "4444:4444"
Note : For Mac, change chrome Image to
seleniarm/node-chromium:latest
and hub image toseleniarm/hub
Configuration Breakdown
-
version: "3" -> Specifies the version of the Docker Compose file format. Version 3 is compatible with Docker Compose 1.13.0 and later.
-
services -> Defines the services that make up your application. In this file, there are two services: chrome and selenium-hub.
-
chrome image: selenium/node-chrome:latest -> Uses the selenium/node-chrome Docker image with the latest tag to run the Chrome browser within the Selenium Grid. This image includes a pre-configured Chrome node for Selenium.
-
shm_size: 2gb: Sets the shared memory size to 2 GB. This is crucial for browser performance, particularly for running tests with Chrome, as it needs more shared memory for proper functioning.
-
depends_on: selenium-hub -> Specifies that the chrome service depends on the selenium-hub service. Docker Compose will start selenium-hub before starting chrome.
-
environment:
- SE_EVENT_BUS_HOST=selenium-hub: Configures the Chrome node to communicate with the Selenium Hub. The SE_EVENT_BUS_HOST environment variable specifies the host of the Selenium Hub.
- SE_EVENT_BUS_PUBLISH_PORT=4442: Sets the port for publishing events to the Selenium Hub.
- SE_EVENT_BUS_SUBSCRIBE_PORT=4443: Sets the port for subscribing to events from the Selenium Hub. selenium-hub
- image: selenium/hub:4.7.2-20221219: Uses the selenium/hub Docker image with the specified version. This image runs the Selenium Hub, which acts as the central point for distributing tests to different browser nodes.
- container_name: selenium-hub: Assigns the container a custom name (selenium-hub) for easier identification.
- ports:
- "4442:4442": Maps port 4442 on the host to port 4442 on the container for the event bus publish port.
- "4443:4443": Maps port 4443 on the host to port 4443 on the container for the event bus subscribe port.
- "4444:4444": Maps port 4444 on the host to port 4444 on the container for the Selenium Grid console and communication with nodes.
Syntax of a docker compose yml : https://docs.docker.com/reference/compose-file/services/
Step 2 : Run all the services using command line
Copy above code and create a new filer docker in the root and cd into it
docker compose -f docker-compose-v3.yml up -d
Step 3 : Scaling the Chrome Service
To scale the number of Chrome nodes, you can use the docker-compose up command with the scale option. This allows you to specify the number of instances you want to run. For example, to run 3 instances of the Chrome node, you would use:
docker compose -f docker-compose-v3.yml scale chrome=3
Also you could do it while starting the grid for first time
docker compose -f docker-compose-v3.yml up --scale chrome=3 -d
This command starts the number of specified Chrome nodes and connects them to the Selenium Hub, allowing for parallel test execution.
Step 4: Change girl URL value to localhost
Go to src/main/resources/configuration.properties and write localhost for gridurl value
selenium.gridurl=localhost
Step 5: Do the execution
Do the execution via command mvn test
Step 6: Tear Down
Make sure after all the testing you execute this command to tear down the containers
docker compose -f docker-compose-v3.yml down
Note : Double check in docker desktop . No containers should be running , otherwise they will continue to consume system resources