Writing simple Selenium Scripts by implementing Data Driven Framework using TestNG

Data Driven Framework scripts means executing a set of steps with multiple sets of data. Selenium is not have any default way of implementing but by using TestNG support we can implement it successfully.

Step 1: JXL API Jar - We can download it form the following URL
http://sourceforge.net/projects/jxls/files/ and download latest version

Step 2: Prepare Data Source - Open an excel and prepare data source as shown below


In this example we are discussing reading inputs from an excel sheet and writing the results in reports sheet.

Step 3: Following example will demonstrate how to access from an excel sheet and how to write the results into an excel sheet.

package com.google.excel;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;
import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
public class GMail{
 private WebDriver driver;
 private String baseURL;
 private FileInputStream fi;
 private Sheet s;
 private Workbook wb;

 @BeforeClass
 public void setup() throws BiffException, IOException
 {
  driver = new FirefoxDriver();
  baseURL = "http://www.gmail.com";
  fi = new FileInputStream("TestDate\\Book1.xls");
  wb = Workbook.getWorkbook(fi);
  s = wb.getSheet(0);
 }

 @AfterClass
 public void teardown()
 {
  driver.quit();
 }
 @Test
 public void testGmail() throws InterruptedException
 {
  for(int row=0; row <s.getRows();row++)
  {
    String username = s.getCell(0, row).getContents();
    System.out.println("Username "+username);
    driver.get(baseURL);
    driver.findElement(By.name("Email")).sendKeys(username);
    String password= s.getCell(1, row).getContents();
    System.out.println("Password "+password);
    driver.findElement(By.name("Passwd")).sendKeys(password);
    Thread.sleep(10000);
    Thread.sleep(30000);
    writeExcel(0, 0, "Passes");
  }
 }

public void writeExcel(int a, int b, String text) {
    try {
        File excelFile = new File("TestDate\\Results.xls");
        WritableWorkbook book;
        WritableSheet sheet;
        Workbook existingBook = null;
        if (!excelFile.exists()) {
            book = Workbook.createWorkbook(excelFile);
            sheet = book.createSheet("TESTRESULTS", 0);
        } else {
            existingBook = Workbook.getWorkbook(excelFile);
            book = Workbook.createWorkbook(excelFile, existingBook);
            sheet = book.getSheet("TESTRESULTS");
        }
        Label i = new Label(a, b, text);
        sheet.addCell(i);
        book.write();
        book.close();
        if (existingBook != null)
            existingBook.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}
}