How to Start Selenium Server with Java Code

When test automating a web application using Selenium, we have to start the Selenium server first, so that a new Selenium session is created to talk to the web browser. This can be either done manually, i.e. user running a command line to start the Selenium server, or to get the pure automation effect of Selenium, it is best to start the Selenium server via a program code. The code below is written in Java and starts the Selenium server when called. Normally this would be the first action within your main() function.

package com.company;
import org.openqa.selenium.server.RemoteControlConfiguration;
import org.openqa.selenium.server.SeleniumServer;
import com.thoughtworks.selenium.Selenium;
import java.net.BindException;
import java.net.ServerSocket;

public class Server {

    public static SeleniumServer server;
    public static void startSeleniumServer() throws Exception {

        try {
            ServerSocket serverSocket = new ServerSocket(RemoteControlConfiguration.DEFAULT_PORT);
            serverSocket.close();
//Server not up, start it
            try {
                RemoteControlConfiguration rcc = new RemoteControlConfiguration();
                rcc.setPort(RemoteControlConfiguration.DEFAULT_PORT);
                server = new SeleniumServer(false, rcc);

            } catch (Exception e) {
                System.err.println("Could not create Selenium Server because of: "
                        + e.getMessage());
                e.printStackTrace();
            }
            try {
                server.start();
                System.out.println("Server started");
            } catch (Exception e) {
                System.err.println("Could not start Selenium Server because of: "
                        + e.getMessage());
                e.printStackTrace();
            }
        } catch (BindException e) {
            System.out.println("Selenium server already up, will reuse...");
        }
    }

    public static void stopSeleniumServer(Selenium selenium){
        selenium.stop();
        if (server != null)
        {
            try
            {
                selenium.shutDownSeleniumServer();
                server.stop();

                server = null;
            }
            catch (Exception e)
            {
                e.printStackTrace();
            }
        }
    }
}

Another very simple and easy way to start selenium server inside the program, and if you are not bothered about setting specific port, just call these few lines in your program (before starting selenium session):

SeleniumServer _server = new SeleniumServer();
_server.boot();
_server.start();

_server.stop(); and once you have finished with the tests.

10 Replies to “How to Start Selenium Server with Java Code”

  1. What need to be passed when we call this function?

    public static void stopSeleniumServer(Selenium selenium){

    40 selenium.stop();

    41 if (server != null)

    42 {

    43 try

    44 {

    45 selenium.shutDownSeleniumServer();

    46 server.stop();

    47

    48 server = null;

    49 }

    50 catch (Exception e)

    51 {

    52 e.printStackTrace();

    53 }

    54 }

  2. Do you have any example code using this class code? That would be helpful. I am planning to deploy the application on Linux server and plan to run on remote machine. Any help would be appreciated.

  3. There seems to be an error in the stopSeleniumServer parameter. I tried to change it from Selenium to SeleniumServer, since the server field is of type SeleniumServer. This is the refactored code:
    public static void stopSeleniumServer(SeleniumServer selenium) {
    selenium.stop();
    if (server != null) {
    try {
    server.stop();
    server = null;
    } catch (Exception e) {
    e.printStackTrace();
    }
    }
    }

    I also made server field private and added a static getter.

    Thanks a lot for the example!

  4. @Amir, you are creating a ServerSocket object and then closing it right after on the next line. Is there any particular reason you are creating the object? I couldn’t find any reference to the ServerSocket object nor the reason to use it in the code.

    Could you please clarify this?

  5. With ss 3.4.0, .start() is not available and .boot() itself is not sufficient to start a session.

    please share if you have any updates on how to start grid via java program with latest selenium server. Thanks for a nice article!

Leave a Reply