Sunday, 31 August 2014

Multiple set's of test data in single sheet

If you are new to the below information(i.e. using library 'AH-DDF-<<VERSION>>.jar') refer URL: http://automation-home.blogspot.in/2014/06/TestAutomationFrameWork.html

Follow below process for reading different sets of test data in single excel sheet.


Note: Header should be in String format, if the header content is not String format then convert into String format.

Sample Program:

package examples.test.excel;

import java.util.HashMap;

import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

import automation_home.ddf.constants.ExcelConstants;
import automation_home.ddf.wrapper.Wrapper;
import automation_home.ddf.wrapperimpl.ExcelWrapper;;

public class TestData2{

  @DataProvider
  public Object[][] dp1() throws Exception {
 Wrapper wrapper = new ExcelWrapper();
 wrapper.setParameter(ExcelConstants.FILE_PATH, "D:\\Eclipse Workspce\\Sample Project-DDF\\src\\examples\\test\\excel\\TestFile2.xls");
 wrapper.setParameter(ExcelConstants.SHEET_NAME, "FlightInfo1");

 wrapper.setParameter(ExcelConstants.INCLUDE_TESTDATA_HEADER_NAME, "Execution");
 wrapper.setParameter(ExcelConstants.INCLUDE_TESTDATA_YES,"RUN");
 wrapper.setParameter(ExcelConstants.INCLUDE_TESTDATA_NO,"NO-RUN");
 return wrapper.retrieveTestData();
  }

  @Test(dataProvider="dp1", enabled=true)
  public void test1(HashMap<String, String> h){
 System.out.println("FlightName : "+h.get("FlightName"));
 System.out.println("FlightNumber : "+h.get("FlightNumber"));
 System.out.println("AirPort : "+h.get("AirPort"));
 System.out.println("Date : "+h.get("Date"));
 System.out.println("----------------------------------------");
  }


  @DataProvider
  public Object[][] dp2() throws Exception {
 Wrapper wrapper = new ExcelWrapper();
 wrapper.setParameter(ExcelConstants.FILE_PATH, "D:\\Eclipse Workspce\\Sample Project-DDF\\src\\examples\\test\\excel\\TestFile2.xls");
 wrapper.setParameter(ExcelConstants.SHEET_NAME, "FlightInfo1");

 wrapper.setParameter(ExcelConstants.INCLUDE_TESTDATA_HEADER_NAME, "Execution");
 wrapper.setParameter(ExcelConstants.INCLUDE_TESTDATA_YES,"RUN");
 wrapper.setParameter(ExcelConstants.INCLUDE_TESTDATA_NO,"NO-RUN");

 wrapper.setParameter(ExcelConstants.INITIAL_POSITION, "9,0");
 return wrapper.retrieveTestData();
  }

  @Test(dataProvider="dp2", enabled=true)
  public void test2(HashMap<String, String> h){
 System.out.println("1 : "+h.get("1"));
 System.out.println("2 : "+h.get("2"));
 System.out.println("3 : "+h.get("3"));
 System.out.println("4 : "+h.get("4"));
 System.out.println("5 : "+h.get("5"));
 System.out.println("6 : "+h.get("6"));
 System.out.println("7 : "+h.get("7"));
 System.out.println("8 : "+h.get("8"));
 System.out.println("----------------------------------------");
  }


  @DataProvider
  public Object[][] dp3() throws Exception {
 Wrapper wrapper = new ExcelWrapper();
 wrapper.setParameter(ExcelConstants.FILE_PATH, "D:\\Eclipse Workspce\\Sample Project-DDF\\src\\examples\\test\\excel\\TestFile2.xls");
 wrapper.setParameter(ExcelConstants.SHEET_NAME, "FlightInfo1");

 wrapper.setParameter(ExcelConstants.INCLUDE_TESTDATA_HEADER_NAME, "Execution");
 wrapper.setParameter(ExcelConstants.INCLUDE_TESTDATA_YES,"RUN");
 wrapper.setParameter(ExcelConstants.INCLUDE_TESTDATA_NO,"NO-RUN");

 wrapper.setParameter(ExcelConstants.INITIAL_POSITION, "16,0");
 return wrapper.retrieveTestData();
  }

