eclipse环境下搭建的数据驱动框架,需要下载Apache POI 并将其内所有jar包导入Build Path
package china; import org.testng.annotations.Test; import org.testng.Assert; import org.testng.annotations.AfterMethod; import org.testng.annotations.BeforeMethod; import org.testng.annotations.DataProvider; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.util.ArrayList; import java.util.List; import org.apache.poi.hssf.usermodel.HSSFWorkbook; //import org.apache.poi.ss.usermodel.Cell; 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; import org.openqa.selenium.support.ui.ExpectedCondition; import org.openqa.selenium.support.ui.WebDriverWait; public class TestDataDriverByExcelFile { public WebDriver driver; String baseUrl="http://www.sogou.com"; @DataProvider(name="testData") public static Object[][] words()throws IOException{ return getTestData("d:\", "testData.xlsx", "Sheet1"); } @Test(dataProvider="testData") public void testSearch(String searchWord1,String searchWord2,String searchresult) { driver.get(baseUrl); driver.findElement(By.id("query")).sendKeys(searchWord1+""+searchWord2); driver.findElement(By.id("stb")).click(); (new WebDriverWait(driver, 10)).until(new ExpectedCondition<Boolean>() { @Override public Boolean apply(WebDriver d){ return d.findElement(By.id("s_footer")).getText().contains("搜索帮助"); } }); Assert.assertTrue(driver.getPageSource().contains(searchresult)); } @BeforeMethod public void beforeMethod() { System.setProperty("webdriver.chrome.driver", "C:\chromedriver\chromedriver.exe"); driver=new ChromeDriver(); } @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;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); return results; } // @SuppressWarnings("deprecation") //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;i++){ // Row row=Sheet.getRow(i); // String fields[]=new String[row.getLastCellNum()]; // for(int j=0;j<row.getLastCellNum();j++){ // Cell cell=row.getCell(j); // cell.setCellType(Cell.CELL_TYPE_STRING); // fields[j]=cell.getStringCellValue(); // } // records.add(fields); // } // Object[][] results=new Object[records.size()][]; // for(int i=0;i<records.size();i++){ // results[i]=records.get(i); // } // return results; } //cell.getStringCellValue()
Excel文档放在D盘中 名字:testData
数据如下