Selenium-Grid is used to run parallel executions in multiple machines at the same time, and to run Selenium WebDriver tests in parallel, we need to set up the Selenium Grid Server as a hub.
Hub will provide the available capabilities to the webdriver and the registered nodes will connect to hub for parallel execution. Selenium Grid supports multiple operating systems like Windows, Mac, Linux and iOS. It also supports several browsers such as Chrome, IE, Firefox, Opera, Safari, and Android.
The Hub acts as a bridge, which receives the test execution request and distributes it to the available nodes or keeps them in queue until any stop sending or receiving requests from hub.
To Exemplify, if you have a suite of 11 tests, but you set up Selenium-Grid to support 10 different machines (VMs or separate physical machines) to run those tests, it will distribute maximum 10 test suites in 10 different nodes; however, it will keep 1 node in queue for execution.
For large test suites and to verify large amounts of data-validation in lesser time, Selenium Grid is the best approach to save time and human efforts. In practicing agile software development, results can be shared by overnight execution of test suite.
Steps to set up the Selenium Grid
1. Install java from https://java.com/en/download/manual_java7.jsp
Verify “java -version” from the command line
IMG1
2. Download the latest Selenium Server standalone JAR file from http://www.seleniumhq.org/download/
Hub Configuration
Create a batch file to run the hub (open a new text document, put the below code lines in it and save it as a .bat extension and then launch the hub batch file.
IMG2Note: Set the path for Selenium-Server-Standalone jar file and run the batch file to launch hub.
This will start the hub server through command prompt and starts listening to node with default port 4444.
IMG3To access grid console; open any browser and access link – http://localhost:4444/grid/console or http://:4444/grid/console. It will display the following information in the browser:
Selenium GridTo view the node config details, click on “view config” link as displayed in above grid console snapshot.
Node Configuration
Node Configuration using Batch File:
Create a batch file to run the node (open a new text document, put the below code lines in it and save it as a .bat extension and then launch the node batch file.
In below snapshot we have registered a node in Windows machine for browser – Internet Explorer 11.
Image4For Instance, below we have created 2 more nodes registering with hub which includes Firefox and Chrome browsers with their respective batch files snapshot in local repository.
IENode FFNode ChromeNode IMG6Post to completion of all nodes configuration, we can view config in grid console as displayed in below screenshot.
Grid-consoleNode Configuration using JSON File:
For Windows
  • Create a notepad file with all below listed configuration details and save it as *.json.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
{
"capabilities":
                [
                {
           "platform": "WINDOWS",
                                "browserName": "chrome",
                                "maxInstances": 3,
                                "seleniumProtocol": "WebDriver"
                },
                {
            "platform": "WINDOWS",
                                "browserName": "firefox",
                                "maxInstances": 2,
                                "seleniumProtocol": "WebDriver"
                },
                {
            "platform": "WINDOWS",
                                "browserName": "internet explorer",
                                "maxInstances": 1,
                                "seleniumProtocol": "WebDriver"
                }
                ],
  "configuration":
                {
                                "cleanUpCycle": 5000,
        "registerCycle": 5000,
        "nodeTimeout": 120,
                            "timeout": 600000,
        "nodePolling": 2000,
        "hub": "http://localhost:4444/grid/register",
                          "proxy": "org.openqa.grid.selenium.proxy.DefaultRemoteProxy",
                          "maxSession": 10,
                           "port": 5555,
                            "host": "localhost",
                            "register": true,
                           "hubPort": 4444,
                            "hubHost" :"localhost",
           "role": "node"
                }
}
Now create batch file for windows and give path for above json file so that it can execute through json.
Img7Here is the view of grid config file:
 Selenium Grid

Configure Android Node in Windows
Download Android SDK for Windows from https://developer.android.com/sdk/index.html?hl=i
Download Brackets (adb bundles) Packages from http://brackets.io/
* Set ANDROID_HOME= Android PATH (D:\adt-bundle) in Environment Variables.
* Set Path for platforms, platform-tools and tools (D:\adt-bundle\…).
NOTE:
* Configuration done only for Android Chrome browser.
* Enter paths for all above folders separately in Environment Variables.
* Connect device with machine and open terminal.
* Enter adb devices (Use this command to connect android device).
Selenium Grid* Set the capabilities and pass some parameters which identify the capability for android-chrome. For instance: ‘version’ (We can put any string value for version).
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
case "windows-android-chrome":
            case "windows-android_chrome":
            case "mac-android-chrome":
            case "mac-android_chrome":
                objCapabilities = null;
                objCapabilities = DesiredCapabilities.chrome();
                chromeoptions = new ChromeOptions();               
                chromeoptions.setExperimentalOption("androidPackage", "com.android.chrome");
                objCapabilities.setCapability(ChromeOptions.CAPABILITY, chromeoptions);
                objCapabilities.setCapability("platform", "WINDOWS");
                objCapabilities.setCapability("browserName", "chrome");
                objCapabilities.setCapability("version", "android");
 
                if(osname.toLowerCase().contains("windows")){
                    ChromeDriverPath = System.getProperty("user.dir") + "/Tool/chromedriver.exe";
                }else{
                    ChromeDriverPath = System.getProperty("user.dir") + "/Tool/chromedriver";
                }
                System.setProperty("webdriver.chrome.driver", ChromeDriverPath);
To view the Selenium-Grid Console for android-chrome with version value as mentioned in previous step.
Selenium GridiOS
Appium_iPad_Simulator
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
{
    "capabilities": [
        {
            "browserName": "iPad-Simulator",
            "version": "7.1",
            "maxInstances": 1,
            "platform": "MAC"
        }
    ],
    "configuration": {
        "cleanUpCycle": 2000,
        "timeout": 300000,
       "browserTimeout": 60000,
        "hub": "http://localhost:4444/grid/register",
        "host": " localhost ",
        "maxSession": 1,
        "port": 4730,
        "hubPort": 4444,
        "hubHost": " localhost ",
        "url": "http:// localhost:4730/wd/hub",       
        "register": true,
       "registerCycle": 5000,
        "role": "node"
    }
}
Command file used to execute iPad in Mac Machine through JSON
1
2
3
#!bin/sh
cd /Users/admin/Desktop/Mindoro_Automation
appium -p 4730 --nodeconfig /Users/admin/Desktop/Mindoro_Automation/appiumgrid_iPad-Simulator1.json
Appium_iPhone_Simulator
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
{
    "capabilities": [
        {
            "browserName": "iPhone-Simulator",
            "version": "7.1",
            "maxInstances": 1,
            "platform": "MAC"
        }
    ],
    "configuration": {
        "cleanUpCycle": 2000,
        "timeout": 300000,
      "browserTimeout": 60000,
        "hub": "http://localhost:4444/grid/register",
        "host": " localhost ",
        "maxSession": 1,
        "port": 4729,
        "hubPort": 4444,
        "hubHost": " localhost ",
        "url": "http:// localhost:4729/wd/hub",       
       "register": true,
       "registerCycle": 5000,
        "role": "node"
    }
}
Command file used to execute iPhone in Mac Machine through JSON
1
2
3
#!/bin/sh
cd /Users/admin/Desktop/Mindoro_Automation
appium -p 4729 -- nodeconfig /Users/admin/Desktop/Mindoro_Automation/appiumgrid_iPhone-Simulator1.json
Safari
NOTE: If safari does not launch through grid or throws an error then might we required a safari plugin from selenium-server-standalone jar file and import it manually in safari browser.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
{
"capabilities":
                [
        {
            "platform": "MAC",
                                "browser": "safari",
                                "maxInstances": 1,
                                "seleniumProtocol": "WebDriver"
                }
                ],
  "configuration":
                {
                                "timeout": 300000,
              "hub": "http:// localhost:4444/grid/register",
                                "proxy": "org.openqa.grid.selenium.proxy.DefaultRemoteProxy",
                                "maxSession": 1,
                                "port": 5557,
                                "host": " localhost ",
                                "register": true,
                                "hubPort": 4444,
                                "hubHost" : "localhost",
              "role": "node"
                }
}
Command file used to register Safari through terminal in Mac
1
2
#!/bin/sh
cd java -jar selenium-server-standalone-2.42.0.jar -role node –nodeConfig //selenium-node-macSafari.cfg.json
NOTE: Once all nodes get configured and registered with hub, it displays as below snapshot in grid console.
Grid-console-2
Terminologies used in Grid Configuration
role = When launching a node, it will forward the parameter to server on the node like, -role node.
host = Though it not needed and determined automatically. For some network configuration, network with VPN, specifying the host might be necessary.
port = the port the remote/hub will listen on. Default to 4444.
throwIn Capability Not Present = Default value is true; if no proxy is currently registered hub will reject the test request. On contrast, the request queued until a node supporting the capability which is added to the grid.
new Session Wait_Timeout = Default to no timeout ( -1 )ms after which a new test waiting for a node to become available will timeout else test will throw an exception before starting a browser.
hubConfig = It defines the hub properties in JSON format.
nodeConfig = It defines the node properties in JSON format.
cleanupCycle = It will check the timeout thread in ms.
node Timeout = The timeout in seconds prior to hub automatically ends the test so that browser will be released for another test to run.
browser Timeout= The browser gets hang after defined timeout in ms.
hub = It used to post the registration request using url – <http://localhost:4444/grid/register>
proxy =This will be used to represent the node. By default org.openqa.grid.selenium.proxy.DefaultRemoteProxy.
maxSession = Maximum number of sessions to run multiple tests at same time independently in different browsers.
register Cycle = It defines how often registered node will try to register itself again in ms,however, without restarting the node command file.
nodePolling = How often the hub checks if the node is still alive in ms.