top of page
Search
Writer's pictureTuyen Nguyen

Part 1 - Set up Selenium Grid with different modes

Updated: Jul 29

As mentioned in main post, we will start with instruction on how to set up selenium Grid with different modes.


Introduction

Selenium Grid allows the execution of WebDriver scripts on remote machines by routing commands sent by the client to remote browser instances.

Grid aims to:

  • Provide an easy way to run tests in parallel on multiple machines

  • Allow testing on different browser versions

  • Enable cross-platform testing

In scope of this test, I only use Selenium Grid 4 (latest version).


Prerequisites

Make sure you have these below before started:

  • Java 11 or higher

  • Browser(s) installed

  • Download new Selenium Server jar: latest release

Below instructions use version 4.7.2 of Selenium Server.


Standalone Mode

Standalone combines all Grid components seamlessly into one.

Running a Grid in Standalone mode gives you a fully functional Grid with a single command, within a single process. Standalone can only run on a single machine.


Common use cases for Standalone are:

  • Develop or debug tests using RemoteWebDriver locally

  • Running quick test suites before pushing code

  • Have a easy to setup Grid in a CI/CD tool (GitHub Actions, Jenkins, etc…)


Start the Grid

Execute below command where you store the downloaded Selenium Server jar file:

java -jar selenium-server-<version>.jar standalone

// We use 4.7.2
java -jar selenium-server-4.7.2.jar standalone

By default, the server will detect the available drivers that it can use from the System

and register nodes with available browsers to the grid server.

In below screenshot, there are 4 drivers discovered (Chrome, Edge, Firefox and Internet Explorer) so they are added.


The server started

The server will start automatically at http://localhost:4444


Point the test

Point RemoteWebDriver in your test to http://localhost:4444

ChromeOptions chromeOptions = new ChromeOptions();
driver = new RemoteWebDriver(new URL("http://localhost:4444"), chromeOptions);

There is an example that I have already written in this repo: https://github.com/trongtuyen96/selenium-grid-docker-zalenium


Head to /test/java/SeleniumGridTest.java for more details. To execute the test properly, you need to run the SeleniumGrid.xml file.

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="SeleniumGridTest_Suite" parallel="tests">
    <test name="ChromeTest">
        <parameter name="browser" value="chrome"/>
        <classes>
            <class name="SeleniumGridTest"/>
        </classes>
    </test>
    <test name="FirefoxTest">
        <parameter name="browser" value="firefox"/>
        <classes>
            <class name="SeleniumGridTest"/>
        </classes>
    </test>
    <test name="EdgeTest">
        <parameter name="browser" value="edge"/>
        <classes>
            <class name="SeleniumGridTest"/>
        </classes>
    </test>
</suite>

There will be 3 sessions run in parallel for Chrome, Firefox and Edge. Each will run all 3 tests specified in SeleniumGridTest class.


Hub and Node mode

Hub and Node is the most used role because it allows to:

  • Combine different machines in a single Grid

    • Machines with different operating systems and/or browser versions, for example

  • Have a single entry point to run WebDriver tests in different environments

  • Scaling capacity up or down without tearing down the Grid


Hub

To start a hub:

java -jar selenium-server-4.7.2.jar hub

By default, the server will listen for RemoteWebDriver requests on http://localhost:4444


Node

The command below assumes the Node is running on the same machine where the Hub is running:

java -jar selenium-server-4.7.2.jar node

During startup time, the Node will detect the available drivers that it can use from the System.

To set up more nodes on the same machine, we can specify different ports for them:

// Node 1 
java -jar selenium-server-<version>.jar node --port 5555  

// Node 2 
java -jar selenium-server-<version>.jar node --port 6666
Node 1


Node 2

The server started

The server will start automatically at http://localhost:4444

And there are 2 node registered on port 5555 and 6666


Point the test

Point RemoteWebDriver in your test to http://localhost:4444

case "chrome":
    ChromeOptions chromeOptions = new ChromeOptions();
    driver = new RemoteWebDriver(new URL("http://localhost:4444"),chromeOptions);
    break;
case "firefox":
    FirefoxOptions firefoxOptions = new FirefoxOptions();
    driver = new RemoteWebDriver(new URL("http://localhost:4444"),firefoxOptions);
    break;

There is an example that I have already written in this repo: https://github.com/trongtuyen96/selenium-grid-docker-zalenium


Head to /test/java/HubNodeTest.java for more details. To execute the test properly, you need to run the HubNode.xml file.

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="HubNodeTest_Suite" parallel="tests">
    <test name="ChromeTest">
        <parameter name="browser" value="chrome"/>
        <classes>
            <class name="HubNodeTest"/>
        </classes>
    </test>
    <test name="FirefoxTest">
        <parameter name="browser" value="firefox"/>
        <classes>
            <class name="HubNodeTest"/>
        </classes>
    </test>
</suite>

There will be 2 sessions run in parallel for Chrome, Firefox. Each will run all 3 tests specified in HubNodeTest class.


Hub and Node on different machines

Hub and Nodes talk to each other via HTTP and the Event Bus (the Event Bus lives inside the Hub). A Node sends a message to the Hub via the Event Bus to start the registration process. When the Hub receives the message, reaches out to the Node via HTTP to confirm its existence.


To successfully register a Node to a Hub, it is important to expose the Event Bus ports (4442 and 4443 by default) on the Hub machine. This also applies for the Node port. With that, both Hub and Node will be able to communicate.


After starting the Hub with default ports, the --hub flag can be used to register the Node

java -jar selenium-server-<version>.jar node --hub http://<hub-ip>:4444

When the Hub is not using the default ports, the --publish-events and --subscribe-events flags are needed. For example, if the Hub uses ports 8886, 8887, and 8888

java -jar selenium-server-<version>.jar hub --publish-events tcp://<hub-ip>:8886 --subscribe-events tcp://<hub-ip>:8887 --port 8888

The Node needs to use those ports to register successfully

java -jar selenium-server-<version>.jar node --publish-events tcp://<hub-ip>:8886 --subscribe-events tcp://<hub-ip>:8887 

Distributed mode

When using a Distributed Grid, each component is started separately, and ideally on different machines. So 6 of components below need to be started:

  1. Event Bus

  2. New Session Queue

  3. Session Map

  4. Distributor

  5. Router

  6. Nodes

This is so far the most complex mode to set up so the detailed instructions will be added later 😁.


Grid size suggestions

  • Small: Standalone or Hub/Node with 5 or fewer Nodes.

  • Middle: Hub/Node between 6 and 60 Nodes.

  • Large: Hub/Node between 60 and 100 Nodes. Distributed with over 100 Nodes.

(selenium.dev)


Summary

Due to the completely new architecture, Selenium Grid 4 supports different modes – Standalone, Hub & Node, and Distributed. It is more modern than the previous versions of the Grid, as it can be used with modern technologies like Docker and Kubernetes with ease.


Hope this instruction helps you start your own journey with amazing Selenium Grid.



Stay tuned for Part 2, Setting up Selenium Grid with Docker 😁!!!

1,261 views0 comments

Recent Posts

See All

Comments


bottom of page