Selenium Grid- Complete guide

Grid Installation/Set up

Prerequisite

1.     Java installed on the machine and environment variable set for Java (JAVA_HOME).
2.     Eclipse installed.
3.     TestNG is installed in eclipse.

Steps to install grid

1.     Download selenium server jar from Selenium Downloads Page , you will find it under section “Selenium-Server (formerly Selenium-RC)”.
2.     Extract it in your system say C drive . For Demonstration purposes , I will be assuming the path of selenium server jar is “C:\”.

Why do we need Selenium Grid

1.     To test Browser and OS compatibility for UI tests in parallel. For example, you want to test your application for multiple combinations of OS and browser like chrome on Windows 7, 8 and 10. IE 9, 10 and 11 etc. 
2.     To save execution time by distributing the tests on
a.      Multiple machines
b.     Running on multiple browser instances on same machine.
Which will run in parallel.

How Selenium grid works

·       HUB : Hub is a master machine which distributes your tests to registered slave machines (nodes).
·       NODE : Node is a slave machine which takes and executes the commands from the master machine (hub).


                       
Grid consists of Hub and nodes. Single hub and one or more nodes registered to the hub.
The hub receives a test to be executed along with information on which browser (Chrome, IE, Firefox safari etc )and ‘platform’ (i.e. WINDOWS, LINUX, etc) where the test should be run. It has the list of nodes registered under it, it selects an available node that has the requested browser-platform combination and initiates, Selenium commands (testcase) and passes them to the node selected. The node runs the browser, and executes the testcase within that browser against the application under test.

Configure Hub and Node

Staring Hub :

To start the selenium grid service first start the hub. We need the below to get started:
1.     Selenium server jar
2.     Identify the machine to be a hub
3.     Identify the port on which the hub service is to be started.

Navigate to the folder where you have placed selenium server jar (I am assuming it under C:\)
Execute the following command in command line (Windows machine) or shell/terminal (Linux/Mac) to run the hub using the default options simply specify -role hub to the Selenium-Server
java -jar selenium-server-standalone-2.44.0.jar -hub

Default port used by selenium grid is – 4444
Machine ip used – localhost
You should be able to see the below on your command line
C:\Users\shama.ugale\Documents\GRID\hubLaunched.png
To verify if hub is working as expected , go to your browser and hit the below URL :
http://localhost:4444/grid/console.
You should see the below page
To run Grid on a specific port, use the below command
java -jar selenium-server-standalone-2.44.0.jar hub –port 5555

Staring Node :

Running the node in the same machine as that of hub.
We need the below to get the node running:
1.     Hub started.
2.     Chromedriver.exe and IEDriverServer.exe and geckodriver.exe in case of selenium 3.

Now open a new command prompt, navigate to the folder where selenium serve jar is extracted. Type the below command
java -jar selenium-server-standalone-2.44.0.jar –role node      –Dwebdriver.ie.driver=”c:/IEDriverServer.exe” –Dwebdriver.chrome.driver=”c:/chromedriver.exe” –hub http://localhost:4444/grid/register -port 5566

Here your node is up and running on port 5566.
By default the number of browser instances will be as follows
1.     Firefox – 5 instances
2.     Chrome – 5 instances
3.     IE – 1 instance.

Now refresh your page
http://localhost:4444/grid/console.
You should see the below page.

You can control the number of browser instances by using the below command to start the node.
java -jar selenium-server-standalone-2.44.0.jar –role node      –Dwebdriver.ie.driver=”c:/IEDriverServer.exe”
–Dwebdriver.chrome.driver=”c:/chromedriver.exe”
–hub http://localhost:4444/grid/register -port 5566
-browser browserName=firefox,maxInstances=10
-browser browserName=chrome,maxInstances=8
-browser browserName=iexplore,maxInstances=5

