Webdriver Script020 : Data Driven Testing from Excel

/*********************************************************************

Script Summary

 Title  : Webdriver Script020 : Data Driven Testing from Excel
 Author : Gaurav Khanna

 1. Open the excel file.
 2. Store Username and Password in list.
 3. Prints username and password.

 *********************************************************************/


package com.gaurav.webdriver;

import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;
import org.testng.annotations.Test;

public class s020_DataDrivenExample1 {

    public List<UserData> getData() throws BiffException, IOException {

        //
        FileInputStream file = new FileInputStream(
                "C:\\gk\\code\\selenium\\sample\\sample1.xls");

        //
        Workbook workbook = Workbook.getWorkbook(file);

        //
        Sheet sheet = workbook.getSheet("register");
        System.out.println(sheet.getRows());
        List<UserData> list = new ArrayList<UserData>();
        for (int i = 1; i < sheet.getRows(); i++) {
            String userName = sheet.getCell(0, i).getContents();
            String password = sheet.getCell(1, i).getContents();
            list.add(new UserData(userName, password));
        }
        return list;
    }

    @Test
    public void testscript020() throws BiffException, IOException {

        List<UserData> list = getData();
        for (UserData userData : list) {
            System.out.println("Row Content : " + userData.getUserName());
            System.out.println("Coloumn Content : " + userData.getPassword());
        }
    }

    class UserData {
        private String userName;
        private String password;

        public String getUserName() {
            return userName;
        }

        public void setUserName(String userName) {
            this.userName = userName;
        }

        public String getPassword() {
            return password;
        }

        public void setPassword(String password) {
            this.password = password;
        }

        public UserData(String userName, String password) {
            super();
            this.userName = userName;
            this.password = password;
        }
    }
}