selenium webdriver學習--利用POI實現資料驅動
包:HSSF 讀寫Excel xls格式文件;
XSSF 讀寫Excel ooxml、xlsx格式;
HWPT 讀寫Word、doc格式
HSLF 讀寫ppt;
HDGF 讀寫Visio;
HPBF 讀寫publisher;
HSMF 讀寫Outlook;
類:HSSFWorkBook Excel文件物件;
HSSFSheet Excel表單;
HSSFCell Excel表格子單元;
HSSFRow Excel行;
HSSFFont Excel字型;
HSSFDateFormat Excel格子單元的日期格式;
HSSFHeader sheet的頁首;
HSSFFooter sheet的頁尾
HSSFCellStyle 格子單元格式
HSSFDateUtil 日期
HSSFPrintSetup 列印
HSSFErrorConstants 錯誤資訊表
主要是用了對Excel處理的包,步驟其實也不難。
先宣告個文件物件,之後再就是sheet表單,最後就是行和列了。你要去的某個東西就好比是一所屋子裡某個櫃子抽屜內的物品,宣告workbook就是進入房子,sheet就是你要找的抽屜所在的櫃子,根據行和列既可以確定。
下面是寫的一個簡單的例項,大家參考下吧。
package com.monkey.selenium; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.math.BigDecimal; import java.util.concurrent.TimeUnit; import org.apache.poi.xssf.usermodel.XSSFCell; import org.apache.poi.xssf.usermodel.XSSFRow; import org.apache.poi.xssf.usermodel.XSSFSheet; import org.apache.poi.xssf.usermodel.XSSFWorkbook; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.firefox.FirefoxDriver; public class LoginExcelTest { public static void main(String[] args) throws IOException { String filepath = "D:\\POI\\logininfo.xlsx"; System.setProperty("webdriver.firefox.bin", "D:\\Program Files (x86)\\Mozilla Firefox\\firefox.exe"); WebDriver driver = new FirefoxDriver(); driver.get("http://ip:10103/login.html"); //獲取URL driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); //開始獲取Excel檔案內的登入資訊 FileInputStream in = new FileInputStream(filepath); XSSFWorkbook workbook = new XSSFWorkbook(in); XSSFSheet sheet = workbook.getSheetAt(0); XSSFRow row = sheet.getRow(1); int i = 0; XSSFCell cell = row.getCell(i); i++; Double userName = cell.getNumericCellValue(); XSSFCell cell2 = row.getCell(i); Double passWord = cell2.getNumericCellValue(); System.out.println(userName + "," + passWord); in.close(); //傳遞引數使用者名稱和密碼文字框 driver.findElement(By.id("name")).sendKeys((new BigDecimal(userName)).toString()); driver.findElement(By.id("pwd")).sendKeys((new BigDecimal(passWord)).toString()); driver.findElement(By.id("loginBtn")).click(); driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); String url = driver.getCurrentUrl(); String result = null; //寫入測試結果 if ("http://10.100.143.26:10103/usrHome.html".equals(url)) { result = "pass"; }else { result = "fail"; }; XSSFCell newCell = row.createCell(2); newCell.setCellValue(result); FileOutputStream out = new FileOutputStream(filepath); workbook.write(out); workbook.close(); out.close(); driver.quit(); System.out.println("Down!"); } }