February 11, 2011

Test Suite using Test NG....

Most testing frameworks have the concept of grouping a set of text fixtures into a ’suite’ so that you can execute a number of related tests together.

Creating test suites with Selenium doesn’t appear to be obvious at first. Here’s a simple guide on how to create and execute suites of functional tests:



First create two scripts....Ex:Google1 and Google2


Google1.java
------------------------------------------------------------

package test;


import com.thoughtworks.selenium.*;


import org.openqa.selenium.server.*;
import org.testng.annotations.*;


public class Google1 extends SeleneseTestCase{
public Selenium selenium;
public SeleniumServer seleniumserver;


@BeforeClass
public void setUp() throws Exception {
RemoteControlConfiguration rc = new RemoteControlConfiguration();
seleniumserver = new SeleniumServer(rc);
selenium = new DefaultSelenium("localhost", 4444, "*iexplore","http://");
seleniumserver.start();
selenium.start();
}


@Test
public void testDefaultTNG() throws Exception {


selenium.open("http://www.google.com");
selenium.windowMaximize();
}


@AfterClass
public void tearDown() throws Exception {
selenium.stop();
seleniumserver.stop();


}
}
------------------------------------------------------------

Google2.java

------------------------------------------------------------

package test;


import com.thoughtworks.selenium.*;

import org.openqa.selenium.server.*;
import org.testng.annotations.*;

public class Google2 extends SeleneseTestCase{
public Selenium selenium;
public SeleniumServer seleniumserver;

@BeforeClass
public void setUp() throws Exception {
RemoteControlConfiguration rc = new RemoteControlConfiguration();
seleniumserver = new SeleniumServer(rc);
selenium = new DefaultSelenium("localhost", 4444, "*iexplore","http://");
seleniumserver.start();
selenium.start();
}

@Test
public void testGoogle2() throws Exception {

selenium.open("http://www.yahoo.com");
selenium.windowMaximize();
}

@AfterClass
public void tearDown() throws Exception {
selenium.stop();
seleniumserver.stop();

}
}




------------------------------------------------------------


Now Create testsuite.xml file....

To create new XML file ..just right click on package(test--my package name) --->new-->File---testsuite.xml(any name you can give )

Sample XML file looks like this..
TestNG Suite

<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd"> <suite thread-count="5" configfailurepolicy="skip" verbose="1" name="suite" skipfailedinvocationcounts="false" parallel="false" annotations="JDK" data-provider-thread-count="10">   <test verbose="2" name="testsuite" junit="false" preserve-order="false"> 
    <classes> 
      <class name="test.Google1"/>
      <class name="test.Google2"/>
    </classes> 
  </test> </suite>  



------------------------------------------------------------
   

       
  
DONE...........


How to RUN????


Right click on testsuite.xml--->Run as--->TestNG Suite.

Thats it!...your tests will run one after the another.................


Hope you enjoy this post....

6 comments:

  1. Hello Nagarjuna,

    How to check form validations in selenium, i am unable to find it, please give me one example like if a form contains username,password,email fields then how to check mandatory fileds & email validation.
    Thank you in advance.. please help me
    Prasanna

    ReplyDelete
  2. Hi
    I am using same approach but my scripts are running parallel not sequential order.

    ReplyDelete
  3. That works fine in and IDE, but how do you invoke it from the command line?

    I would also appreciate an example that shows sequential and parallel versions that could be invoked from the command line.

    So far all examples I have seen involve using JUnit or TestNG from within the IDE.

    Mike

    ReplyDelete
  4. I am using the same approach , but getting the error main function has not declare while running the script through testsuite.xml file

    ReplyDelete
  5. Hi Naga,This is very Helpful for learners,and is it possible to add already created sample maven project with download option.
    That is more helpful for clear understanding.

    ReplyDelete