Selenium Grid

Selenium Grid is part of the Selenium project. It lets you distribute test execution across several machines.

You can connect to it with Selenium Remote by specifying the browser, browser version, and operating system you want.

Why you want to use Selenium-Grid. ?

    To run tests in multiple browsers, multiple versions of browser, and browsers running on different operating systems.

    To reduce the time it takes for the test suite to complete a test pass.

    Selenium-Grid is used to speed up the execution of a test pass by using multiple machines to run tests in parallel.

For eg: if you have a suite of 1000 tests, but you set up Selenium-Grid to support 4 different machines (VMs or separate physical machines) to run those tests, your test suite will complete in (roughly) one-fourth the time as it would if you ran your tests sequentially on a single machine.

For large test suites, and long-running test suite such as those performing large amounts of data-validation, this can be a significant time-saver.

Some test suites can take hours to run. Another reason to boost the time spent running the suite is to shorten the turnaround time for test results after developers check-in code for the AUT.

Increasingly software teams practicing Agile software development want test feedback as immediately as possible as opposed to wait overnight for an overnight test pass. Selenium-Grid is also used to support running tests against multiple runtime environments, specifically, against different browsers at the same time.

For example, a ‘grid’ of virtual machines can be setup with each supporting a different browser that the application to be tested must support. So, machine 1 has Internet Explorer 8, machine 2, Internet Explorer 9, machine 3 the latest Chrome, and machine 4 the latest Firefox. When the test suite is run, Selenium-Grid receives each test-browser combination and assigns each test to run against its required browser.

In addition, one can have a grid of all the same browser, type and version.

For instance, one could have a grid of 5 machines each running 4 instances of Firefox 12, allowing for a ‘server-farm’ (in a sense) of available Firefox instances. When the suite runs, each test is passed to Selenium-Grid which assigns the test to the next available Firefox instance. In this manner one gets test pass where conceivably 12 tests are all running at the same time in parallel, significantly reducing the time required to complete a test pass.

Selenium-Grid is very flexible. These two examples can be combined to allow multiple instances of each browser type and version.

A configuration such as this would provide both, parallel execution for fast test pass completion and support for multiple browser types and versions simultaneously.

linktext in selenium-webdriver

How to install Selenium Grid

1.Goto https://seleniumhq.org/download/
Grid Concept

2.Downlaod the Selenium server.yes,we use it for both purposes for writing selenium script and also as server.

3.Save it in some place in your system.

To implement Grid Concept we need to implement follow below steps:


1.Start the Hub

2.Start the node

3.Execute script from Hub machine

4.Check results in Hub machine

There is no such thing called as Grid installation & Grid installation means installing HUB and NODE

Hub

Hub is system where all the framework and all details available, follow below steps to install

1.Goto the CMD
Grid Concept with cmd selenium webdriver

2.Write the below command in the CMD and hit enter button

java -jar c:\LocationOfServer\SeleniumServerName.jar  -role hub


cmd code to start hub in selenium webdriver

3.When Grid starts running you can see like below
start hub selenium webdriver

Once you see this then hub has started
4.Grid always starts in port: 4444 by default
selenium grid hub's default port selenium webdriver

5.You can also save this command in .bat file (like running TestNG script from batch file)
6.If port 4444 is occupied by some other process means you can give your own node like 8888,8899,1234...

java -jar c:\LocationOfServer\SeleniumServerName.jar  -role hub -port 8888 

Method Overloading in Selenium and java

How to install Selenium Grid

1.Goto https://seleniumhq.org/download/
Grid Concept

2.Downlaod the Selenium server.yes,we use it for both purposes for writing selenium script and also as server.

3.Save it in some place in your system.

To implement Grid Concept we need to implement follow below steps:

  1. Start the Hub
  2. Start the node
  3. Execute script from Hub machine
  4. Check results in Hub machine

There is no such thing called as Grid installation & Grid installation means installing HUB and NODE

Create Headless Firefox Browser in Selenium webdriver

Node

Node is system execution happens and execution is controlled by hub, follow below steps to install selenium node

1.Goto the CMD
Grid Concept with cmd

2.Write the below command in the CMD and hit enter button

java -jar c:\LocationOfServer\SeleniumServerName.jar  -role node -hub http://ipaddress:4444/grid/register



But in my case I am using same system as Hub and Node so I can replace the ip address with LocalHost

java -jar c:\Node\selenium-server-standalone-2.45.0.jar  -role node -hub http://localhost:4444/grid/register



cmd code to start hub in selenium


3.When Grid starts running you can see like below

start hub
Once you see this then hub has started

4.Grid always starts in port: 4444 by default

selenium grid hub's default port
5.You can also save this command in .bat file (like running TestNG script from batch file)

Execution with Grid and Opting Browser and OS

Node is system execution happens and execution is controlled by hub

Run Grid:

1.Goto program which you want to run in grid
2.In order execute a script in Grid we need to create a RemoteWebdriver Class

WebDriver driver=new RemoteWebDriver(hubUrl, dCap);

3.We should supply url of the hub and the desired capabilities(browser and os) of the remote computer to the constructor of RemoteWebdriver class while creating object


						//hub URL
						URL hubUrl=new URL("http://localhost:4444/grid/register");
						//creating instance to Desired Capabilities class
						DesiredCapabilities dCap=new DesiredCapabilities();
						//set the browser name
						dCap.setBrowserName("firefox");
						//set the os
						dCap.setPlatform(Platform.WINDOWS);

					


Complete program for remote webdriver execution

						import java.net.MalformedURLException;
					import java.net.URL;

					import org.openqa.selenium.Platform;
					import org.openqa.selenium.WebDriver;
					import org.openqa.selenium.firefox.FirefoxDriver;
					import org.openqa.selenium.remote.DesiredCapabilities;
					import org.openqa.selenium.remote.RemoteWebDriver;
					import org.testng.Reporter;
					import org.testng.annotations.Test;

					public class TestNGClass 
					{
						public static void main(String[] args) throws MalformedURLException 
						{
							//hub URL
							URL hubUrl=new URL("http://localhost:4444/grid/register");
							
							//creating instance to Desired Capabilities class
							DesiredCapabilities dCap=new DesiredCapabilities();
							
							//set the browser name
							dCap.setBrowserName("firefox");
							
							//set the os
							dCap.setPlatform(Platform.WINDOWS);
							
							WebDriver driver=new RemoteWebDriver(hubUrl, dCap);
							
							driver.get("https://chercher.tech");
							//remaining normal code
						}
						
					}

					

Run the code it will execute it in the node.

Ways to select dropdown option in selenium

 
Join My Facebook Group
Join Group
 

About Author

Myself KarthiQ, I am the author of this blog, I know ways to write a good article but some how I donot have the skills to make it to reach people, would you like help me to reach more people By sharing this Article in the social media.

Share this Article Facebook
Comment / Suggestion Section
Point our Mistakes and Post Your Suggestions
  • Rajesh
    Hi Tried GRID set up as given in this artical but i gettting error "Exception in thread "main" org.openqa.selenium.WebDriverException: Unable to parse remote response: " Evety time 
     pls help
    Reply
  • Copyright © CherCher Tech