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.

How to download Selenium ?

Download Selenium WebDriver Jars and configuring in eclipse.
In order to start with Automation testing using selenium WebDriver, we need below things –

1. Installed JDK or JRE on your machine.

           2. Installed Eclipse / NetBeans / or any other IDE
As we know, java is not a tool but it is a collection of jars which we need to use in eclipse or any other IDE. Here we will see how can this be achieved.

Step 1: Download Selenium WebDriver jar files.

Use below URL to download Selenium WebDriver jar files.
To Download Latest version, use this URL –   http://www.seleniumhq.org/download/
Once Selenium WebDriver jar has been downloaded, extract them to some folder. Now we will see how to configure these jar files in to our project.

Step 2: Create Java Project

Create Java project in eclipse so that we can configure selenium jars in to java project, which can be used for creating test cases.
Below steps explain how to create java project.
Click on File -> New -> Other
In select wizard, type Java, then select “Java project” from the list.
Give the desired project name & click on finish.
Now, Once the project is created, we need to create Package.
Right click on project that you have created.  Then go to New -> Package
Enter proper package name -> then click on Finish.
Now, once package is created, we need to create a class  where we can write our automation test cases.
Right click on Package, -> New -> class
Give some class name, then click on finish.
That’s it, now we have created a project along with package and class within it.

Step 3: Configure Selenium jar files in java project

Since now, we have created java project, package & class within it, its time to configure selenium jars within project. Below steps explains how this can be done.
Right click on the project -> Build Path -> Configure Build path
Go to Libraries, Click on Add External Jars -> Select the selenium jars (which you downloaded. Mentioned in step 1)
Click on Apply and OK
This is all done. To verify if your jars are added in to project, Check Referenced library in your project. It should contain all the jar files which you have added.
All set now, you are good to begin with your first test case using selenium
Hope this helps. Drop your comments.

First Program in Selenium WebDriver to test www.google.com

Once we have configured Selenium jar files in to Eclipse (refer post: Downloading Selenium WebDriver Jars and configuring in eclipse), We are now good to start with Basic programs using Selenium.
We write the java program for selenium test in a java class. So, let us create one java class.
Right click on Package -> New -> Class (If you don’t find Class over there, then go to Other, and search for Java class there)
Give some meaningful name to the class and click on Finish.
The first basic thing is, we need to create driver of the web browser which will drive the flow.
Here I am using Mozilla Firefox driver.
Below is the sample code for my first selenium test where I am navigating to www.google.com and finding out the Title of the page.
Once the code is written in a class file, right click on it, select Run AS – > Java Application
Sample code – 

package basicsOfSelenium;


import org.openqa.selenium.WebDriver;

import org.openqa.selenium.firefox.FirefoxDriver;

public class FirstSeleniumTest {

public static void main(String[] args) {

WebDriver driver = new FirefoxDriver();

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

String TitleOfPage = driver.getTitle();

System.out.println("Title of Current page is : "+TitleOfPage);

driver.close();
}

}

Sample Output:

Title of Current page is : Google
Good luck for your beginning

Do post your comments for any questions / suggestions.