Wednesday, July 6, 2011

Selenium - TestNG Parameterization

Selenium - TestNG Parameterization

Based on the testing requirement you many need to repeat the test with multiple data sets.
In this post I will explain how to achieve this (parameterization) using TestNG.

There are two ways of sending parameters to the selenium test
(1)using testng.xml
(2) Programmatically
    i)array of objects (Object[][])
    ii)Iterator

Note: I have purposefully commented selenium code for better understanding of TestNG Parameterization.

Using testng.xml 
How to execute the test with Eclipse, select this link.
How to execute the test with Ant, select this link.
Selenium TestNG, Select this link.

Below sample code will help you understand how to send parameters from testng.xml.
Testng code


  
  
  
    
     
   
   
     
        
        
     
   
 


Java code
package package1;

import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;
import com.thoughtworks.selenium.*;

public class Sample 
{
    //public Selenium s = new ds( "localhost",4444,"*iexplore","www.test.com");
    //private Selenium s = new ds( "localhost",4444,"*firefox",BASE_URL);
    @BeforeClass (alwaysRun=true)
    protected void setUp()throws Exception
    {
     /*
     s.start();
     s.windowFocus();
     s.windowMaximize();
     s.windowFocus();
     */
    }
    @AfterClass(alwaysRun=true)
    protected void tearDown() throws Exception
    {
     //s.stop(); 
    }
    @Parameters({"UserNameTestng","PasswordTestng"})
    @Test(groups={"test","SampleDemo"},enabled=true)
    public void test_verifyData1(String s1,String s2) {
     System.out.println(s1+" -  "+s2);
    }    
}



Select following option while executing the code from eclipse (Look at the screen shot)

Output for the above code (Look at the below screen shot)


Using this method we can pass the parameters using xml file, but we can't achieve repeatability, executing the same test with different data sets.

 Programmatically


i)array of objects (Object[][])
If  you have data set, it is possible to place all the values in @dataprovider and run the program.
Dimension one is considered as number of rows, dimension two is considered as number of different data set columns. It will automatically repeat the execution of same code as per the object dimension one count.
Below sample code will help you understand the concept in a better way.

Java Code
package package1;


import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;
import com.thoughtworks.selenium.*;

public class Sample 
{
    //public Selenium s = new ds( "localhost",4444,"*iexplore","www.test.com");
    //private Selenium selenium = new DefaultSelenium( "localhost",4444,"*firefox",BASE_URL);
    @BeforeClass (alwaysRun=true)
    protected void setUp()throws Exception
    {
     /*
     s.start();
     s.windowFocus();
     s.windowMaximize();
     s.windowFocus();
     */
    }
    @AfterClass(alwaysRun=true)
    protected void tearDown() throws Exception
    {
     //s.stop(); 
    }
    
   @DataProvider (name="TestData") 
   public Object[][] createData1() {
   return new Object[][] {
     { "Bharath", "11" },
     { "Raj", "22"},
       };
    }
        
        
    @Test(dataProvider = "TestData",groups={"test","SampleDemo"},enabled=true)
    public void test_verifyData1(String s1,String s2) {
        System.out.println(s1+" - "+s2);
    }    
       
}

Output for the above code (Look at the below screen shot)

This method will be useful when you have the data set upfront, What is the situation if the data exist in excel or notepad (csv format).

ii)Iterator
If the data exist in excel or notepad, this technique is the best choice. Below sample code will help you understand the concept in a better way. I have used Jexcel api to read data from the excel file, don't forget to add jxl.jar to package build path.Basically I am reading the data from the excel file and feeding it to the iterator object.


Java Code
package package1;

import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.DataProvider;

import org.testng.annotations.Parameters;
import org.testng.annotations.Test;
import com.thoughtworks.selenium.*;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;


import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;


public class Sample 
{
    //public Selenium s = new ds( "localhost",4444,"*iexplore","www.test.com");
    //private Selenium selenium = new DefaultSelenium( "localhost",4444,"*firefox",BASE_URL);
    @BeforeClass (alwaysRun=true)
    protected void setUp()throws Exception
    {
     /*
     s.start();
     s.windowFocus();
     s.windowMaximize();
     s.windowFocus();
     */
    }
    @AfterClass(alwaysRun=true)
    protected void tearDown() throws Exception
    {
     //s.stop(); 
    }
 @DataProvider (name="TestData") 
 public Iterator createData() throws IOException{                 
 ArrayList myEntries = new ArrayList();  
 File inputWorkbook = new File("c:/jexcel.xls");   
              Workbook w;     
 String Temp1, Temp2;       
 try {         
  w = Workbook.getWorkbook(inputWorkbook);     
  Sheet sheet = w.getSheet(0);       
  for (int i = 0; i < sheet.getRows(); i++) {      
                // column, row     
  Cell cell = sheet.getCell(0, i);         
  Temp1 = cell.getContents();                 
  cell = sheet.getCell(1, i);      
  Temp2 = cell.getContents();                
  myEntries.add(new Object [] {Temp1,Temp2});       
   }      
  } 
  catch (BiffException e) 
  {e.printStackTrace();}      
  System.out.println("Successfully read data file");          
                return myEntries.iterator();          
 }    
  
 @Test(dataProvider = "TestData",groups={"test","SampleDemo"},enabled=true)    
 public void test_verifyData1(String n1,String n2) {    
  System.out.println(n1+" "+n2); }        
}
Sample excel file shown below. Always save in .xls only, as jxl currently support .xls.
Output for the above code (Look at the below screen shot)
How to place data provider in different class?
By default it will search for dataprovider in the current test class. If you want to declare the data provider in different class, specify the method as static and mention the class name in the DataProviderClass attribute.
@Test(dataProvider = "TestData",dataProviderClass="StaticData.class",groups={"test","SampleDemo"},enabled=true)
---

5 comments:

  1. Hi Bharath,

    Can I have build.xml for testng suite please.


    Thanks,
    Rama

    ReplyDelete
  2. This comment has been removed by the author.

    ReplyDelete
  3. This comment has been removed by the author.

    ReplyDelete
  4. Hello Rama,

    I have created a separate post for this, visit this link

    http://bharath-marrivada.blogspot.com/2011/11/testng-selenium-build-jar-standlone.html

    ReplyDelete