  @Test(dataProvider="dp3", enabled=true)
  public void test3(HashMap<String, String> h){
 System.out.println("TrainName : "+h.get("TrainName"));
 System.out.println("TrainNumber : "+h.get("TrainNumber"));
 System.out.println("Date : "+h.get("Date"));
 System.out.println("----------------------------------------");
  }

  @DataProvider
  public Object[][] dp4() throws Exception {
 Wrapper wrapper = new ExcelWrapper();
 wrapper.setParameter(ExcelConstants.FILE_PATH, "D:\\Eclipse Workspce\\Sample Project-DDF\\src\\examples\\test\\excel\\TestFile2.xls");
 wrapper.setParameter(ExcelConstants.SHEET_NAME, "FlightInfo1");

 wrapper.setParameter(ExcelConstants.INCLUDE_TESTDATA_HEADER_NAME, "Execution");
 wrapper.setParameter(ExcelConstants.INCLUDE_TESTDATA_YES,"RUN");
 wrapper.setParameter(ExcelConstants.INCLUDE_TESTDATA_NO,"NO-RUN");

 wrapper.setParameter(ExcelConstants.INITIAL_POSITION, "22,0");
 return wrapper.retrieveTestData();
  }

  @Test(dataProvider="dp4", enabled=true)
  public void test4(HashMap<String, String> h){
 System.out.println("a : "+h.get("a"));
 System.out.println("b : "+h.get("b"));
 System.out.println("c : "+h.get("c"));
 System.out.println("----------------------------------------");
  }
}

Output:

FlightName : India Air Lines
FlightNumber : 10011.0
AirPort : Bangalore
Date : Thu Apr 10 00:00:00 PDT 2014
----------------------------------------
FlightName : Emirates
FlightNumber : 10012.0
AirPort : Hyderabad
Date : Sun Jun 22 00:00:00 PDT 2014
----------------------------------------
1 : Emirates1
2 : 10012.0
3 : Hyderabad
4 : Sun Jun 22 00:00:00 PDT 2014
5 : Emirates
6 : 10012.0
7 : Hyderabad
8 : Sun Jun 22 00:00:00 PDT 2014
----------------------------------------
TrainName : MUMBAI EXPRESS
TrainNumber : 16332.0
Date : Sun Jun 22 00:00:00 PDT 2014
----------------------------------------
a : 3.0
b : 6.0
c : 7.0
----------------------------------------
PASSED: test1({AirPort=Bangalore, FlightName=India Air Lines, Date=Thu Apr 10 00:00:00 PDT 2014, FlightNumber=10011.0})
PASSED: test1({AirPort=Hyderabad, FlightName=Emirates, Date=Sun Jun 22 00:00:00 PDT 2014, FlightNumber=10012.0})
PASSED: test2({3=Hyderabad, 2=10012.0, 1=Emirates1, 7=Hyderabad, 6=10012.0, 5=Emirates, 4=Sun Jun 22 00:00:00 PDT 2014, 8=Sun Jun 22 00:00:00 PDT 2014})
PASSED: test3({TrainNumber=16332.0, Date=Sun Jun 22 00:00:00 PDT 2014, TrainName=MUMBAI EXPRESS})
PASSED: test4({b=6.0, c=7.0, a=3.0})

===============================================
    Default test
    Tests run: 5, Failures: 0, Skips: 0

===============================================

Note:
1. With the above approach as the data grow's more and more, value for parameter 'ExcelConstants.INITIAL_POSITION' should be updated.
2. Above approach works with "AH-DDF-3.0.4.jar" or higher version.

Download zip file "SingleExcelWithMultiData.zip" for sampe test data and source code for accessing testdata from repository.


No comments:

Post a Comment