包: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!");
}
}