Search This Blog

Monday, May 17, 2010

How to setup Selenium RC with TestNG in eclipse

TestNG is a framework to work in JAVA. Junit and TestNG almost same on the surface but there are major differences between their frameworks and their nature of testing. JUnit is designed to do the unit testing, and it's doing it very well while the TestNG has designed to do the functionality testing at high level. So there are many features available in the TestNG that you will not find in JUnit.

TestNG Annotation

@BeforeTest: The annotated method will be run before any test method belonging to the classes inside the tag is run.

@AfterTest: The annotated method will be run after all the test methods belonging to the classes inside the tag have run.

@BeforeClass: The annotated method will be run before the first test method in the current class is invoked.

@AfterClass: The annotated method will be run after all the test methods in the current class have been run.

@Parameters: Describes how to pass parameters to a @Test method.

Test case

import org.openqa.selenium.server.RemoteControlConfiguration;
import org.openqa.selenium.server.SeleniumServer;
import org.testng.annotations.*;
import com.thoughtworks.selenium.*;

public class googlesearch extends SeleneseTestCase{

Selenium selenium;
public static final String MAX_WAIT_TIME_IN_MS="60000";
private SeleniumServer seleniumServer;

public void setUp(String host, String port, String browser , String url ) throws Exception {
RemoteControlConfiguration rc = new RemoteControlConfiguration();


seleniumServer = new SeleniumServer(rc);
selenium = new DefaultSelenium(host, Integer.parseInt(port), browser, url);


public void googling(String search, String expected) {"/");
selenium.type("q", search);"btnG");

public void tearDown() throws InterruptedException{


TestNG Suite


<!DOCTYPE suite SYSTEM "">
<suite name="googlesearch" verbose="3">
<parameter name="" value="localhost"></parameter>
<parameter name="selenium.port" value="4444"></parameter>
<parameter name="selenium.browser" value="*iexplore"></parameter>
<parameter name="selenium.url" value=""></parameter>

<test name="googlesearch">
<parameter name="search" value=""></parameter>
<parameter name="expected" value=""></parameter>
<class name="googlesearch"></class>

In this method you dont have to start your Selenium RC server through command line . The java code will start the server , run the test and stop the server.
Before you run the Test , make sure all path and build path are correct.

  • Install TestNG plug-in in your eclipse.
  • Set the java build path and pointing to RC sever.
    Go to Java project and right click on it then select properties then select java build path then select library tab and select external jar pointing you selenium RC server.
  • Run the test case as TestNG test suite.
    Go to page right click on the page the click on Run as... and select the suite in the dialog box which you created above.


    1. Hi,
      I am not able to run the above example.. please help.
      Class not found in classpath error..!!


    2. Hi Niraj,

      This is Pravin.The example given above is really good. Can you please post another example on passing the data using an xml isntead of an excel ?? This makes the concept comprehensive.


    3. Hi Niraj,
      Article is good. But where are you passing xml date?

    4. Thanks for sharing great information in your blog. Got to learn new things from your Blog . It was very nice blog to learn about Selenium.

    5. It was very nice blog to learn about Selenium.Thanks for sharing new things.selenium training in chennai

    6. Just found your post by searching on the Google, I am Impressed and Learned Lot of new thing from your post.

      Mehandi Design