How to configure and run Selenium Grid

Selenium Grid : Selenium Grid is a component of Selenium that distributes multiple tests across different browsers, operating systems, and machines so that we can execute scripts in parallel. The selenium-server-standalone package includes Hub, WebDriver, and Selenium RC to execute the scripts in grid.

Selenium Grid has a Hub and a Node.
Hub :The hub is the central point  where the tests would be triggered. A Selenium Grid has only one Hub and it is launched on a single machine once.
NodeNodes are the Selenium instances that are linked to the Hub which execute the tests. There can be one or more nodes in a grid which can be launched on multiple machines with different platforms and browsers.

Prerequisite Software:

1. Download Eclipse
2. Download Selenium Standalone Server jar

3. The Internet Explorer Driver Server

4. Google Chrome driver for selenium webdriver

5. Opera driver for selenium webdriver


Installing TestNG for Eclipse:

Step 1 : Launch Eclipse and select Main Menu 'Help' --> 'Install New Software' .

Step 2 : Enter the URL as 'http://beust.com/eclipse' and click 'Add' .

Step 3 :  Enter the name as 'TestNG' on dialog box 'Add Repository' dialog box. and click 'OK'

Step 4 : Click 'Select All'

Step 5 : Click 'Next' to continue.

Step 6 :  Click 'Next'.

Step 7 : "Accept the License Agreement" and click 'Finish'.

Step 8 : Security Warning pops up as the validity of the software cannot be established. Click 'Ok'

Step 9 : The Installer prompts to restart Eclipse for the changes to take effect. Click 'Yes'.

Configuring the Hub:

Step 1 :  Port# 4444 must be free. The following below commandon Window command window to start the Hub by launching the Selenium Server. Now we will use the port '4444' to start the hub. 
java -jar selenium-server-standalone-2.53.0.jar -port 4444 -role hub -nodeTimeout 1000
Example: java -jar F:\Grid\selenium-server-standalone-2.53.0.jar -port 4444 -role hub -nodeTimeout 1000
Step 2 : Open desired browser and navigate to the URL http://localhost:4444 from the Hub
Step 3 : Click on 'console' link  --> click 'view config'. The config of the hub would be displayed as follows. As of now, we haven't got any nodes, hence we will not be able to see the details.

Configuring the Nodes: 

Step 1 : Start Firefox Node using the following below command.
java -jar 'the location of selenium server standalone jar' -role node -hub http://PC_IP:4444/grid/register -browser browserName=firefox -port port_number
Example:
java -jar F:\Grid\selenium-server-standalone-2.53.0.jar -role node -hub http://192.168.111.141:4444/grid/register -browser browserName=firefox -port 5555
 Step 2 : Launch Internet Explorer Node using the following below on command.
java -Dwebdriver.ie.driver='the location of IE driver' -jar 'the location of selenium server standalone jar' -role webdriver -hub http://PC_IP:4444/grid/register -browser browserName=ie,platform=WINDOWS -port port_number
Example: 
java -Dwebdriver.ie.driver=F:\Grid\IEDriverServer.exe -jar F:\Grid\selenium-server-standalone-2.53.0.jar -role webdriver -hub http://192.168.111.141:4444/grid/register -browser browserName=ie,platform=WINDOWS -port 5556 
 Step 3 : Launch Chrome Node using the following below on command.
java -Dwebdriver.chrome.driver='the location of chrome driver' -jar 'the location of selenium server standalone jar' -role webdriver -hub  http://PC_IP:4444/grid/register -browser browserName=chrome,platform=WINDOWS -port port_number
Example: 
java -Dwebdriver.chrome.driver=F:\Grid\chromedriver.exe -jar F:\Grid\selenium-server-standalone-2.53.0.jar -role webdriver -hub  http://192.168.111.141:4444/grid/register -browser browserName=chrome,platform=WINDOWS -port 5557 
Step 4 : After running the command, come back to the Hub. Navigate to the URL - http://pc_ip:4444 then Click on 'console' link ,  the Hub would now display Firefox, IE and chrome node attached to it.