Saturday, September 22, 2012

Check if selenium server is running in background

When we run selenium RC test cases sometimes we faces this issue saying "java.net.BindException: Selenium is already running on port 4444. Or some other service is.
When you check the port 4444 no service is running. We change the port and run the program even that too is not working. 
In these cases we need to shutdown the selenium server on this port.
Use below command to shut down the server.

http://localhost:4444/selenium-server/driver/?cmd=shutDownSeleniumServer

If selenium server is already running on port 4444 then it will shut down the server and says
OKOK
if selenium is not running on this port 4444 then by hitting above url will give you 
"Unable to connect"
Now you can run your test cases i am sure will run smoothing.

You can also do an alternative way to handle this i.e you can and some code to check whether the selenium server in a specific port is running or not before starting the server like below:

//ServerControl.java
  1. package test.example;
  2. import java.net.HttpURLConnection;
  3. import java.net.URL;
  4. import org.openqa.selenium.server.RemoteControlConfiguration;
  5. import org.openqa.selenium.server.SeleniumServer;
  6. import org.testng.annotations.AfterClass;
  7. import org.testng.annotations.AfterSuite;
  8. import org.testng.annotations.BeforeClass;
  9. import org.testng.annotations.BeforeSuite;
  10. import com.thoughtworks.selenium.DefaultSelenium;
  11. import com.thoughtworks.selenium.Selenium;
  12. /**
  13.  * This class contains methods to control selenium server
  14.  *
  15.  * @author aksahu
  16.  */
  17. public class ServerControl {
  18.     protected Selenium selenium;
  19.     protected SeleniumServer seleniumServer;
  20.     private static String host = "localhost";
  21.     private static int port = 4444;
  22.     @BeforeSuite
  23.     public void startSeleniumServer() {
  24.         if (!isSeleniumServerRunning()) {
  25.             try {
  26.                 RemoteControlConfiguration rc = new RemoteControlConfiguration();
  27.                 seleniumServer = new SeleniumServer(rc);
  28.                 seleniumServer.start();
  29.             } catch (Exception e) {
  30.                 e.printStackTrace();
  31.             }
  32.         }
  33.     }
  34.     @BeforeClass
  35.     public void startSelenium() {
  36.         selenium = new DefaultSelenium("localhost"4444"*firefox""http://www.google.com");
  37.         selenium.start();
  38.         selenium.windowMaximize();
  39.     }
  40.    
  41.     @AfterClass
  42.     public void stopSelenium() {
  43.         selenium.stop();
  44.     }
  45.     @AfterSuite
  46.     public void stopSeleniumServer() throws Exception {
  47.         seleniumServer.stop();
  48.     }
  49.     /**
  50.      * Check whether the selenium server in the given port is running or not
  51.      * @return
  52.      */
  53.     public static boolean isSeleniumServerRunning() {
  54.         try {
  55.             String baseUrl = "http://" + host + ":" + port;
  56.             System.out.println("Checking selenium server status [" + baseUrl + "]");
  57.             URL url = new URL(baseUrl + "/selenium-server/driver/?cmd=testComplete");
  58.             HttpURLConnection connection = (HttpURLConnection) url.openConnection();
  59.             if (connection.getResponseCode() == HttpURLConnection.HTTP_OK)
  60.                 return true;
  61.         } catch (Exception e) {
  62.             System.err.println("Could not check selenium server status: " + e.getMessage());
  63.         }
  64.         return false;
  65.     }
  66. }

In the above the method isSeleniumServerRunning() returns a boolean value true if the server is running in the given port and host and returns false otherwise.

1 comment:

  1. Hi!

    Do you have a similar code for Selenium 3 please?

    Thank you!

    ReplyDelete