javaapache-commons-execselenium-server

How to run selenium standalone server as hub using apache commons exec?


I am trying to start selenium-standalone-server as a hub in java program. I have tried to use ProcessBuilder and Process. I was unable to do so. Then I found Apache Commons exec library. I am able to launch selenium server. But, when I am using following code, I am not able to run the server as hub. I am trying to run this code through eclipse.

package selenium.tool.utils;
import org.apache.commons.exec.CommandLine;
import org.apache.commons.exec.DefaultExecutor;

public class HubRunner {

public HubRunner(String portNumber) {
    try {
        String cmd = "java";

        CommandLine cmdline = CommandLine.parse(cmd);
        cmdline.addArgument("-jar");
        cmdline.addArgument("selenium-server-standalone-2.33.0.jar");
        cmdline.addArgument("-role hub", false);
        cmdline.addArgument("-port 5454", false);

        System.out.println(cmdline.toString());

        DefaultExecutor exe = new DefaultExecutor();
        exe.execute(cmdline);
    } catch (Exception ex) {
        System.err.println(ex.getMessage());
    }
}

public static void main(String[] args) {
    HubRunner hr = new HubRunner("8888");
}
}

Ideally, this program should launch server as hub. But, it's not doing. It is omitting '-role hub' and '-port 5454' params.

I get following output:

java -jar selenium-server-standalone-2.33.0.jar -role hub -port 5454
Jul 28, 2013 10:42:32 PM org.openqa.grid.selenium.GridLauncher main
INFO: Launching a standalone server
22:42:33.468 INFO - Java: Sun Microsystems Inc. 14.0-b16
22:42:33.470 INFO - OS: Linux 2.6.33.3-85.fc13.i686.PAE i386
22:42:33.520 INFO - v2.33.0, with Core v2.33.0. Built from revision 4e90c97
22:42:34.111 INFO - RemoteWebDriver instances should connect to: http://127.0.0.1:4444/wd/hub
22:42:34.115 INFO - Version Jetty/5.1.x
22:42:34.117 INFO - Started HttpContext[/selenium-server/driver,/selenium-server/driver]
22:42:34.119 INFO - Started HttpContext[/selenium-server,/selenium-server]
22:42:34.120 INFO - Started HttpContext[/,/]
22:42:34.182 INFO - Started org.openqa.jetty.jetty.servlet.ServletHandler@13c468a
22:42:34.183 INFO - Started HttpContext[/wd,/wd]
22:42:34.200 INFO - Started SocketListener on 0.0.0.0:4444
22:42:34.200 INFO - Started org.openqa.jetty.jetty.Server@15e83f9

Please help me.


Solution

  • I solved the problem by using shell file. I wrote the command to start the selenium hub in .sh file. Then I did below stuff:

    String cmd = "bash";
    CommandLine cmdLine = new CommandLine(cmd);
    cmdLine.addArgument("./src/main/resources/scripts/hub_start.sh");
    cmdLine.addArgument(portNumber);
    executor.execute(cmdLine);
    

    Thus, I was able to execute the shell file and started selenium-hub.