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.


  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();
run.exec(“cmd.exe /c start sel-start.bat”);
Thread.sleep(5000);“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.

How to fetch data from the database

There are several methods to fetch data from the Database.  Out of which I personally  prefer DNS less connectivity (JDBC Type 4), as its very easy and handy for any user.

The JDBC type 4 driver, also known as the Direct to Database Pure Java Driver, is a database driver implementation that converts JDBC calls directly into a vendor-specific database protocol.Therefore it is called a THIN driver.

Written completely in Java, type 4 drivers are thus platform independent. They install inside the Java Virtual Machine of the client. This provides better performance  as it does not have the overhead of conversion of calls into ODBC or database API calls and it does not need associated software to work.

Now this being said lets peek into the coding part.

Here goes the code:

import java.sql.Connection;
import java.sql.DriverManager;

public class Main {
public static void main(String[] argv) throws Exception {

/** There are different connection string to the database which you want to connect to.  For eg,
SQL Server – “jdbc:odbc:Driver={SQLServer};Server=Your_Server_Name;Database=Your_Database_Name;Uid=Your_Username;Pwd=Your_Password;”
Microsoft Access – “jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};DBQ=C:/data.MDB”
Oracle – “jdbc:odbc:Driver= {Microsoft ODBCforOracle};;Uid=Your_Username;Pwd=Your_Password;”

String myDB = “jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};DBQ=C:/data.MDB”;
Connection conn = DriverManager.getConnection(myDB, “UserName”, “Password”);
ResultSet rs = stmt.executeQuery(query);

while ( {
System.out.println(rs.getString(“EmployeeID”) + ” ” + rs.getString(“LastName”) + ” ”
+ rs.getString(“FirstName”));
}catch (Exception e) {
// handle the exception
} finally {
try {

} catch (Exception ee) {


Hope this code snipped has been of your help!!!

To avoid Errors and Improve performance of the Selenium automation (Selenium RC+Java)

To Avoid Errors and Improve the performance of the Selenium automation coding(selenium RC+Java)
  1. Maintain a constant url while defining the setup(Instantiation of DefaultSelenium class) and to avoid the Permission Denied Error.
  2. Add the URL under Trusted sites to avoid Permission Denied Error
  3. If Changing from http to https, above mentioned 2 points would be an added advantage. But its advisable to use either http or https for any instance, or else, selenium would consider this as a foreign domain access and would throw permission denied error.
  4. To avoid the delays and speed up the automation run, avoid Thread.sleep() and wait commands as much as possible. Instead use waitForElementPresent and alternatively use webDriver which has much more feasibility with Selenium
  5. For Ajax related events, Avoid using selenium.typeKeys(), Instead use the combination of selenium.type & selenium.fireEvent. This approach gives more flexibility for ajax events
  6. For Ajax related drop down use for loop and verifyElementPresent to synchronise the time, Instead of wait command
  7. If test data is enormous(Multi dimensional or Multiple data like User Details-username, userid, password, etc), avoid declaring in the class files or property files. Instead use xml files and pharse the data using SAX parser.