How to setup a selenium grid locally

Mar 7, 2024 | by Ralph Van Der Horst

How to setup a selenium grid locally

How to setup a selenium grid 4 localhost (Manually or Docker)

In windows

Download the Selenium Server jar: Ensure you have the correct version of the Selenium Server jar file (selenium-server-4.17.0.jar) downloaded to your machine.

  1. Open Command Prompts: You need to open two command prompt windows (cmd) on your Windows machine for the hub and the node.
  2. Start the Hub:
  • In the first command prompt window, navigate to the directory where you downloaded selenium-server-4.17.0.jar.
  • Execute the following command to start the Selenium Grid Hub: java -jar selenium-server-4.17.0.jar hub

java -jar selenium-server-4.17.0.jar hub

selenium grid session learautomated testing

  1. Start the Node and Register to the Hub:
  • In the second command prompt window, navigate to the directory where selenium-server-4.17.0.jar is located.
  • Run the following command to start a node and register it with the hub:

java -jar selenium-server-4.17.0.jar node –selenium-manager true

java -jar selenium-server-4.17.0.jar node –selenium-manager true

  1. Verify the Setup:
  • Open a web browser and go to http://localhost:4444/ui to access the Grid’s console. You should see both the hub and the node listed, indicating they are working and registered correctly.
  1. Test the Grid:
  • Use the curl command to create a new session and open a browser. This step is done to verify that commands can be sent to the Grid and executed properly. Run the following command from another command prompt or terminal window:
curl --location 'http://localhost:4444/session' \
--header 'Content-Type: application/json; charset=utf-8' \
--data '{
 "capabilities": {
 "firstMatch": [
 {
 "goog:chromeOptions": {
 "args": [],
 "extensions": []
 },
 "browserName": "chrome"
 }
 ]
 }
}'

}

Troubleshooting

  • Ensure Java is Installed: Verify that Java is installed on your machine by running java -version in the command prompt.
  • Check Port Availability: The default port for the Selenium Grid Hub is 4444. Ensure that this port is not in use by another application.
  • Firewall and Antivirus: Sometimes, firewall or antivirus settings may block the communication between the hub and node. Ensure these settings allow traffic on port 4444 and any other ports used by Selenium. Setup Selenium grid in Docker and Docker Compose(windows linux etc)

Setting up Selenium Grid using Docker Compose is a more streamlined and modern approach, especially when you require a scalable and easily configurable environment. Docker Compose allows you to define and run multi-container Docker applications, making it simpler to deploy the Selenium Hub and nodes with just a few commands.

Prerequisites

  • Docker: Ensure Docker and Docker Compose are installed on your machine. You can download them from the Docker website and follow the installation instructions for your operating system.

Creating a Docker Compose File

  1. Create a docker-compose.yml File: This file defines the services, networks, and volumes for your Docker application. For Selenium Grid, you will define services for the Selenium Hub and the nodes (e.g., Chrome, Firefox).
  2. Define the Services: In the docker-compose.yml file, you will specify the Selenium Hub and node services. Here is an example configuratio but all configurations you can find on the main repository of selenium https://github.com/SeleniumHQ/docker-selenium

Creating a Docker Compose File

  1. Create a docker-compose.yml File: This file defines the services, networks, and volumes for your Docker application. For Selenium Grid, you will define services for the Selenium Hub and the nodes (e.g., Chrome, Firefox).
  2. Define the Services: In the docker-compose.yml file, you will specify the Selenium Hub and node services. Here is an example configuration:
version: '3'
services:
 selenium-hub:
 image: selenium/hub:4.17.0
 container_name: selenium-hub
 ports:
 - "4444:4444"

 chrome-node:
 image: selenium/node-chrome:4.17.0
 depends_on:
 - selenium-hub
 environment:
 - SE_EVENT_BUS_HOST=selenium-hub
 - SE_EVENT_BUS_PUBLISH_PORT=4442
 - SE_EVENT_BUS_SUBSCRIBE_PORT=4443
 volumes:
 - /dev/shm:/dev/shm

 firefox-node:
 image: selenium/node-firefox:4.17.0
 depends_on:
 - selenium-hub
 environment:
 - SE_EVENT_BUS_HOST=selenium-hub
 - SE_EVENT_BUS_PUBLISH_PORT=4442
 - SE_EVENT_BUS_SUBSCRIBE_PORT=4443
 volumes:
 - /dev/shm:/dev/shm

This configuration sets up a Selenium Grid with one hub and two nodes (one for Chrome and one for Firefox). The depends_on option specifies that the nodes depend on the hub being started first. The volumes option for /dev/shm:/dev/shm is recommended to avoid out-of-memory errors in browsers.

Running Docker Compose

  • Start the Grid: Navigate to the directory containing your docker-compose.yml file and run the following command to start all services defined in the configuration: docker-compose up -d

docker-compose up -d

  • Verify the Setup: Open a web browser and go to http://localhost:4444/ui to access the Selenium Grid console. You should see the hub and registered nodes.
  • Scaling Nodes: If you need more instances of Chrome or Firefox nodes, you can scale them using Docker Compose: docker-compose up -d –scale chrome-node=2 –scale firefox-node=2

Note: manually scaling(via normal docker syntax) is done like this

docker run -d –link selenium-hub:hub selenium/node-chrome:4.17.0

docker run -d –link selenium-hub:hub selenium/node-chrome:4.17.0

docker-compose up -d –scale chrome-node=2 –scale firefox-node=

This command scales the Chrome and Firefox nodes to have 2 instances each.

Stopping and Removing the Containers

When you’re done with the Selenium Grid, you can stop and remove the containers using Docker Compose:

docker-compose down

docker-compose down

This command stops and removes the containers, networks, and volumes defined in your docker-compose.yml file.

Using Docker Compose for Selenium Grid simplifies the setup and management process, allowing for easy scaling and configuration adjustments.

by Ralph Van Der Horst

arrow right
back to blog

share this article

Relevant articles

How to serve an Allure Report on GitHub Pages A Step by Step Guide

How to serve an Allure Report on GitHub Pages A Step by Step Guide

How to Run Multiple Tests at Once in WebdriverIO A Simple Guide

How to Run Multiple Tests at Once in WebdriverIO A Simple Guide

Selenium Fargate at scale grid 4

Feb 26, 2024

Selenium Fargate at scale grid 4