Selenium Grid | Setting up Hub and Nodes

In the Previous Tutorial, we learned some advanced user interactions like automating mouse and keyboard movements. In this tutorial, we’ll learn to distribute our tests on remote machines.

What is Selenium Grid?

Selenium Grid provides a mechanism where we can run tests in a distributed environment against multiple browsers and operating systems. It works as a hub-nodes combination where multiple nodes or machines can be configured to a single hub machine.

Selenium Grid is a smart proxy server that allows Selenium tests to route commands to remote web browser instances. Its aim is to provide an easy way to run tests in parallel on multiple machines.

https://selenium.dev/documentation/en/grid/

Hub

A hub is a machine (physical, virtual or a container) that drives the tests and distributes it to the node machines with the asked configuration. It receives the JSON formatted test commands and routes it to one or more registered nodes.

Node

Nodes are machines distributed over the network or configured as a Virtual Machines inside the host machine. It is also possible to create multiple virtual machines inside the same host machine and register them as nodes and execute the test. It is the nodes that perform actions on their browser based on the commands they get from the hub.

Suppose we want to test our application on Linux machine against Firefox browser version 16. We would find a machine of matching configuration, then move your code to that machine and finally run the code there. The very next day we want to test the same application on Windows 7 and against Chrome 20. Rework, rework and rework for each change of environment. Isn’t it?

How it works?

The hub or nodes in the Selenium Grid architecture can be a physical machine, virtual machine or docker containers. Also, there must be only one hub in a single Selenium Grid architecture.

Grid
Source: https://selenium.dev/documentation/en/grid/components_of_a_grid/

The client is your WebDriver test. It could reside on any machine. It could also be the same machine which acts as a hub.

How to start a Hub?

If you remember in This Tutorial we downloaded selenium-server-standalone.jar file. You can put that file in any directory on your computer. For this tutorial, I am assuming it is placed at C drive.

  • We need to run the following command in Terminal/Command prompt to start a hub:
java -jar C:\selenium-server-standalone-3.11.0.jar -role hub

Please update the name and location of the selenium-server-standalone.jar file as your environment.

If selenium grid server is launched successfully we may get a similar message in the command prompt window:

  • To verify if the hub has started we can hit the following URL in a browser – http://localhost:4444/grid

By default, the hub will run on port 4444. If we want to change the default port, we can add the optional parameter -port in the previous command.

  • To verify the hub details we can click the console link and then view config link on next page.

How to register Nodes to a Hub?

We can register to add many nodes to a single hub in Selenium grid architecture. The nodes can also reside in separate machines/containers. In this tutorial, we’ll add only one node and expect both the hub and node to reside in the same machine.

  • Do not close the previously opened window that was used to register the hub.
  • Run the following command in a new Terminal/Command prompt window to register the node to the running hub:
java -jar C:selenium-server-standalone-2.43.1.jar -role node -hub http://127.0.0.1:4444/grid/register

Please update the name and location of the selenium-server-standalone.jar file as your environment.

What the heck is that IP address – 127.0.0.1:4444?

Well, after the -hub flag you need to specify the IP address of the machine where the hub is registered. After IP address you need to give a colon(:) and then specify the port on which the hub is running. In this example, we are registering hub and node in the same machine.

In this case, we can either specify localhost or 127.0.0.1 as IP address. If your hub was registered on some other machine you need to update this command by specifying the IP address of that machine.

Also, by default, the hub runs on 4444 port. If you have started a hub on some other port then you need to update the command accordingly.

  • If all goes well, the terminal/command prompt window will display a message that the node is being registered:

By default, the node runs on port 5555. If you want to use some other port you can specify it by using the -port flag in the command while registering the node.

If we hit the following URL – http://localhost:4444/grid/console we can see the details:

We can apply the exact same steps to register more nodes to the hub if we want.

We have successfully set up a Selenium grid infrastructure. In the Next Tutorial, we will learn how to write code to make use of this setup.

8 thoughts on “Selenium Grid | Setting up Hub and Nodes”

  1. Good information and it shows how the big giants in the market would be testing their sites, before making any launch.

    would like to know how the results are reported back to hub. I mean if at one node it fails while still running. So how the coordination happens.
    Also it would be interesting to write a code which should run on multiple configurations

    Reply
  2. We need to handle reporting in code itself and not to worry on which node the test is running.
    For instance suppose the hub is on M1 and the node is on M2. The code would run on M1 and the browser would be running on M2. Whatever code it is, the instructions would be passed to node by hub automatically. We dont need to woory about it.
    So, if we have a code that clicks on 'Products' link, the next line od code should validate that Products page is opened or not. Suppose, the page is not opened the line of code that checks existance of target page would fail and we would get to know.

    Reply

Leave a Reply