Wednesday, July 20, 2011

How to start selenium server using java

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.

Procedure:
=========
  1. Create a bat file as mentioned below and place it inside the project folder
  2. Create a java class as mentioned below and invoke the windows command prompt and call the above created bat
  3. Now you can continue to start your selenium test case execution
The code goes here:
===============

Creating a .bat file:
==============
  1. 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.
  2. Save the file as sel-start.bat

Creating a java class file to call he above created .bat file:
========================================

package com

import org.apache.log4j.Logger;

/**
* @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();
try
{
run.exec("cmd.exe /c start sel-start.bat");
Thread.sleep(5000);
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.

1 comment: