TestNG Annotation
@BeforeTest: The annotated method will be run before any test method belonging to the classes inside the
@AfterTest: The annotated method will be run after all the test methods belonging to the classes inside the
@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.
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.
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.
Hi,
ReplyDeleteI am not able to run the above example.. please help.
Class not found in classpath error..!!
Thanks
Shalabh
Hi Niraj,
ReplyDeleteThis 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
Hi Niraj,
ReplyDeleteArticle is good. But where are you passing xml date?
Just found your post by searching on the Google, I am Impressed and Learned Lot of new thing from your post.
ReplyDeleteMehandi Design
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