How To Set Up Docker Selenium GRID
Tags: docker, grid, selenium, dockercompose
Published: 2024-06-07 13:34:53

## What is a Selenium GRID?
Selenium GRID is one of the 3 components of Selenium : Selenium WebDriver, GRID and IDE. The architecture consists of a Hub and several nodes. Through our test script, we will connect to the Selenium Hub url and it will run tests on the nodes.

_Parallel execution_ : A grid helps in execution of test script on a virtual machine(s) instead of local machine. Suppose there are few hundred testcases, which will take couple of hours to complete execution and it would totally seize your local machine for that time duration.

_Scaling up browser containers_ : A grid enables execution of test script on scaled up browser instances. If I want to run my tests only on Chrome browser and I have hundreds of testcases, I can scale up Chrome instances and run them in parallel which will significantly reduce execution time. After that I can scale it down as per my requirement.

_Cross browser testing_ : On my system, I don't have a specific browser and I don't want to set it up. I can always configure the grid to have those browsers and run my tests on them!

## Standalone Selenium GRID :

This is something Selenium itself provides. We need to download and install the Selenium Server (preferably latest version and Java version >= 11) on our machine.

Navigate to [Selenium Server Download](https://www.selenium.dev/downloads/) page and we can see all the language compatible zip files : Or directly click on the selenium-server-4.21.0.jar file.


![Selenium Server grid official page](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/amnnz1ui5w8hvq4tga4s.png)

We will see the complete installation and execution of test scripts using Selenium server in another article.

## Docker Selenium GRID :

Selenium provides Docker images to run on our machine and all the grid installation and different browser setup etc. will be done automatically, we don't need to do anything! (that means we don't need to configure the grid, browser instances etc. step by step- like in case of Selenium Server.)

## What is a Docker Compose file?

According to official Docker documentations :

> _Docker Compose is a tool for defining and running multi-container applications. It is the key to unlocking a streamlined and efficient development and deployment experience._
> _Compose simplifies the control of your entire application stack, making it easy to manage services, networks, and volumes in a single, comprehensible YAML configuration file. Then, with a single command, you create and start all the services from your configuration file._

It is a .yml/.yaml, where all the selenium grid configurations are defined. Selenium provides these files for all types of releases : [SeleniumHQ/docker-selenium](https://github.com/SeleniumHQ/docker-selenium/blob/trunk/README.md)

## How to Run a Docker Compose file?

**_Start Docker_**: Once we have installed Docker on our system, it is also know as the Docker Desktop, just open it. There. It has started.

**_Get a docker compose file and store it in a folder_** : Here is a sample docker compose file :

```
# To execute this docker-compose yml file use `docker-compose -f docker-compose-v3-beta-channel.yml up`
# Add the `-d` flag at the end for detached execution
# To stop the execution, hit Ctrl+C, and then `docker-compose -f docker-compose-v3-beta-channel.yml down`
version: "3"
services:
  chrome:
    image: selenium/node-chrome:4.20.0-20240505
    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

  edge:
    image: selenium/node-edge:4.20.0-20240505
    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

  firefox:
    image: selenium/node-firefox:4.20.0-20240505
    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.20.0-20240505
    container_name: selenium-hub
    ports:
      - "4442:4442"
      - "4443:4443"
      - "4444:4444"
```
_**Run the docker compose file**_: Go to docker compose folder and open command prompt.

![docker-compose file on local system](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xbkpz8e4mxr9jjqikn4t.png)

On the command prompt, run the command _**docker compose up -d**_ . (-d means in detached mode. Your command prompt will be available to you for running further commands)

![Running docker compose up -d command](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/p73uhybogp6tlscan9fk.png)

![Containers created from docker compose file](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9bwovyfxv3ct0zw1zwwa.png)

_Pulling_ means the images are being downloaded and pulled from Docker image hub.

_**Open Selenium grid on browser **_: After all the installations are complete, open any browser and enter the url : localhost:4444/ui . The grid runs on the port 4444 by default, as defined in the docker compose file.

![Selenium Docker GRID on localhost](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gwg8q1as5nntqo42o9i0.png)

"localhost", because it is not hosted on any cloud or other remote machines. It is running on local machine.

That's it! The Selenium GRID is up and running. We just need to configure our test script, so it runs the tests on remote machine.

Happy learning!