Running the node on some other machine.
Pre-requisites :
1.     Java should be installed on that machine.
2.     Selenium sever should be downloaded.
3.     Register this machine as a node to the hub launched on some other machine. Follow the below steps :
a.      Launch a command prompt.
b.     Type the below command.
java -jar selenium-server-standalone-2.44.0.jar –role node      –Dwebdriver.ie.driver=”c:/IEDriverServer.exe”
–Dwebdriver.chrome.driver=”c:/chromedriver.exe”
–hub http://HUB_IP:HUB_PORT/grid/register -port 5566
-browser browserName=firefox,maxInstances=10
-browser browserName=chrome,maxInstances=8
-browser browserName=iexplore,maxInstances=5

Note: In case you are using selenium 3 , please also pass the gecko driver as well. See the below example

java -jar selenium-server-standalone-2.44.0.jar –role node
-Dwebdriver.gecko.driver=”c:/geckodriver.exe”
–Dwebdriver.ie.driver=”c:/IEDriverServer.exe”
–Dwebdriver.chrome.driver=”c:/chromedriver.exe”
–hub http://HUB_IP:HUB_PORT/grid/register -port 5566
-browser browserName=firefox,maxInstances=10
-browser browserName=chrome,maxInstances=8
-browser browserName=iexplore,maxInstances=5


Code to Support Selenium Grid execution
Testng File
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite" parallel="tests" thread-count="3">
       <parameter name="browser" value="Chrome" />
              <parameter name="platform" value="Windows" />            
              <parameter name="Frontend_Server" value="http://rahul-k.dev.mykronos.internal/dashboard"/>
              <parameter name="Tenant" value="manufacturing" />
              <parameter name="Report_Loc" value="C:\\Reports\\" />
              <parameter name="Log_Loc" value="C:\\Reports\\log" />
              <parameter name="logInfoLevel" value="true" />
              <parameter name="dateFormat" value="MM/dd/yyyy" />
              <parameter name="deepReporting" value="True" />
              <parameter name="runWithSeleniumGrid" value="true" />
              <parameter name="HUB_URL" value="http://localhost:4444/wd/hub" />
              <parameter name="timeout" value="60" />
       <test name="Test Chrome">
              <parameter name="browser" value="Chrome" />
              <parameter name="platform" value="Windows" />            
              <groups>
                      <run>
                             <include name="Smoke" />
                      </run>
              </groups>
              <packages>           
                      <package name="kronos.testcases.*" />                           
              </packages>
       </test> <!-- Test -->
      
       <test name="Test firefox">
              <parameter name="browser" value="firefox" />
              <parameter name="PLATFORM" value="Windows" />            
              <groups>
                      <run>
                             <include name="Smoke" />
                      </run>
              </groups>
              <packages>           
                      <package name="kronos.testcases.*" />
                            
              </packages>
       </test> <!-- Test -->
      
       <test name="Test IE">
              <parameter name="browser" value="ie" />
              <parameter name="platform" value="Windows" />            
              <groups>
                      <run>
                             <include name="Smoke" />
                      </run>
              </groups>
              <packages>           
                      <package name="kronos.testcases.*" />
                            
              </packages>
       </test> <!-- Test -->
</suite> <!-- Suite -->

Selenium Code :
DesiredCapabilities caps = DesiredCapabilities.firefox(); // or chrome or IE
caps.setPlatform(env);                    
return new RemoteWebDriver(new URL(hubUrl),caps);

Note :
1.  Hub URL : http://IP_of_Hub:port_no_of_the_hub/wd/hub , this comes from the testng.xml file.

The hub will automatically distribute the tests to the nodes registered to it based on the browser and platform combination request coming from tests parameters and the nodes configuration.


You are good to go test execution using with selenium grid

Comments

  1. Hello Shama,
    The Article on Selenium Grid Complete Guide is nice.It give detail information thanks for Sharing the information about it. Software Testing Services

    ReplyDelete

Post a Comment

Popular posts from this blog

Cross Browser testing for React Apps

Fixing the appium installation with NPM

Challenges in testing ChatBots