Wednesday 5 August 2015

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

    package test.example;

    import java.net.HttpURLConnection;
    import java.net.URL;
    import org.openqa.selenium.server.RemoteControlConfiguration;
    import org.openqa.selenium.server.SeleniumServer;
    import org.testng.annotations.AfterClass;
    import org.testng.annotations.AfterSuite;
    import org.testng.annotations.BeforeClass;
    import org.testng.annotations.BeforeSuite;
    import com.thoughtworks.selenium.DefaultSelenium;
    import com.thoughtworks.selenium.Selenium;

    /**
     * This class contains methods to control selenium server
     *
     */

    public class ServerControl {

        protected Selenium selenium;
        protected SeleniumServer seleniumServer;
        private static String host = "localhost";
        private static int port = 4444;


        @BeforeSuite
        public void startSeleniumServer() {
            if (!isSeleniumServerRunning()) {
                try {
                    RemoteControlConfiguration rc = new RemoteControlConfiguration();
                   seleniumServer = new SeleniumServer(rc);
                    seleniumServer.start();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }

        @BeforeClass
        public void startSelenium() {
           selenium = new DefaultSelenium("localhost", 4444, "*firefox", "http://www.google.com");
           selenium.start();
            selenium.windowMaximize();
        }
     
        @AfterClass
        public void stopSelenium() {
            selenium.stop();
        }


        @AfterSuite
        public void stopSeleniumServer() throws Exception {
           seleniumServer.stop();
        }


        /**
         * Check whether the selenium server in the given port is running or not
         * @return
         */
       public static boolean isSeleniumServerRunning() {
            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());
            }
            return false;
        }
    }

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.

No comments:

Post a Comment