top of page

Selenium Grid

What is Selenium Grid?

Generally we run our tests parallel in same machine. But if our testcases are more, then our machine will run out of memory. Say 15 chrome browser opens at the same time. Then you would see slowness in your machine and your testcases will start failing. So how about distributing them in multiple machines like 3 per machine.

In this way our execution time is same and we can run 5 test cases per machine. So our system performance will also be good. So Selenium Grid will help you to run your tests in multiple machines.

Selenium Grid is a smart proxy server that makes it easy to run tests in parallel on multiple machines.

But you can control your execution from a single machine. So you will run the testcases from your machine but Selenium Grid will take care how to distribute to different machines.

After executing it consolidates all the output from different machines and shows in your console.. How does it happen? Lets see about Selenium grid in detail.

Internal Architecture of Selenium Grid:

There is one centralized execution system called as "HUB" which consists of all internal components. The Hub could be in your local machine where you are writing your selenium code and trying to run your test cases. This Hub decides how to distribute your load to different machines. In Grid terminology we call different machines as your node. The different machines can have different browsers installed in them. If your test case demand Chrome then Grid will transfer that load to the machine which has installed the chrome browser. It reads the system properties and decides how to transfer the load. If all system has Chrome browser then it will load balance the traffic. So that level knowledge this Grid has.

Selenium Raw code which we write in eclipse is the client. So if we run our test cases we are making a request.

Request first goes to the router. It will ask the distributor to create a new session and assign it to the one physical machine(Node). All the requests are first received by router only. First time requests router is not sure which machine it has to send so it contacts the distributor to create a session ID for a machine. The distributor is the one which reads your system properties to distribute the load to the machines (browser and other OS details).

At the same time it sends information to the Session Map also. The session information will be stored in Session Map and distributor will share the machine information also to the distributor. The IP address of the machine is also provided to Session Map. The session map stores the session ID and IP address (Node URL) of the machine which will execute the code.

After this if the requests coming for same session (same test case) then router will not ask distributor now for figuring out the machine it will check with the session Map to provide information about the node if already exists.

It will directly route to the physical machine. This is how our Selenium Grid architecture is designed.

Lets create a Hub and a node and see how we can run our test cases in that node.

Selenium Grid Setup Instructions-

The following are the steps to setup the selenium grid in our machines. There are several ways to setup the selenium Grid. We can either start each components one by one or to save time we can start all the components together as a Hub (different Grid roles).

  1. Distributed: Each components have to be started on its own. To Start Event Bus All the required communication are done by event bus. This is a channel. Command: java -jar selenium-server-<version>.jar event-bus --publish-events tcp://<event-bus-ip>:4442 --subscribe-events tcp://<event-bus-ip>:4443 --port 5557 To Start New Session Queue If there are multiple sessions then distributor can solve the sessions one by one. Rest all will wait in New Session Queue. Command: java -jar selenium-server-<version>.jar sessionqueue --port 5559 To Start Session Map Command: java -jar selenium-server-<version>.jar sessions --publish-events tcp://<event-bus-ip>:4442 --subscribe-events tcp://<event-bus-ip>:4443 --port 5556 To Start Distributor: Command: java -jar selenium-server-<version>.jar distributor --publish-events tcp://<event-bus-ip>:4442 --subscribe-events tcp://<event-bus-ip>:4443 --sessions http://<sessions-ip>:5556 --sessionqueue http://<new-session-queue-ip>:5559 --port 5553 --bind-bus false To Start Router: Command: java -jar selenium-server-<version>.jar router --sessions http://<sessions-ip>:5556 --distributor http://<distributor-ip>:5553 --sessionqueue http://<new-session-queue-ip>:5559 --port 4444 To Start Node: Command: java -jar selenium-server-<version>.jar node --publish-events tcp://<event-bus-ip>:4442 --subscribe-events tcp://<event-bus-ip>:4443

  2. Hub and Node: This is very friendly and suitable for small size grids. We simply start the Hub here. one command to start the Hub then the components will automatically start inside the hub. No need to give separate commands for starting all components. We will be seeing Hub and Node in this blog!!

Download the Selenium Server(Grid). It is Jar file. Download other Browser drivers based on your browser versions and place in the same folder/path where Selenium server is located or we can check for Selenium Server (Grid) maven dependency. Now from that path open your terminal.

Start the Hub - which eventually Starts Router, Distributor, Session Map , New Session Queue, Event Bus


• java -jar <SeleniumJarname> hub

SeleniumJarname ---> Selenium Server Grid downloaded. Send hub as an argument. Now eventually the processing will be done and starts your components.

We can see from the below screenshot that Socket is created and Event Bus is ready.

To Check the status of our hub

There are no registered nodes currently. We have to attach machines to it. So we will start a node. Hub and node can be the same system also. Hub can act as a node to execute the test cases. We will see how to have hub and node in same machine in this blog.

To Start the Node in Same Machine where Hub is running . Open the terminal from the folder which has your Selenium server and browsers. Don't write in previous terminal where hub is running. Now execute the following:

Command : java -jar <SeleniumJarname> node --detect-drivers true

Node is sent as a argument here.

We will get the message that our Event Bus is ready. Now your Nodes are added. We have added the nodes in the same hub machine. check your local host http://localhost:4444. Now it has the ability of running 8 Chrome browsers, 8 Firefox browsers and 8 edge browsers. It can run 8 tests at a time. Your nodes are listening in the port 5555. Now if we check the terminal of Hub, new logs are added. It will ping every two min to check if your node is active.

08:38:59.647 INFO [LocalDistributor.add] - Added node 12c26a02-5a2b-48c6-9491-9e14111a77cd at your ip address. Health check every 120s

How to Start the Node in different Physical Machine?

We need to follow the same steps. Download the Selenium Server Grid and browser driver in the same folder and

open the terminal from the folder. We have to have the selenium code also in that physical machines. All the set up should be ready for that machine also.

The following command to provide in the different physical machine:

java -jar <SeleniumJarname> node --detect-drivers true -- publish-events tcp://<ipaddressofhub> --subscribe-events tcp:// <ipaddressofhub>

We need to give Ip address of the hub (publish and subscribe events) --> these are transfer protocols from XPUB and XSUB. For connecting to these protocols we need the ip address of Hub.

tcp://<ipaddressofhub> ---> for information transfer.

I have written a simple program to run in grid:

We need two imports to make this possible. We have to use RemoteWebDriver class to run in hub.

We need to send two arguments for this class. one is hub listening port with ip address. We need to wrap them as class with :"new URI" and convert to URL for accessing remote web Driver. and browser details as second argument with the help of setBrowserName method from DesiredCapabilities caps =new DesiredCapabilities(); .

We can set platform details also using DesiredCapabilities class. import org.openqa.selenium.remote.DesiredCapabilities;

import org.openqa.selenium.remote.RemoteWebDriver;

Run the program and we can check our grid. Our Chrome browser has used 13% for this test.

Thanks!! Happy Learning!!!

69 views0 comments

Recent Posts

See All


Rated 0 out of 5 stars.
No ratings yet

Add a rating
bottom of page