"No one is harder on a talented person than the person themselves" - Linda Wilkinson ; "Trust your guts and don't follow the herd" ; "Validate direction not destination" ;

August 21, 2010

Learning TestNG & Selenium

TestNG is a testing framework
  • Developed by Cedric Beust
  • Provides support for Data Driven Testing
  • Provides annotations for Pre-Test, Assigning Data, Executing Test, Verifying results (Assert)
Annotation - Way of associating metadata (declarative information) with program elements such as classes, methods, fields, parameters, local variables, and packages.

How Annotation works - Annotations hook into the compile process of the java compiler, to analyse the source code for user defined annotations and handle then. Reference
Get TestNG Eclipse plug-in - This allows you to run your TestNG tests from Eclipse and easily monitor their execution and their output.

Basic Example
1. Create a basic java project in eclipse editor
2. Add TestNG jar in Libraries. You can download it from testng.org
3. Create example1.java and paste the below code
----------------------------------------------------------------------------------------------------------------------------------
import java.util.Vector;
import org.testng.Assert;
import org.testng.annotations.*;
public class example1 {
private int a;
private int b;
private int c = 0;

@BeforeClass
public void setUp() {
// code that will be invoked when this test is instantiated
}
//This method will provide data to any test method that declares that its Data Provider is named "test1"
@DataProvider(name = "test1")
public Object[][] createData1()
{ return new Object[][]
{ { new Integer(40), new Integer(30)}
};
}
//This test method declares that its data should be supplied by the Data Provider named "test1"
@Test(dataProvider = "test1")
public void verifyData1(Integer a, Integer b)
{
c = a + b;
Assert.assertEquals(c, 70);
System.out.println("Fast test");
}
@Test(groups = { "slow" })
public void aSlowTest() {
System.out.println("Slow test");
}
}
----------------------------------------------------------------------------------------------------------------------------------
4. Run this as a TestNg Test
5. Result would be
Slow test
Fast test
PASSED: aSlowTest
PASSED: verifyData1(40, 30)
A test is successful if no exceptions are thrown.

More Reads
Basic Example
TestNG Documentation on exposed methods
Junit Vs TestNG

Moving to Next Level - Integrating Selenium and TestNG
1. Start the Selenium Server thru command mode by command java -jar selenium-server.jar. Open Command Windows in Administrator Mode (For Win7)
2. Create a java Project. Add TestNG Library and Selenium Java Client Driver Jar files
3. Paste the below code
----------------------------------------------------------------------------------------------------------------------------------
import static org.testng.Assert.assertTrue;
import java.util.Vector;
import org.testng.Assert;
import org.testng.annotations.*;
import com.thoughtworks.selenium.*;
public class example1 {
public static Selenium selenium;
@BeforeTest
public void openBrowser() {
System.out.println("Before Test");
selenium = new DefaultSelenium("localhost", 4444, "*iehta",http://google.com/);
}

@AfterTest
public void closeBrowser() {
System.out.println("After Test");
selenium.stop();
}

@Test
public void Testmethod() {
System.out.println("Running Test");
selenium.start();
selenium.open("http://google.com");
selenium.type("q", "selenium rc");
selenium.click("btnG");
selenium.setTimeout("1000000");
assertTrue(selenium.isTextPresent("seleniumhq.org"));
}
}
----------------------------------------------------------------------------------------------------------------------------------
4. Run as TestNG Test
5. Result would be
Before Test
Running Test
After Test
PASSED: Testmethod

Next Example
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Vector;
import org.testng.Assert;
import org.testng.annotations.*;
import static org.testng.Assert.*;
public class test {

@DataProvider(name = "test1")
public Object[][] createData() {
    Object[][] retkeyword={{"One"},
                        {"Two"},
                        {"Three"}};
    return(retkeyword);
}

//This test method declares that its data should be supplied by the Data Provider named "test1"
@Test(dataProvider = "test1")
public void verifyData1(String keyword)
{
      System.out.println("Value :"+keyword);
}
}

Example - III
I am updating it with complete steps as some of the links seems to be invalid now. This is extension of previous post

1. Install JDK 6 or any latest version

2. Download Eclipse IDE for Java Developers


3. Unzip and Run Eclipse.exe as shown below

4. Download Testng jar file for project.

5. Create a new project and add the external jar as shown below

6. Copy-paste below source code in java file created for the project


import org.testng.annotations.*;
            import org.testng.*;
public class FirstExample {
    private int a;
    private int b;
    private int c = 0;
    @BeforeClass
    public void setUp() {
        // code that will be invoked when this test is instantiated
    }

    //Data Provider provide input test data (2 Integer Numbers)
    @DataProvider(name = "Add2Numbers")
    public Object[][] createData1()
    { return new Object[][]
    { { new Integer(2), new Integer(2)},
            { new Integer(3), new Integer(3)},
            { new Integer(4), new Integer(4)}
        };
    }

    //Test method gets input from data provider Add2Numbers
    @Test(dataProvider = "Add2Numbers")
    public void testtoAddnumber(Integer a, Integer b)
    {
        c = a + b;
        verify(c,a,b);
    }   

    public void verify(Integer c, Integer a, Integer b)
    {
        Assert.assertEquals((Integer)(a+b), (Integer)c);
        System.out.println("Test Completed");
    }  

} 

7. Eclipse IDE, Under Help-Install Software Install TestNG-Add on as shown below. First Provide the URL, then Add it with Name.



8. To Run the test please find below snapshot

9. Output from console window



10. Explanation for Annotations
@Dataprovider – Here is where we can read from xml / write custom code for setting test data. The return type is an object. Depending on number of objects returned test would run that many times.
@Test – This is the test which we would run, We have mentioned the data provider for that particular test. Return Type of Data provider is the input parameter for the test method. You can provide type of test (Functional / regression / priority)
You also have other annotations like pretest, posttest, onsuccess, onfailure, run parallel tests


Happy reading!!

No comments: