Setup Selenium Grid to run test parallelly across platforms

In this tutorial, we will talk about how can we setup selenium Grid to execute tests parallel.
Prior to this, to understand the basic concept of Selenium Grid, refer below article – 
Introduction to Selenium Grid – What is Selenium Grid?  <Link>
Selenium Grid will actual help you to run the tests on multiple nodes which will reduce the total execution time. Let us see how selenium Grid work – 
How Selenium Grid works? 
 Observe the above image, it shows how Selenium Grid works. It has Hub and Multiple nodes. Entire script is done at Hub node whereas entire execution will be done at Nodes. So, on various nodes we can have various OS along with various browsers. 

(Note: Note that, ideally node machines are different from Hub machine)

Let us talk more about Hub and Node –  
As the image shows, Hub is the machine where we write all our scripts. So, it a central point where we load out tests. In entire Grid system, we will have one Hub and multiple nodes. As it is Hub, it is launched on single machine. All the execution will be happening on Node machines, so there can be one or multiple nodes having different platforms with same or different browser. 
So, the concept is like, Hub has got one PC (Parent PC) whereas Nodes has got multiple machines (Child PC). Hub will be giving instructions to Nodes, accordingly nodes will execute the test cases. Hub is only to give instructions.
Before we setup Hub and Nodes, we need to get selenium Standalone server from http://docs.seleniumhq.org/download/   
To download Selenium Grid, click here
Let us see now how-to setup the Hub – 
Now, Selenium Standalone Server is a jar file, so we need to do the setup from command prompt.
Open Command Prompt.
Use below command to start Hub.
java -jar C:selenium-server-standalone-3.4.0.jar -role hub

Once above command has run, hub setup will complete. You will see the message saying Hub is running. Also, it gives the Hub address which we need while registering nodes.
Note that, Hub will start at port 4444 by default.
Now, let us see how to setup Nodes – 
Note: Ideally, nodes should be setup on different machines than Hub machine, but for demo purpose, I am using the same machine. The process for using different machines for Nodes is exactly same. Just we need to do the node setup on each of the node machine individually by giving Hub address (shown in above image).

To setup Node, we need same jar again. We just need to setup the role as node and give hub address while registering the Node. Let us see how this can be done.
Open another command Prompt. Do not close the command prompt where Hub is running.
Use below command to setup Node – 
java -jar C:selenium-server-standalone-3.4.0.jar -role node -hub http://192.168.1.106:4444/grid/register/
where, Selenium server standalone jar is placed in C drive. Hub machine address Is used to register node.

 
Observe above screenshot, it shows Node is running.
Now, we are done with hub and node setup. In the similar way, we can do the node setup on multiple nodes (machines) as required.
That’s it, Node setup is done on the machine for 3 browsers by default ( Firefox, IE and Chrome). To verify this, go to the link – http://localhost:4444/grid/console

Now, suppose, you want to setup only 1 browser, then use below command line –
java -jar C:selenium-server-standalone-3.4.0.jar -role webdriver -hub http://localhost:4444/grid/register -port 5556 -browser browserName=iexplore
Above command will set Internet Explorer.
If you want to setup only Chrome browser or Firefox browser, then use browserName=firefox and browserName=chrome
If you want to setup only 1 instance of all 3 browsers (Chrome, IE and firefox), then use – 
java -jar C:selenium-server-standalone-3.4.0.jar -role webdriver -hub http://localhost:4444/grid/register -port 5556 -browser browserName=iexplore -browser browserName=firefox -browser browserName=chrome

Once Hub and Node setup is done (Grid setup is done), Create one sample test case to run on the Grid.
Refer below example – 

package basicsOfSelenium;

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

import org.openqa.selenium.By;
import org.openqa.selenium.Platform;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.testng.annotations.Test;

public class ArrayDemo {

@Test

public void mailTest() throws MalformedURLException{

DesiredCapabilities dr=null;

dr=DesiredCapabilities.firefox();

dr.setBrowserName("firefox");

dr.setPlatform(Platform.WINDOWS);

RemoteWebDriver driver=new RemoteWebDriver(new URL("http://localhost:4444/wd/hub"), dr);

driver.navigate().to("https://www.google.co.in");

driver.findElement(By.xpath("//*[@id='gs_htif0']")) .sendKeys("Search me");

driver.findElement(By.xpath("//*[@id='tsf']/div[2]/div[3]/center/input[1]")) .click();

driver.close();

}
}
When you run above piece of code, your test should be executed on node machine, on firefox browser.
If you want to run the test on multiple platforms, parallelly, then what you can do is, set browserType parameter in testng.xml as below – 
                    <parametername =”browserType” value=”IE”/>
In similar way add above parameter for Chrome and firefox browser in testng.xml. Update the code to handle browser type in your program.
That’s it. 
This is how we can setup the Selenium Grid and run test cases in parallel on Selenium Grid.
Hope this helps.

 

Introduction to Selenium Grid – What is Selenium Grid?

Let us Try to understand about selenium grid from an example.
Example 1:
Consider a scenario where you want to execute 1000 test cases. You can do this by setting “thread-count” to suppose 5. So, here using threads your test cases will be executed. One thread will execute 200 test cases. So, time taken will be 1/5th of total time.
Now, let us say, you want to execute multiple test cases (or same test cases) on multiple operating systems and on multiple browsers.  Suppose 1000 test cases need to be executed on Windows OS, Linux OS (on Mozilla Firefox, chrome, safari, IE -> all 4 browsers). In this case, we can use Selenium Grid by setting various nodes on the hub machine.
Example 2:
There are so many web browsers which are in use today. Users may be using either Chrome or Firefox or Safari or Opera or even Internet Explorer for reading this very blog. They may be using different versions of the browsers. They may also be running these browsers on any OS, be it Windows or Mac or Ubuntu or any other distribution of Linux.
What if the users were seeing something important on the internet? Something important to your business. Wouldn’t you want to go that extra mile (testing the web app) just to ensure that your users and/ or customers get the best user experience? You would, right?
Well, you should!
But the question is, how feasible is it? Will you be able to spend time testing your web app on every possible OS and every available web browser? Hmm, that sounds like a bit of a challenge. This is where Selenium Grid fits the bill and helps save your time and energy.
Observe below diagram.
The above example shows, in Selenium Grid, we have a concept of Hub and Nodes. Hub is set on a machine from which you are trying to execute the test cases. On hub, we can set multiple nodes (suppose as like multiple Operating systems / multiple computes) and on these multiple nodes, various browsers can be set for test execution. All these things are virtually.
So, in short, Selenium-grid works on different machines against different browsers at the same time. Selenium grid support distributed test execution environment.
when to use selenium grid:
1.Run your tests against different browsers, operating systems, and machines all at the same time. This will ensure that the application you are testing is fully compatible with a wide range of browser- O.S combinations.
2.Save time in the execution of your test suites. If you set up Selenium Grid to run, say, 4 tests at a time, then you would be able to finish the whole suite around 4 times faster.
Grid concept is introduced to support parallel execution of test cases.
For Example, executing the same test case in different browsers in different operating systems.
Executing a set of test cases at an instance – This could be elaborated as below: if you are running a set of 30 test cases which consumes around 5 mins each, run time for the test suite will be 5*30(You have wait until first test cases execution is completed to start second test case execution) = 150 Mins. If the same is ran through GRID it triggers 30 instance of test cases at once, so 5*1 = 5 Mins (All the test cases will run at once as 30 different browser instances will be opened).
Advantages of using selenium Grid – 
         1.       It allows running test cases in parallel with different browsers.
         2.       It reduces script execution time.
         3.       It provides the flexibility to run your test automation scripts on different browsers and different OS at the same time.