Launch Selenium Grid Hub And Nodes Using "JSON" Config File

What is JSON?
"JavaScript Object Notation" is full form of JSON and it is best way to store information in an organized, easy-to-access and easy to read and understand manner. Here we can use it to store selenium grid config parameters.

What is usage of JSON in selenium grid?
Well.. Answer is :  When we start selenium grid hub and nodes manually from command prompt, We need to set many different parameters for grid hub and nodes like port, browserName, maxInstances, platform, maxSession etc.. from command prompt. You will find all config parameter's example links on Selenium Grid tutorial page. Instead of feeding all config parameters directly from command prompt, We can store selenium grid hub and nodes config parameters in .json file and then can use that file to start hub and nodes with required configuration.

Let's learn it practically how to create .json files for selenium grid hub and node and then how to run them.

Scenario : Consider example of "maxSession" as described on Usage of maxSession. We wants to run 2 test cases on 2 different browsers(Firefox and Chrome) but max 2 sessions at a time using selenium grid.

Creating .json file for selenium grid hub
Follow the steps given bellow to create GridHub.json file.
  • Create new notepade(.txt) file.
  • Copy-paste bellow given code in it.
{
        "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
}
  • Save file with name GridHub.json in D: drive.
Creating .json file for selenium grid node

Follow the steps given bellow to create GridNode.json file.
  • Create new notepade(.txt) file.
  • Copy-paste bellow given code in it.
{
  "capabilities":
      [        
        {
          "browserName": "firefox",
          "maxInstances": 2,
   "acceptSslCerts": true,
     "javascriptEnabled": true,
   "platform": "WINDOWS",
          "seleniumProtocol": "WebDriver"
        },
        {
          "browserName": "chrome",
          "maxInstances": 2,
   "platform": "WINDOWS",
          "seleniumProtocol": "WebDriver"
        }        
      ],
  "configuration":
  {
    "proxy": "org.openqa.grid.selenium.proxy.DefaultRemoteProxy",
    "maxSession": 2,
    "port": 5555,
    "host": ip,
    "register": true,
    "registerCycle": 5000,
    "hubPort": 4444,
    "hubHost": ip
  }
}
  • Save file with name GridNode.json in D: drive.
Note : You can change config parameters like hubPort, maxSession, browserName, maxInstances etc.. in above given files as per your requirements.

Start hub and node using JSON files
I hope, selenium server standalone jar file and chromedriver exe file is already available in your D: drive. If they are not there then download both files and put in D: drive.

- Start grid hub
  • Open command prompt.
  • Navigate to D: drive and run bellow given command.
java -jar selenium-server-standalone-2.52.0.jar -role hub -hubConfig GridHub.json

Here, -hubConfig GridHub.json will load hub config parameters to start selenium grid hub.
It will start selenium grid hub.

- Start grid node
  • Open command prompt.
  • Navigate to D: drive and run bellow given command.
java -jar selenium-server-standalone-2.52.0.jar -role node -Dwebdriver.chrome.driver="D:/chromedriver.exe" -nodeConfig GridNode.json

Here, -nodeConfig GridNode.json will load node config parameters to start selenium grid hub.
It will start selenium grid node.

Now if you access http://localhost:4444/grid/console URL in browser, It will show you grid configuration with 2 firefox and 2 chrome browsers as bellow.

Now you are ready to run selenium webdriver test on selenium grid.

Create WebDriver test and run using grid
Create fillingForm.java, Calc.java and testng.xml files in your eclipse as given in maxSession Example and run test from testng.xml file. It will start running on grid.

This way you can use JSON file to launch selenoum grid hub and node configuration.

1 comment:

  1. Thanks Aravind

    I like what you did for all people as me who learnt almost parts of all needed from you.

    Mak

    ReplyDelete