import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
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.xssf.usermodel.XSSFWorkbook;
import org.testng.annotations.DataProvider;
public class PoiEx {
static Object[][] names = null;
static String filePath = "C:\\Users\\Mutturaj\\Desktop";
public static Object[][] readExcel(String filePath, String filename, String sheetName) throws IOException {
File file = new File(filePath + "\\" + filename);
FileInputStream fileInputStream = new FileInputStream(file);
Workbook workbook = null;
String fileextensionname = filename.substring(filename.indexOf("."));
if (fileextensionname.equals(".xlsx")) {
workbook = new XSSFWorkbook(fileInputStream);
} else if (fileextensionname.equals(".xls")) {
workbook = new HSSFWorkbook(fileInputStream);
}
// Get sheet by name
Sheet sheet = workbook.getSheet(sheetName);
// Count rows in the sheet
int rowCount = sheet.getLastRowNum();
int colCount = 2;
names = new Object[rowCount + 1][colCount];
for (int i = 0; i < rowCount + 1; i++) {
Row row = sheet.getRow(i);
int lastCellNumber = row.getLastCellNum();
for (int j = 0; j < lastCellNumber; j++) {
names[i][j] = (row.getCell(j).getStringCellValue());
}
}
return names;
}
@DataProvider(name = "array")
public Object[][] nana() throws IOException {
Object[][] array = PoiEx.readExcel(filePath, "muttu.xlsx", "muttu");
return array;
}
}
import org.testng.annotations.AfterMethod;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
public class NGTest1 {
@BeforeTest()
public void loadFile() {
System.out.println("File open....");
}
@BeforeMethod
public void openBrowser() {
System.out.println("Browser opened************");
}
@AfterMethod
public void closeBrowser() {
System.out.println("**************Close browser");
}
@Test(priority = 1, dataProvider = "array", dataProviderClass = PoiEx.class)
public void add(String first, String second) {
System.out.println(first);
System.out.println(second);
}
@Test(priority = 2)
public void substract() {
System.out.println("Substraction");
}
@Test(priority = 3)
public void Multiply() {
System.out.println("Multiply");
}
@Test(priority = 4)
public void Divide() {
System.out.println("Divide");
}
@AfterTest
public void closeFile() {
System.out.println("File closed.....");
}
}
No comments:
Post a Comment