Cross Browser Testing Using Selenium Grid: Selenium Tutorial 29

We are now close to the end of this comprehensive Selenium tutorials series. Next week, we will conclude this online Selenium Training series with “effort estimation of Selenium Projects” and “Selenium Interview questions and answers” tutorials.

Today, in this tutorial we will introduce you with Selenium Grid –  a distributed test execution environment to speed up the execution of a test pass. Learn how to perform Cross browser testing using Selenium Grid.

What You Will Learn:

What is need of Selenium Grid?

As you go through entire Selenium WebDriver tutorials you will find out WebDriver will execute your test cases on a single machine.

Here are few problems with such a setup:

  1. What if you want to execute your test cases for different Operating Systems?
  2. How to run your test cases in the different version of the same browser?
  3. How to run your test cases in multiple browsers?
  4. Why should a scenario wait for execution of other test cases even if it does not depend upon any test cases?

All these problems are addressed in Selenium GRID.

As we proceed with the Selenium course, we will get the idea about how we can overcome these problems. Basically, Grid architecture is based on master-slave architecture. Master machine distributes test cases to different slave machines.

There are 2 versions of Grid available. Selenium Grid 2.0 is the latest from Selenium. Selenium 1.0 was the earlier version. Most of the Selenium experts prefer using Selenium Grid 2.0 as it is packed with new features. Selenium Grid 2.0 supports both Selenium RC and Selenium WebDriver scripts.

Benefits of Selenium Grid:

  1. Selenium Grid gives the flexibility to distribute your test cases for execution.
  2. Reduces batch processing time.
  3. Can perform multi-browser testing.
  4. Can perform multi-OS testing.

Basic terminology of Selenium Grid:

Hub: Hub is the central point to the entire GRID Architecture which receives all requests. There is only one hub in the selenium grid. Hub distributes the test cases across each node.

Node: There can be multiple nodes in Grid. Tests will run in nodes. Each node communicates with the Hub and performs test assigned to it.

Install Selenium GRID

Step 1: Download Selenium Server jar file from Selenium’s official website which is formerly known as Selenium RC Server and save it at any location on the local disk.

URL of selenium HQ: http://www.seleniumhq.org/download/

Step 2: Open the command prompt and navigate to a folder where the server is located. Run the server by using below command

java -jar selenium-server-standalone-2.41.0.jar -role hub

The hub will use the port 4444 by default. This port can be changed by passing the different port number in command prompt provided the port is open and has not been assigned a task.

Status can be checked by using the web interface: http://localhost:4444/grid/console

Step 3: Go to the other machine where you intend to setup Nodes. Open the command prompt and run the below line.


java -jar selenium-server-standalone-2.41.0.jar -role node -hub
http://localhost:4444/grid/register -port 5556

Run the selenium server in other machines to start nodes.

Browser and Nodes:

After starting hub and nodes on each machine when you will navigate to GRID Console

You will find 5 Chrome, 5 Firefox and 1 IE browser under Browser section like below.

This indicates that by default you can use 5 Chrome, 5 Firefox and 1 IE browser.

For Instance, if you want to use only IE you can start the node by using below command:


java -jar selenium-server-standalone-2.41.0.jar -role webdriver -hub
http://localhost:4444/grid/register -port 5556 -browser browserName=iexplore

Verify the browser Type along with other details in GRID Console by clicking on view config.

Similarly for Firefox:


java -jar selenium-server-standalone-2.41.0.jar -role webdriver -hub
http://localhost:4444/grid/register -port 5556 -browser browserName=firefox

For Chrome:


java -jar selenium-server-standalone-2.41.0.jar -role webdriver -hub
http://localhost:4444/grid/register -port 5556 -browser browserName=chrome

There are few scenarios where you may need the browser from each type i.e.: IE, Chrome and Firefox.

For instance, you may need to use 1 IE and 1 Firefox and 1 Chrome browser


java -jar selenium-server-standalone-2.41.0.jar -role webdriver -hub
http://localhost:4444/grid/register -port 5556 -browser browserName=iexplore
-browser browserName=firefox -browser browserName=chrome

maxInstances:

maxInstance is used to limit the number of browser initialization in a node.

For example, if you want to work with 2 Firefox and 2 IE then you can start the node using maxInstance.


java -jar selenium-server-standalone-2.41.0.jar -role webdriver -hub
http://localhost:4444/grid/register -port 5556 -browser browserName=firefox,maxInstance=3

Maximum instance can be verified under configuration tab.



Similarly, other browser instances can be configured using maxInstances.

maxSession:

maxSession is used to configure how many numbers of browsers can be used parallel in the remote system.


java -jar selenium-server-standalone-2.41.0.jar -role webdriver -hub
http://localhost:4444/grid/register -port 5556 -browser browserName=chrome,maxInstance=3
-browser browserName=firefox,maxInstance=3 –maxSession 3

Similarly, you can start multiple nodes and configuration can be verified in the console.

NODE1:

NODE2:

Sample Grid Code:

Here I have used TestNG to run a sample GRID test case.

Prerequisite: Create Hub and nodes as explained earlier and TestNG should be configured in eclipse.

Here I have taken sample test to login to Gmail and enter username and password


public class GridExample {
          @Test
          public void mailTest() throws MalformedURLException{
                   DesiredCapabilities dr=null;
                   if(browserType.equals("firefox")){
                   dr=DesiredCapabilities.firefox();
                   dr.setBrowserName("firefox");
                   dr.setPlatform(Platform.WINDOWS);
                   }else{
                            dr=DesiredCapabilities.internetExplorer();
                            dr.setBrowserName("iexplore");
                            dr.setPlatform(Platform.WINDOWS);
                   }
                   RemoteWebDriver driver=new RemoteWebDriver(new     URL("http://localhost:4444/wd/hub"), dr);
                   driver.navigate().to("http://gmail.com");
                   driver.findElement(By.xpath("//input[@id='Email']")) .sendKeys("username");
                   driver.findElement(By.xpath("//input[@id='Passwd']")) .sendKeys("password");
                   driver.close();
}

As in the example, you have to use RemoteWebDriver if you are using GRID and you have to provide capabilities to the browser. You have to set the browser and platform as above.

In this example, I have used the platform as WINDOWS. You can use any platform as per your requirement.

Version of browser can also be set by using dr.setVersion(“version”)

For Instance, you need to run this test serially in multiple browsers you have to configure your testng.xml.Below is the testng.XML suite for above test to run your test serially.


<?xml version="1.0" encoding="UTF-8"?>
<suite name="GRID SAMPLE TEST" thread-count="2">
    <test name="GRID TEST WITH SERIAL EXECTION WITH BROWSER IE">
    <parameter name ="browserType" value="IE"/>
        <classes>
            <class name ="GridExample"/>
        </classes>
    </test>

    <test name="GRID TEST WITH SERIAL EXECTION WITH BROWSER FF ">
    <parameter name ="browserType" value="firefox"/>
        <classes>
            <class name ="GridExample"/>
        </classes>
    </test>
</suite>

To run the test parallel, you have to change your testng.xml like below.


<?xml version="1.0" encoding="UTF-8"?>
<suite name="GRID SAMPLE TEST" parllel="tests" thread-count="3">
    <test name="GRID TEST WITH SERIAL EXECTION WITH BROWSER FF">
    <parameter name ="browserType" value="firefox"/>
        <classes>
            <class name ="GridExample"/>
        </classes>
    </test>
    <test name="GRID TEST WITH SERIAL EXECTION WITH BROWSER IE">
    <parameter name ="browserType" value="IE"/>
        <classes>
            <class name ="GridExample"/>
        </classes>
    </test>
</suite>

Here in the testng.XML you have to specify the parameter as parllel=“tests” and thread-count=“3” describes the maximum number of threads to be executed in parallel.

Configuration using JSON file:

The grid can also be launched along with its configuration by using JSON configuration file.

Create a JSON file for having below configuration. Here I have created a JSON file named as grid_hub.json


{
  "host": null,
  "port": 4444,
  "newSessionWaitTimeout": -1,
  "servlets" : [],
  "prioritizer": null,
  "capabilityMatcher":   "org.openqa.grid.internal.utils.DefaultCapabilityMatcher",
  "throwOnCapabilityNotPresent": true,
  "nodePolling": 5000,

  "cleanUpCycle": 5000,
  "timeout": 300000,
  "maxSession": 5
}

Start the hub using below command

java -jar selenium-server-standalone-2.41.0.jar -role hub –hubConfig grid_hub.json

Similarly, create different json file for different nodes as per required configuration.

Here is an example of JSON configuration file for node named as grid_node.json


{
  "capabilities":
      [
        {
          "browserName": "chrome",
          "maxInstances": 2
        },
        {
          "browserName": "firefox",
          "maxInstances": 2
        },
        {
          "browserName": "internet explorer",
          "maxInstances": 1
        }
      ],
    "configuration":
        {
        "nodeTimeout":120,
        "port":5555,

        "hubPort":4444,
        "hubHost":"localhost",

        "nodePolling":2000,

        "registerCycle":10000,
        "register":true,
        "cleanUpCycle":2000,
        "timeout":30000,
        "maxSession":5,
        }
}

To start the node

java -jar selenium-server-standalone-2.41.0.jar -role rc –nodeConfig grid_node.json

You can change all the configuration of a browser, maxInstances, port, maxSession etc. in the JSON file.

You can provide browser version, platform in the JSON config file like below

{

   “browserName”: “chrome”,”version”:”8”,”platform”:”Windows”

}

Conclusion:

It is advisable to use Selenium Grid when you have to perform multi-browser testing and you have a large number of test cases.

In this module, we covered how to setup Grid hub and nodes along with how to run Grid test cases using testng.XML and JSON file.

Next Tutorial #30: Automation testing with Selenium and Cucumber tool. Cucumber is a BDD testing tool and Framework. We will learn features of Cucumber tool and its usage in real time scenario including how to integrate Selenium WebDriver with Cucumber.

Please post your queries related to Selenium Grid in comments below.