Selenium Grid 4 is a smart proxy server that allows you to run tests in parallel across multiple machines, which is great for reducing the time for running your test suite. It supports distributing test commands to several instances of WebDriver, which can then run the tests in various browsers and environments.you can install it in various ways but one of the easiest ways is using Docker compose, which i only explain in this course. Docker Compose simplifies the deployment of Selenium Grid 4 by allowing you to define and start all the services (Selenium Grid Hub and Nodes) with a single command.

Prerequisites

  • Docker: Ensure you have Docker installed and running on your machine. Docker will be used to pull and run Selenium Grid 4 containers.
  • Docker Compose: Ensure Docker Compose is installed. It’s included with Docker Desktop for Mac and Windows.

Step 1: Create a Docker Compose File

The first step is to create a docker-compose.yml file that defines the Selenium Grid Hub and Node services. This file instructs Docker Compose on how to set up and link the containers.

  1. Create a new file named docker-compose.yml in your project directory or a directory of your choice.
  2. Add the following content to the file:
 version: '3'
services:
  selenium-hub:
    image: selenium/hub:4.0.0
    container_name: selenium-hub
    ports:
      - "4444:4444"

  chrome-node:
    image: selenium/node-chrome:4.0.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.0.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 Selenium Grid with one Hub and two Nodes (one Chrome and one Firefox). The Hub acts as the central point where test commands are received and distributed to the nodes.

Step 2: Start Selenium Grid

With the docker-compose.yml file in place, start the Grid by running the following command in


docker-compose up -d

The -d flag runs the containers in detached mode, allowing you to continue using the terminal.

Step 3: Verify the Installation

To ensure that Selenium Grid is running correctly:

  1. Open a web browser and navigate to http://localhost:4444. This should open the Selenium Grid console, showing the Hub and connected Nodes.
  2. Check the Docker containers by running docker ps. You should see the containers for the Selenium Hub and Nodes running.

Optional Steps

Scaling Nodes

You can easily scale the number of Chrome or Firefox nodes by using the docker-compose up –scale command. For example, to increase the number of Chrome nodes to 3:


docker-compose up -d --scale chrome-node=3

Custom Configuration

You can customize the Selenium Grid setup by modifying the docker-compose.yml file. For instance, you can add more node types, like Opera or Edge, by including additional services in the docker-compose.yml file using the respective Docker images (selenium/node-opera or selenium/node-edge).

Setting up Selenium Grid 4 can also be accomplished through manual installations or by leveraging cloud services like AWS Fargate. Each method suits different needs based on the scale, complexity, and resource availability of your project. But again for learning this course Docker compose is by far the easiest method to spin up a grid.

Conclusion

Using Docker Compose to install and run Selenium Grid 4 simplifies the process of setting up a scalable test environment. It allows you to easily manage the Grid and scale up or down as required. This setup is ideal for development teams looking to automate their testing processes and improve the efficiency of running tests across different browsers and environments.