import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
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.junit.Assert;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

public class TestDriverExcel2007 {
WebDriver driver;
String baseurl="http://www.sogou.com/";
static String path = "d:/selenium/";
static String fileName = "testData";
static String fileType = "xls";
static int sheetIndex=0;
@BeforeMethod
public void beforeMethod(){
driver=new FirefoxDriver();
driver.get(baseurl);
}
@AfterMethod
public void afterMethod(){
driver.quit();
}
@DataProvider(name="testData")
public static Object[][]words()throws Exception{
return getTestData(path,fileName,fileType,sheetIndex);

}
@Test(dataProvider="testData")
public void searchTest(String searchWord1,String searchWord2){
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) {
// TODO Auto-generated method stub
return d.findElement(By.id("s_footer")).getText().contains("搜索帮助");
}

});
Assert.assertTrue(driver.getPageSource().contains(searchWord1));
}

public static Object[][] getTestData(String path,String fileName,String fileType,int sheetIndex) throws IOException
{
InputStream stream = new FileInputStream(path+fileName+"."+fileType);
Workbook wb = null;
if (fileType.equals("xls")) {
wb = new HSSFWorkbook(stream);
}
else if (fileType.equals("xlsx")) {
wb = new XSSFWorkbook(stream);
}
else {
System.out.println("您输入的excel格式不正确");
}
Sheet sheet1 = wb.getSheetAt(sheetIndex);
// for (Row row : sheet1) {
// for (Cell cell : row) {
// row.getCell(1).setCellType(Cell.CELL_TYPE_STRING);
// System.out.print(cell.getStringCellValue()+" ");
//
// }
// System.out.println();
// }
// return null;

int rowCount=sheet1.getLastRowNum()-sheet1.getFirstRowNum();
List<Object[]>records=new ArrayList<Object[]>();
for (Row row : sheet1) {
String fields[]=new String[row.getLastCellNum()];
// for (Cell cell : row) {
// row.getCell(1).setCellType(Cell.CELL_TYPE_STRING);
// System.out.print(cell.getStringCellValue()+" ");
// }
for(int j=0;j<row.getLastCellNum();j++){
row.getCell(j).setCellType(Cell.CELL_TYPE_STRING);
fields[j]=row.getCell(j).getStringCellValue();
}
records.add(fields);

System.out.println();
}
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;
}

}

最新文章

  1. 【每日一linux命令2】命令执行顺序:
  2. iOS开发UI篇—CAlayer(创建图层)
  3. Java 社区论坛 - Sym 1.6.0 发布
  4. HighChart报表之饼图
  5. 使用Spring Data JPA查询时,报result returns more than one elements异常
  6. ASP.NET Web数据控件
  7. C/C++中static关键字详解
  8. UHF桌面式发卡器
  9. GT-n8000平板开机密码忘记 解决办法
  10. 见过的最好AWK手册
  11. Altium 9中正确的差分对走线方法
  12. Linux学习4——Vim和Bash
  13. linux yum命令
  14. swing-窗体添加背景图片的2种方法
  15. Mysql 5.7 Windows 版本(zip)的安装简单过程
  16. MySQL数据库的基本使用简单易懂
  17. VS2013 Ctrl+Shift+F 没反应
  18. python简说(二十六)异常
  19. android开发 gradle 总结
  20. git命令行使用帮助

热门文章

  1. [Oracle]ORA-01499的处理
  2. PostgreSQL的PITR中,对 unfilled wal log 如何处理为好
  3. 【LG3247】[HNOI2016]最小公倍数
  4. Openstack入门篇(十一)之neutron服务(控制节点)的部署与测试
  5. Oracle Database Link 连接数据库复制数据
  6. 提交到开源git时出现:fatal: refusing to merge unrelated histories的解决办法
  7. webstorm 使用svn
  8. 解决 sr 的端口占用问题
  9. Python抓取歌词自制FreeStyle
  10. 从零搭建HBase集群