Data Driven Testing With Apache POI Excel

Posted By : Manish Kumar | 30-Jan-2018
We can use data sheet for our data-driven testing with the help of Apache POI. As a difference between Selenium and QTP, selenium is just a framework which can be used for automated testing instead of a ready-to-test tool, means we need to put some efforts.
 
Additionally, for changing parameters if execution you just need to change add or update cell values in excel and need not to change anything in your script.
 
We just need to download and add the JAR file to our selenium and import required classes.
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;


public class DataGenerate {
	Sheet readsheet = null;
	String c = null;
	public DataGenerate() throws IOException
	{
		Workbook readexcel = null ;
		String fileName = "testdata2.xls";
	File file = new File("/home/manish/Desktop/testdata2.xls");
	FileInputStream inputStream = new FileInputStream(file);
	String fileExtension = fileName.substring(fileName.indexOf("."));
	
	if (fileExtension.equals(".xls"))
	{
		readexcel = new HSSFWorkbook(inputStream);
		}
	else
	{
		System.out.println("File extension is different, please check");
	}
	System.out.println("sheet read run");
	readsheet = readexcel.getSheet("Sheet1");
}
	public String getelement(int rowindex, int coulmnindex)
	{
		
		System.out.println("run");
		Row row = readsheet.getRow(rowindex);
		String element = row.getCell(coulmnindex).getStringCellValue();
		System.out.println(element);
		return(element);    }

Hope, this will help you for your script and data-driven testing.
Related Tags

About Author

Author Image
Manish Kumar

Manish is Adaptive and passionate to learn new technical skills, have knowledge of testing and love to play badminton in free time

Request for Proposal

Name is required

Comment is required

Sending message..