Pages

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

goolgesearch.java

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;


@BeforeClass
@Parameters({"selenium.host","selenium.port","selenium.browser","selenium.url"})
public void setUp(String host, String port, String browser , String url ) throws Exception {
RemoteControlConfiguration rc = new RemoteControlConfiguration();

rc.setSingleWindow(true);

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

}

@Test
@Parameters({"search","expected"})
public void googling(String search, String expected) {
selenium.open("/");
selenium.waitForPageToLoad("6000");
selenium.type("q", search);
selenium.click("btnG");
selenium.waitForPageToLoad(MAX_WAIT_TIME_IN_MS);
assertTrue(selenium.isTextPresent(expected));

}
@AfterTest
public void tearDown() throws InterruptedException{
selenium.stop(); 
seleniumServer.stop();

}
}


TestNG Suite

googlesearchsuite.xml

<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="googlesearch" verbose="3">
<parameter name="selenium.host" value="localhost"></parameter>
<parameter name="selenium.port" value="4444"></parameter>
<parameter name="selenium.browser" value="*iexplore"></parameter>
<parameter name="selenium.url" value="http://www.google.com"></parameter>

<test name="googlesearch">
<parameter name="search" value="automationtricks.blogspot.com"></parameter>
<parameter name="expected" value="automationtricks.blogspot.com"></parameter>
<classes>
<class name="googlesearch"></class>
</classes>
</test>
</suite>


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 googlesearch.java page right click on the page the click on Run as... and select the suite in the dialog box which you created above.

  • 5 comments:

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

      Thanks
      Shalabh

      ReplyDelete
    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.

      Thanks,
      pravin

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

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

      Mehandi Design

      ReplyDelete
    5. I am happy to find this post Very useful for me, as it contains lot of information. I Always prefer to read The Quality and glad I found this thing in you post. Thanks. Eminem, also known as Marshall Mathers, is one of the most commercially successful rappers of all time, with more than 100 million albums sold worldwide. He is one of the richest rappers in the world, with an estimated net worth of $210 million. Read more in Eminem net worth.

      ReplyDelete