How to create a Selenium Grid?


Selenium grid is one of the components of selenium. It is used to execute test automation on multiple browsers, operating systems at the same time. You not only want to test your web application on a single OS and browser. You want your application to be compatible with multiple browsers and OS, and even mobile smartphones for android and iOS. To achieve this we implement a grid component of selenium.







1. Create a hub in selenium grid:


Hub is a central machine on which automation code resides and this hub route the selenium commands to nodes registered with this hub. I've two windows laptops with me. On one of the laptop, I've my selenium automation code set up.
Create this laptop as a hub by following the below steps.

Open the command prompt.
Go to the location where your selenium server standalone jar resides.
Now type the below command.

java -jar selenium-server-standalone-3.141.59.jar -role hub

Hit enter, and you'll see -
'Selenium Grid hub is up and running'
It'll also tell you the IP and port to register the node to this hub.

creating a hub




2. Create a node in selenium grid:


A node is a target machine on which we want to execute our selenium test scripts.
In node machine also you need to have selenium server standalone jar along with chrome driver exe. Create the second laptop a node by following the below steps:

Open the command prompt.
Go to the location where your selenium server standalone jar resides.
Now type the below command.

java -Dwebdriver.chrome.driver=D:\chromedriver.exe -jar selenium-server-standalone-3.141.59.jar -role node -hub http://192.168.1.10:4444/grid/register

Here http://192.168.1.10:4444 is the IP and port of the hub where this node has to be registered.
Now hit enter, you'll see-
'The node is registered to the hub and ready to use'.

creating a node

In the hub machine you will see- 'Registered a node http://192.168.1.8:17042'

creating a node

Now you have your hub and node ready. The node is successfully registered to the hub successfully. Now make the following changes to your selenium code which is residing on the hub machine.

setting capabilities for hub and node

Since we're executing our automation test on a remote machine that we've set as a node we've to set the desired capabilities as shown above, and create a URL object. Give IP and port of node machine in the url constructor.
Now next step is initialized WebDriver object but instead of writing
WebDriver driver=new ChromeDriver();
You have to write:
WebDriver driver=new RemoteWebDriver(node,dc);

and pass node url object and desired capabilities object as arguments in the RemoteWebDriver constructor.
The rest will remain the same i.e.

driver.get("http://www.abcd.com");

Now run your automation test on your hub, and the execution will take place on the node machine. Also, check test automation execution on cloud Test automation execution on cloud