package testNGWithDataDriven;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.testng.Assert;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.AfterMethod;
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.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class TestNGExcelDriven {
public WebDriver driver;
String baseUrl = "http://www.sogou.com";
@DataProvider(name="testData")
public static Object[][] words() throws IOException{
return getTestData("F:\\TestNGWithDataDriven\\dataDriven\\src\\test\\java\\testData", "testData.xlsx", "Sheet1");
}
@Test(dataProvider = "testData")
public void testSearchExcel(String searchWord1, String searchWord2, String searchWord3, String searchResult){
driver.get(baseUrl + "/");
driver.findElement(By.id("query")).sendKeys(searchWord1+" "+searchWord2+" "+searchWord3);
driver.findElement(By.id("stb")).click();
Assert.assertTrue(driver.getPageSource().contains(searchResult));
}
@BeforeMethod
public void beforeMethod(){
driver = new ChromeDriver();
}
@AfterMethod
private void afterMethod(){
driver.quit();
}
public static Object[][] getTestData(String filePath, String fileName, String sheetName) throws IOException{
File file = new File(filePath + "\\" + fileName);
//建立FileInputStream對象用于讀取Excel檔案
FileInputStream inputStream = new FileInputStream(file);
//聲明Workbook對象
Workbook workbook = null;
//擷取檔案名參數的擴充名,判斷是.xlsx檔案還是.xls檔案
String fileExtensionName = fileName.substring(fileName.indexOf("."));
//如果是.xlsx,則用XSSFWorkbook對象進行執行個體化,如果是.xls則使用HSSFWorkbook對象進行執行個體化
if (fileExtensionName.equals(".xlsx")){
workbook = new XSSFWorkbook(inputStream);
}
else if (fileExtensionName.equals(".xls")) {
workbook = new HSSFWorkbook(inputStream);
}
//通過sheetName參數生成Sheet對象
Sheet sheet = workbook.getSheet(sheetName);
//擷取Excel資料檔案Sheet1中資料的行數,getLastRowNum方法擷取資料的最後一行行号
//getFirstRowNum方法擷取資料的第一行行号,相減之後算出資料的行數
//Excel行和列都是從0開始
int rowCount = sheet.getLastRowNum()-sheet.getFirstRowNum();
//建立名為records的list對象來存儲從Excel資料檔案讀取的資料
List<Object[]> records = new ArrayList<Object[]>();
//使用兩個for循環周遊Excel資料檔案除去第一行外所有資料
//是以i從1開始,而不是從0開始
for (int i = 1; i<rowCount+1; i++){
Row row = sheet.getRow(i);
//聲明一個數組,用來存儲Excel資料檔案每行中的資料,數組的大小用getLastCellNum辦法來進行動态聲明,實作測試資料個數和數組大小相一緻
String fields[] = new String[row.getLastCellNum()];
for (int j = 0; j<row.getLastCellNum();j++){
//調用getCell和getStringCellValue方法擷取Excel檔案中的單元格資料
fields[j] = row.getCell(j).getStringCellValue();
}
//将fields的資料兌現存儲到records的list中
records.add(fields);
}
//定義函數傳回值,即Object[][]
//将存儲測試資料的list轉換為一個Object的二維數組
Object[][] results = new Object[records.size()][];
//設定二維數組每行的值,每行是一個Object對象
for (int i = 0; i<records.size(); i++){
results[i] = records.get(i);
}
return results;
}
}