Most of the time people ask me whether we can integrate a code using java so that, starting selenium server from command prompt can be avoided. I have been giving this piece of code for the users.
- Create a bat file as mentioned below and place it inside the project folder
- Create a java class as mentioned below and invoke the windows command prompt and call the above created bat
- Now you can continue to start your selenium test case execution
The code goes here:
Creating a .bat file:
- Open a notepad or any editor and Type the following without double qoutes “java -jar selenium-server-standalone-2.0b2.jar -interactive -port 4444”. Note, the selenium-server-standalone-2.0b2.jar file should be in the same folder as of the bat file or else it will not work. If the server and bat file are place in different location, then proper path should be mentioned in the bat file.
- Save the file as sel-start.bat
Creating a java class file to call he above created .bat file:
* @author : ramesh
* @purpose : This class will start the Selenium Server automatically.
* @usage : Instantiate this class to start the Selenium Server
public class StartSeleniumServer
Logger log = Logger.getLogger(StartSeleniumServer.class);
public void startServer() throws Exception
Runtime run =Runtime.getRuntime();
run.exec(“cmd.exe /c start sel-start.bat”);
log.info(“Started the Selenium Server successfully”);
catch (Exception e)
log.error(“Could not start the Server Server Due to the following reason.”,e);
Yuppie, now you are set to go.