Thursday, April 19, 2012

Selenium: Check if Selenium Server is running

If you want to check whether selenium server is running or not, hit the url "http://host:port/selenium-server/driver/?cmd=testComplete", If response is returned that means, server is running on the given host:port. Here is the Java code for this:


import java.net.HttpURLConnection;
import java.net.URL;

public static boolean isSeleniumServerRunning(String host, int port) 
  {
   try {
    String baseUrl = "http://" + host + ":" + port;
    System.out.println("Checking selenium server status [" + baseUrl + "]");
    URL url = new URL(baseUrl + "/selenium-server/driver/?cmd=testComplete");
    HttpURLConnection connection = (HttpURLConnection) url.openConnection();
    
    if(connection.getResponseCode() == HttpURLConnection.HTTP_OK)
     return true;
  } catch (Exception e) {
   System.err.println("Could not check selenium server status: " + e.getMessage());
   e.printStackTrace();
  }   
  return false;
  }

No comments: