package cn.gloryroad;
import org.testng.annotations.Test;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.DataProvider;
import java.io.File;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;
import org.testng.Assert;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebDriver.Navigation;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.WebDriverWait;
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;;
public class TestDataDrivenByExcelFile {
public WebDriver driver;
String baseUrl = "http://www.baidu.com/";
@DataProvider(name = "testData")
public static Object[][] words() throws IOException {
return getTestData("D:\\","testData.xls","sheet1");
}
@Test(dataProvider="testData")
public void testSearch(String searchWord1, String searchWord2,
String SearchResult) {
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
Navigation navigation = driver.navigate();
navigation.to("http://www.baidu.com");
driver.findElement(By.id("kw"))
.sendKeys(searchWord1 + "" + searchWord2);
driver.findElement(By.id("su")).click();
(new WebDriverWait(driver, 10)).until(new ExpectedCondition<Boolean>() {
public Boolean apply(WebDriver d) {
return d.findElement(By.id("foot")).getText()
.contains("搜索帮助");
}
});
Assert.assertTrue(driver.getPageSource().contains(SearchResult));
}
@BeforeMethod
public void beforeMethod() {
System.setProperty("webdriver.ie.driver", "D:\\IEDriverServer.exe");
driver = new InternetExplorerDriver();
}
@AfterMethod
public void afterMethod() {
driver.quit();
}
public static Object [][] getTestData (String filePath,String fileName,String sheetName) throws IOException{
File file = new File(filePath + "\\"+ fileName);
FileInputStream inputStream = new FileInputStream(file);
Workbook Workbook = null;
String fileExtensionName = fileName.substring(fileName.indexOf("."));
if(fileExtensionName.equals(".xlsx")){
Workbook = new XSSFWorkbook(inputStream);
}else if (fileExtensionName.equals(".xls")){
Workbook = new HSSFWorkbook(inputStream);
}
Sheet Sheet = Workbook.getSheet(sheetName);
int rowCount = Sheet.getLastRowNum() - Sheet.getFirstRowNum();
List<Object[]> records = new ArrayList <Object[]>();
for (int i=1;i<rowCount+1;i++){
Row row = Sheet.getRow(i);
String fields[] = new String[row.getLastCellNum()];
for(int j = 0;j<row.getLastCellNum();j++){
fields[j]=row.getCell(j).getStringCellValue();
}
records.add(fields);
}
Object[][] results = new Object[records.size()][];
for (int i=0;i<records.size();i++){
results[i]=records.get(i);
System.out.println(results[i]);
}
return results;
}
}