CosmoCode (Formerly TeachMeSelenium)

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.

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.

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:

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.

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.

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.

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.

Exit mobile version