For some of our automated tests we are switching to use the open-source project Selenium-RC. You can read more about it at its web site: http://selenium-rc.openqa.org/, but essentially it runs a java server which can control an internet browser, and then your testing code sends commands to this server. One key part of this setup is that you need the server running while your testing code is executing. For automated testing machines it would be no big deal to make the Selenium server a service; however developers probably don’t want it running all the time—in fact they do not want to think about it!
Thus our solution was to have our testing code launch the server. I’ve seen a number of posts on various forums asking how to start the selenium server form Java, but none of them had concrete answers. Thus I will reproduce our implementation for you to use and modify as you please:
Process p = null;
try {
String[] cmd = {"java","-jar","C:\\<path to selenium>\\server\\selenium-server.jar" };
p = Runtime.getRuntime().exec(cmd);
} catch (IOException e) {
System.out.println("IOException caught: "+e.getMessage());
e.printStackTrace();
}
System.out.println("Waiting for server...");
int sec = 0;
int timeout = 20;
boolean serverReady = false;
try {
BufferedReader input = new BufferedReader(new InputStreamReader(p.getInputStream()));
while (sec < timeout && !serverReady) {
while (input.ready()) {
String line = input.readLine();
System.out.println("From selenium: "+line);
if (line.contains("Started HttpContext[/,/]")) {
serverReady = true;
}
}
Thread.sleep(1000);
++sec;
}
input.close();
} catch (Exception e) {
System.out.println("Exception caught: "+e.getMessage());
}
if (!serverReady) {
throw new RuntimeException("Selenium server not ready");
}
System.out.println("Done waiting");
Some notes on the above code: