seleniumWebDriver自动化测试框架_03TestNG和ApachePOIExcel文件数据驱动测试
1.遇到的问题
a.报错信息:FAILED: f
java.lang.NoClassDefFoundError: org/w3c/dom/ElementTraversal
解决方法:maven项目的pom.xml中引入如下依赖包
<dependency>
<groupId>xml-apis</groupId>
<artifactId>xml-apis</artifactId>
<version>1.4.01</version>
</dependency>
b.报错信息:FAILED: f
java.lang.IllegalStateException: Cannotget a text value from a numeric cell
解决办法:此异常常见于类似如下代码中:row.getCell(0).getStringCellValue();
解决办法:先设置Cell的类型,然后就可以把纯数字作为String类型读进来了:
if(row.getCell(0)!=null){
row.getCell(0).setCellType(Cell.CELL_TYPE_STRING);
stuUser.setPhone(row.getCell(0).getStringCellValue());
}
============================
操作步骤:
1.maven项目中引入apache poi的jar包:
<dependencies>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>2.44.0</version>
</dependency>
<dependency>
<groupId>org.jsoup</groupId>
<artifactId>jsoup</artifactId>
<version>1.8.3</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.9</version>
</dependency>
<dependency>
<groupId>xml-apis</groupId>
<artifactId>xml-apis</artifactId>
<version>1.4.01</version>
</dependency>
</dependencies>
2.编辑如下代码:
public class demo1103{
public WebDriver driver;
private static final String filePath="F:\\SeleniumWebDriver\\workspace\\h5";
private static final String fileName="testExcel.xlsx";
private static final String sheetName="Sheet1";
@Test(dataProvider="testData")
public void f(String s1,String s2,String s3) throwsIOException, InterruptedException {
driver.get("http://www.sogou.com");
Thread.sleep(3000);
driver.findElement(By.id("query")).sendKeys(s1+""+s2);
driver.findElement(By.id("stb")).click();
Thread.sleep(5000);
Assert.assertTrue(driver.getPageSource().contains(s3));
Thread.sleep(3000);
}
@BeforeMethod
public voidbeforeMethod() {
System.setProperty("webdriver.firefox.bin", "D:\\firefox\\firefox.exe");
driver=newFirefoxDriver();
}
@AfterMethod
public voidafterMethod() throws InterruptedException {
Thread.sleep(3000);
driver.quit();
}
public staticObject[][] getTestData(String filePath,String fileName,String sheetName) throwsIOException{
Filefile=new File(filePath+"\\"+fileName);
FileInputStreaminputStream=new FileInputStream(file);
Workbookwb=null;
StringfileExtensionName=fileName.substring(fileName.indexOf("."));
if(fileExtensionName.equals(".xlsx")){
wb=newXSSFWorkbook(inputStream);
}else{
wb=newHSSFWorkbook(inputStream);
}
Sheetsheet=wb.getSheet(sheetName);
//System.out.println("第一行行号"+sheet.getFirstRowNum());
//System.out.println("最后一行行号"+sheet.getLastRowNum());
int rowCount=sheet.getLastRowNum()-sheet.getFirstRowNum();
//System.out.print("表格行数"+rowCount);
List<Object[]>records=newArrayList<Object[]>();
for(int i=1;i<=rowCount;i++){
Rowrow=sheet.getRow(i);
String[]fields=new String[row.getLastCellNum()];
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);
}
Object[][]result=new Object[records.size()][];
for(int i=0;i<records.size();i++){
result[i]=records.get(i);
}
return result;
}
@DataProvider(name="testData")
public staticObject[][] words() throws IOException{
return getTestData(filePath, fileName, sheetName);
}
}
执行结果如下:(数据来源与excel表格内容一致)
PASSED:f("11", "12", "13")
PASSED:f("21", "22", "23")