在設(shè)計(jì)測試,參數(shù)化測試是不可避免的。我們會(huì)利用Apache的POI- Excel JAR實(shí)現(xiàn)是一樣的。它可以幫助我們來讀取和寫入到Excel中。
第1步:導(dǎo)航到URL- http://poi.apache.org/download.htmll并下載ZIP格式。
第2步:點(diǎn)擊鏡像鏈接下載JAR。
第3步:解壓縮到一個(gè)文件夾。
第4步:如下所示的解壓縮后的內(nèi)容將被顯示。
第5步:現(xiàn)在創(chuàng)建一個(gè)新的項(xiàng)目,并在“External JARs”添加“POI-3.10.FINAL”文件夾中所有的jar包。
第6步:現(xiàn)在,添加所有的“External JARs”在“OOXML-LIB”文件夾中。
第7步:現(xiàn)在,添加所有的“External JARs”在“lib”文件夾中。
第8步:如下圖所示,顯示已添加的JAR文件。
第9步:如下圖所示的Package Explorer顯示。此外附加“webdriver”相關(guān)的JAR
為了演示目的,我們將參數(shù)的百分比計(jì)算器測試。
第1步:我們將所有的參數(shù)需要使用Excel的%計(jì)算器的輸入。所設(shè)計(jì)的excel如下所示。
第2步:現(xiàn)在,我們將執(zhí)行所有百分比計(jì)算器,所有指定的參數(shù)。
第3步:讓我們創(chuàng)建通用的方法來訪問使用導(dǎo)入JARS Excel文件。這些方法可以幫助我們獲得一個(gè)特定的單元格數(shù)據(jù)或設(shè)置一個(gè)特定的單元格的數(shù)據(jù)等。
import java.io.*;
import org.apache.poi.xssf.usermodel.*;
public class excelutils
{
private XSSFSheet ExcelWSheet;
private XSSFWorkbook ExcelWBook;
//Constructor to connect to the Excel with sheetname and Path
public excelutils(String Path, String SheetName) throws Exception
{
try
{
// Open the Excel file
FileInputStream ExcelFile = new FileInputStream(Path);
// Access the required test data sheet
ExcelWBook = new XSSFWorkbook(ExcelFile);
ExcelWSheet = ExcelWBook.getSheet(SheetName);
}
catch (Exception e)
{
throw (e);
}
}
//This method is to set the rowcount of the excel.
public int excel_get_rows() throws Exception
{
try
{
return ExcelWSheet.getPhysicalNumberOfRows();
}
catch (Exception e)
{
throw (e);
}
}
//This method to get the data and get the value as strings.
public String getCellDataasstring(int RowNum, int ColNum) throws Exception
{
try
{
String CellData = ExcelWSheet.getRow(RowNum).getCell(ColNum).getStringCellValue();
System.out.println("The value of CellData " + CellData);
return CellData;
}
catch (Exception e)
{
return "Errors in Getting Cell Data";
}
}
//This method to get the data and get the value as number.
public double getCellDataasnumber(int RowNum, int ColNum) throws Exception
{
try
{
double CellData = ExcelWSheet.getRow(RowNum).getCell(ColNum).getNumericCellValue();
System.out.println("The value of CellData " + CellData);
return CellData;
}
catch (Exception e)
{
return 000.00;
}
}
}
第4步:現(xiàn)在,添加它將訪問,我們已經(jīng)開發(fā)了Excel的方法,主要的方法。
import java.io.*;
import org.apache.poi.xssf.usermodel.*;
public class excelutils
{
private XSSFSheet ExcelWSheet;
private XSSFWorkbook ExcelWBook;
//Constructor to connect to the Excel with sheetname and Path
public excelutils(String Path, String SheetName) throws Exception
{
try
{
// Open the Excel file
FileInputStream ExcelFile = new FileInputStream(Path);
// Access the required test data sheet
ExcelWBook = new XSSFWorkbook(ExcelFile);
ExcelWSheet = ExcelWBook.getSheet(SheetName);
}
catch (Exception e)
{
throw (e);
}
}
//This method is to set the rowcount of the excel.
public int excel_get_rows() throws Exception
{
try
{
return ExcelWSheet.getPhysicalNumberOfRows();
}
catch (Exception e)
{
throw (e);
}
}
//This method to get the data and get the value as strings.
public String getCellDataasstring(int RowNum, int ColNum) throws Exception
{
try
{
String CellData = ExcelWSheet.getRow(RowNum).getCell(ColNum).getStringCellValue();
//Cell = ExcelWSheet.getRow(RowNum).getCell(ColNum);
//String CellData = Cell.getStringCellValue();
System.out.println("The value of CellData " + CellData);
return CellData;
}
catch (Exception e)
{
return "Errors in Getting Cell Data";
}
}
//This method to get the data and get the value as number.
public double getCellDataasnumber(int RowNum, int ColNum) throws Exception
{
try
{
double CellData = ExcelWSheet.getRow(RowNum).getCell(ColNum).getNumericCellValue();
//Cell = ExcelWSheet.getRow(RowNum).getCell(ColNum);
//String CellData = Cell.getStringCellValue();
System.out.println("The value of CellData " + CellData);
return CellData;
}
catch (Exception e)
{
return 000.00;
}
}
}
在執(zhí)行腳本,輸出顯示在控制臺(tái)中,如下圖所示。