The Selenium grid console fails to launch when attempting to start the Selenium Hub programmatically

While using Mac OS Sierra (10.12.5), I encountered an issue when starting the selenium server with a hub role from the command line:

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

Upon opening the grid console at http://localhost:4444/grid/console, the information would display within a couple of seconds.

However, when attempting to start the hub with a Java program, the same URL either did not load or took an exceptionally long time to do so:

Hub hub = null;
 public void startSeleniumHub(){
 try{
                String strIP = "localhost";

                GridHubConfiguration config = new GridHubConfiguration();

                config.host = strIP;
                config.port = 4444;

                hub = new Hub(config);
                hub.start();

                if(isSeleniumHubRunning(10)){
                    System.out.println("Selenium Grid is Running");
                }else{
                    System.err.println("*** Selenium Grid is down");
                }

 }catch(Exception e){
     e.printStackTrace();
 }
 }

 public boolean isSeleniumHubRunning(int timeOut){
     int count = 0 ;
     while(count < timeOut){
              try{    
                         Thread.sleep(1000);
                         URL u = new URL ( "http://localhost:4444/grid/console");
                         HttpURLConnection huc =  ( HttpURLConnection )  u.openConnection (); 
                         huc.setRequestMethod ("GET");  
                         huc.connect () ; 
                         int code = huc.getResponseCode() ;
                         System.out.println(code);
                         return true;
                 }catch(Exception e){
                 System.err.println("Selenium Grid is still down.....");         
                 count++;
                 } 
     }
     System.err.println("Selenium Grid failed to start up even after   " + timeOut + "  seconds");
     return false;
      }

I attempted to troubleshoot and identify the root cause but was unable to find a solution.

Thank you in advance for any assistance.

EDIT: The solution provided by krishnan-mahadevan only seems to work with Eclipse version 4.6.0 and not with IDEA Community Edition 2017.2. I will be posting a separate question regarding this specific issue.

Answer №1

In an effort to summarize all the information I have previously shared, I will be consolidating it as part of this Google forum thread, which also covers the same discussion with the OP.

  1. One issue on the Sierra OS is that at times it takes a prolonged duration to resolve the IP address for localhost. To resolve this, you may need to add the output of the hostname command to the /etc/hosts file and then attempt again. Refer to this Stack Overflow post for more detailed instructions.
  2. If you are operating on a corporate network with a proxy server between you and the internet, configuring the Proxy settings to bypass traffic intended for either localhost or the IP Address of your machine from
    Network Preferences > Advanced (Click "Advanced" button) > Proxies (tab) > Bypass proxy settings for these hosts & domains
    may be necessary.

After implementing both solutions mentioned above, you can try one of the suggested combinations to boot up and load the Grid console:

  1. Explicitly provide the host name as localhost (as your code currently does) and proceed to load the grid console via
    http://localhost:4444/grid/console
    (or)
  2. Repeat step (1) without setting the hostname, and load the URL as returned by hub.getUrl().

My suspicion is that you may be using a company-provided MAC system with a configured proxy server. When attempting to access the console URL in your browser, the traffic is initially routed to the Proxy server, causing delays as it tries to resolve the page without success, since your proxy does not recognize neither localhost nor the IP address of your machine (likely utilizing an internal IP address not exposed externally).

I hope this explanation proves helpful!

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

What is the process for generating an alert box with protractor?

While conducting tests, I am attempting to trigger an alert pop-up box when transitioning my environment from testing to production while running scripts in Protractor. Can someone assist me with this? ...