Search code examples
javaseleniumremote-serverselenium3

Selenium using Java: Start Hub Server and register Nodes with Hub


I am new to the selenium and I have been trying to start Selenium Hub and adding the node to the Hub using java. I have started it using command prompt but I want it to happen on its own. I am able to start the Hub but have no Idea how to add the Node.

Following is my code to start the Hub:

import org.openqa.grid.internal.utils.configuration.GridHubConfiguration;
import org.openqa.grid.internal.utils.configuration.GridNodeConfiguration;
import org.openqa.grid.web.Hub;

public class StartHub {

    public static void main(String[] args) throws Exception {
        // TODO Auto-generated method stub
        GridHubConfiguration configHub = new GridHubConfiguration();
        configHub.host = "localhost";
        configHub.port = 8060;

        //GridNodeConfiguration configNode = new GridNodeConfiguration();
        //configNode.host = configHub.host;
        //configNode.port = 5300;

        Hub hub = new Hub(configHub);
        //configNode.getHubHost();
        hub.start();

        //System.out.println(configNode.getRemoteHost());

        //hub.stop();
    }

}

How to add a Node going forward ?


Solution


  • Selenium-Grid Server Version 2.x.x configuration.

    Their Packages list

    « org.openqa.grid.internal.utils.GridHubConfiguration;
    « org.openqa.selenium.server.SeleniumServer;
    

    HUB Grid HUB Running as a standalone server: Hub is a central point which distributing tests on several node-machines, which are registered to it for parallel execution.

    Use GRID HUB Server:

    • To distribute your tests over multiple machines or virtual machines (VMs).
    • You want to connect to a remote machine that has a particular browser version that is not on your current machine.

    Start HUB Server

    public class Grid_HUBServer {
    
        public static String HUBIP = GRIDINFO.HOSTIP.toString();
        public static Integer HUBPORT = 4444;
        static String separator = "\n------------------------\n";
    
        public static void hubServer() {
            try {
                GridHubConfiguration gridHubConfig = new GridHubConfiguration();
                gridHubConfig.setHost( HUBIP );
                gridHubConfig.setPort( HUBPORT );
                gridHubConfig.setNewSessionWaitTimeout( 50000 );
                File JOSNFile = new File(System.getProperty("user.dir")+"/gridHub.json" );
                gridHubConfig.loadFromJSON( JOSNFile.toString() );
    
    
                Hub hub = new Hub(gridHubConfig);
                hub.start();
    
                System.out.println("Nodes should register to " + hub.getRegistrationURL());
                System.out.format("%s Running as a grid hub: %s\n" +
                        "Console URL : %s/grid/console \n", separator,separator,hub.getUrl());
    
                System.out.println("Please Enter to stop service.");
                System.in.read();
                System.in.read();
    
                hub.stop();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    
        public static void main(String[] args) {
            hubServer();
        }
    }
    

    NODE [RC] has to post a request to HUB in order to register with it, by this URL - IP:Port//grid/register.

    public class Grid_NODEServer extends Grid_HUBServer {
        static Integer NODEPORT = 5555;
        static String PROXYCLASS = "org.openqa.grid.selenium.proxy.DefaultRemoteProxy";
    
        public static void nodeServer() {
            try {
                System.setProperty(ChromeDriverService.CHROME_DRIVER_EXE_PROPERTY, CHROME_DRIVER_EXE );
                System.setProperty(InternetExplorerDriverService.IE_DRIVER_EXE_PROPERTY, IE_DRIVER_EXE);
    
    
                RegistrationRequest request = new RegistrationRequest();
                request.setRole( GridRole.NODE );
                File JOSNFile = new File(System.getProperty("user.dir")+"/registerNode.json" );
                request.loadFromJSON( JOSNFile.toString() );
    
                Map<String, Object> configuration = new HashMap<String, Object>();
    
                URL remoteURL = new URL(String.format("http://%s:%d", HUBIP, HUBPORT) );
                System.out.println("Remote URL : "+remoteURL);
                configuration.put(RegistrationRequest.AUTO_REGISTER, true);
                configuration.put(RegistrationRequest.HUB_HOST, HUBIP);
                configuration.put(RegistrationRequest.HUB_PORT, HUBPORT);
                configuration.put(RegistrationRequest.REMOTE_HOST, remoteURL);
    
                configuration.put(RegistrationRequest.PORT, NODEPORT);
    
                configuration.put(RegistrationRequest.PROXY_CLASS, PROXYCLASS);
                configuration.put(RegistrationRequest.MAX_SESSION, 1);
                //configuration.put(RegistrationRequest.CLEAN_UP_CYCLE, 2000);
                //configuration.put(RegistrationRequest.MAX_INSTANCES, 1);
    
                request.setConfiguration( configuration );
                GridNodeServer node = new SeleniumServer( request.getConfiguration() );
    
                SelfRegisteringRemote remote = new SelfRegisteringRemote( request );
                remote.setRemoteServer( node );
                remote.startRemoteServer();
                System.out.format("Selenium Grid node is up and ready to register to the hub " +
                        "%s Running as a grid node: %s ", separator,separator);
                remote.startRegistrationProcess();
    
                System.out.println("Please Enter to stop service.");
                System.in.read();
                System.in.read();
    
                remote.stopRemoteServer();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    
        public static void main(String[] args) {
            nodeServer();
        }
    }
    

    Selenium-Grid - JSON Configuration Files for HUB & NODE are gridHub.json, registerNode.json to instantiate.

    {
      "host": null,
      "port": 4444,
      "newSessionWaitTimeout": -1,
      "servlets" : [],
      "prioritizer": null,
      "capabilityMatcher": "org.openqa.grid.internal.utils.DefaultCapabilityMatcher",
      "throwOnCapabilityNotPresent": true,
      "nodePolling": 5000,
    
      "cleanUpCycle": 5000,
      "timeout": 300000,
      "browserTimeout": 1800000,
      "maxSession": 5,
      "jettyMaxThreads":-1
    }
    
    {
      "capabilities":
          [
            {
              "browserName": "firefox",
              "version": "39.0",
              "firefox_binary": "C:\\Program Files (x86)\\Mozilla Firefox\\firefox.exe",
              "maxInstances": 5,
              "seleniumProtocol": "WebDriver"
            },
            {
              "browserName": "chrome",
              "version": "54.0",
              "maxInstances": 5,
              "seleniumProtocol": "WebDriver"
            },
            {
              "browserName": "internet explorer",
              "version": "11",
              "maxInstances": 1,
              "seleniumProtocol": "WebDriver"
            }
          ],
      "configuration":
      {
        "proxy": "org.openqa.grid.selenium.proxy.DefaultRemoteProxy",
        "role": "node",
        "host": "127.0.0.1",
        "port": 5555,
    
        "maxSession": 5,
        "cleanUpCycle":2000,
        "registerCycle": 10000,
        "nodeTimeout":120,
        "nodePolling":2000,
    
        "register": true,
        "hubPort": 4444,
        "hubHost": "127.0.0.1"
      }
    }
    

    Selenium-Grid Server Version 3.x.x (>= beta4) configuration.

    Their packages list - (org.seleniumhq.jetty9.server.Server)

    « org.openqa.grid.internal.utils.configuration.GridHubConfiguration;
    « org.openqa.selenium.remote.server.SeleniumServer;
    « org.openqa.grid.internal.utils.configuration.GridNodeConfiguration;
    
    //  HUB Configuration - org.openqa.grid.internal.utils.configuration.GridHubConfiguration
    GridHubConfiguration gridHubConfig = new GridHubConfiguration();
    gridHubConfig.host = HUBIP;
    gridHubConfig.port = HUBPORT;
    gridHubConfig.newSessionWaitTimeout = 50000;
    File JOSNFile = new File(System.getProperty("user.dir")+"/gridHub.json" );
    gridHubConfig.loadFromJSON( JOSNFile.toString() );
    
    Hub hub = new Hub(gridHubConfig);
    hub.start();
    
    // NODE Configuration - org.openqa.selenium.remote.server.SeleniumServer
    GridNodeConfiguration configuration = new GridNodeConfiguration();
    
    File JOSNFile = new File(System.getProperty("user.dir")+"/registerNode2.json" );
    configuration.loadFromJSON( JOSNFile.toString() );
    configuration.role = GridRole.NODE.name();
    
    URL remoteURL = new URL(String.format("http://%s:%d", HUBIP, HUBPORT) );
    System.out.println("Remote URL : "+remoteURL);
    RegistrationRequest request = new RegistrationRequest( configuration );
    
    GridNodeServer node = new SeleniumServer( request.getConfiguration() );
    
    SelfRegisteringRemote remote = new SelfRegisteringRemote( request );
    remote.setRemoteServer( node );
    remote.startRemoteServer();
    remote.startRegistrationProcess();
    

    Use different JSON format to overcome the Exception - org.openqa.grid.common.exception.GridConfigurationException: Error with the JSON of the config : Deprecated -nodeConfig file encountered.

    registerNode2.json

    {
        "capabilities": [ ... ],
    
        "proxy": "org.openqa.grid.selenium.proxy.DefaultRemoteProxy",
        "role": "node",
        "host": "127.0.0.1",
        "port": 5555,
    
        "maxSession": 5,
        "cleanUpCycle":2000,
        "registerCycle": 5000,
        "nodePolling": 5000,
        "nodeStatusCheckTimeout": 5000,
        "unregisterIfStillDownAfter": 60000,
        "downPollingLimit": 2,
        "debug": false,
    
        "register": true,
        "hub": "http://localhost:4444",
        "servlets" : [],
        "withoutServlets": [],
        "custom": {}
    }