Tuesday 26 February 2013

Data driven testing using TestNG

Data driven testing is executing the same script with set of multiple data from the external source file. Selenium does not have any inbuilt source for data driven testing to fetch the data like QTP has. To overcome this selenium uses external sources like excel, etc. To achieve data driven testing in selenium through many frameworks. In that TestNG also one of the frameworks which could help to pass the multiple data to a single script.

TestNG is a testing framework which helps for data driven testing with additional some more features that make it more powerful and easier to use. DataProvider is one of the feature which passes the set of data stored in the variables. 

Jxl is a java API that enables to read, write, and modify excel spreadsheets dynamically.

Prerequesties:



Steps:

  1. Open eclipse IDE
  2. Create new JAVA project
  3. Create new package under the project
  4. Create new class under the package
  5. Add the jxl,TextNG, selenium server, junit jar files in java build path
  6. Add new folder inside src folder
  7. Add another folder inside newly added folder
  8. Create excel file as defined below and save it in finally created folder.
  9. Type the following code inside the class editor
  10. Save the project
  11. Right click on project name and click on Build Project option
  12. Right click on project name and choose Run as-->click on TestNG test option

Please look at the following example for data driven testing using jxl and TestNG,

Example: (Searching data in Google)


package google4;

import com.thoughtworks.selenium.*;
import static org.testng.AssertJUnit.*;
import org.testng.annotations.*;
import java.io.File;
import jxl.*;


public class google1{
private Selenium selenium;

@BeforeClass
public void startSelenium() throws Exception {
selenium = new DefaultSelenium("localhost", 4444, "*chrome", "http://www.google.co.in");
selenium.start();
selenium.windowMaximize();
}

@DataProvider(name = "DP1")
public Object[][] createData1() throws Exception{
    Object[][] retObjArr=getTableArray("src\\Resources\\Data\\data1.xls",
            "DataPool", "imdbTestData1"); // /test1/src/Resources/Data/data1.xls
    return(retObjArr);
   
}

@Test (dataProvider = "DP1")
public void testGoogle1(String search) throws Exception{
selenium.open("http://www.google.co.in/");
Thread.sleep(3000);
System.out.println("Opened");
selenium.type("id=gbqfq", search);
Thread.sleep(3000);
System.out.println("Typed");
selenium.click("id=gbqfb");
Thread.sleep(3000);
System.out.println("Clicked");
}

@AfterClass
public void tearDown() throws Exception {
//selenium.stop();
}
public String[][] getTableArray(String xlFilePath, String sheetName, String tableName) throws Exception{
        String[][] tabArray=null;
        
            Workbook workbook = Workbook.getWorkbook(new File(xlFilePath));
            Sheet sheet = workbook.getSheet(sheetName); 
            int startRow,startCol, endRow, endCol,ci,cj;
            Cell tableStart=sheet.findCell(tableName);
            startRow=tableStart.getRow();
            startCol=tableStart.getColumn();

            Cell tableEnd= sheet.findCell(tableName, startCol+1,startRow+1, 100, 64000,  false);                

            endRow=tableEnd.getRow();
            endCol=tableEnd.getColumn();
            System.out.println("startRow="+startRow+", endRow="+endRow+", " +
                    "startCol="+startCol+", endCol="+endCol);
            tabArray=new String[endRow-startRow-1][endCol-startCol-1];
            ci=0;

            for (int i=startRow+1;i<endRow;i++,ci++){
                cj=0;
                for (int j=startCol+1;j<endCol;j++,cj++){
                    tabArray[ci][cj]=sheet.getCell(j,i).getContents();
                }
            }
       return(tabArray);
    }
    
}


Excel data:



In the above example following code helped to read the data from the excel sheet and store it in the variable.

@DataProvider(name = "DP1")
public Object[][] createData1() throws Exception{
    Object[][] retObjArr=getTableArray("src\\Resources\\Data\\data1.xls",
            "DataPool", "imdbTestData1"); // /test1/src/Resources/Data/data1.xls
    return(retObjArr);
   
}




public String[][] getTableArray(String xlFilePath, String sheetName, String tableName) throws Exception{
        String[][] tabArray=null;
        
            Workbook workbook = Workbook.getWorkbook(new File(xlFilePath));
            Sheet sheet = workbook.getSheet(sheetName); 
            int startRow,startCol, endRow, endCol,ci,cj;
            Cell tableStart=sheet.findCell(tableName);
            startRow=tableStart.getRow();
            startCol=tableStart.getColumn();

            Cell tableEnd= sheet.findCell(tableName, startCol+1,startRow+1, 100, 64000,  false);                

            endRow=tableEnd.getRow();
            endCol=tableEnd.getColumn();
            System.out.println("startRow="+startRow+", endRow="+endRow+", " +
                    "startCol="+startCol+", endCol="+endCol);
            tabArray=new String[endRow-startRow-1][endCol-startCol-1];
            ci=0;

            for (int i=startRow+1;i<endRow;i++,ci++){
                cj=0;
                for (int j=startCol+1;j<endCol;j++,cj++){
                    tabArray[ci][cj]=sheet.getCell(j,i).getContents();
                }
            }
       return(tabArray);
    }
    

Happy Testing.

3 comments:

  1. Your blog is very helpful. I am automating quikr and I have a doubt.
    My doubt is that while editing any add that you posted in quikr.com, then there is a subcategory field in the add.
    I think that is not a driopdown. After selecting the main category, corresponding subcategories are displayed there on the same dropdown (here i am saying dropdown but please do not get confused as that is not dropdown i think)...
    Now I am unable to select any subcategory from that field.
    Can you please help me out here.


    Please help how to select the value here...

    ReplyDelete
  2. How to use this code in our Automation scripts. I mean to say this is to read the data from Excel file but how to use only few data from the file

    ReplyDelete
  3. I want to pass my base url from excel sheet using eclipse ide for selenium RC can you please help me out from here

    ReplyDelete