selenium WebDriver:使用TestNG、POI和Excel文件进行数据驱动

package cn.gloryroad;

import org.testng.annotations.Test;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.DataProvider;
import java.io.File;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

import org.testng.Assert;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebDriver.Navigation;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.WebDriverWait;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
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;;

public class TestDataDrivenByExcelFile {

public WebDriver driver;
String baseUrl = "http://www.baidu.com/";


@DataProvider(name = "testData")
public static Object[][] words() throws IOException {

return getTestData("D:\\","testData.xls","sheet1");
}


@Test(dataProvider="testData")
public void testSearch(String searchWord1, String searchWord2,
String SearchResult) {


driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);

Navigation navigation = driver.navigate();
navigation.to("http://www.baidu.com");
driver.findElement(By.id("kw"))
.sendKeys(searchWord1 + "" + searchWord2);
driver.findElement(By.id("su")).click();

(new WebDriverWait(driver, 10)).until(new ExpectedCondition<Boolean>() {
public Boolean apply(WebDriver d) {
return d.findElement(By.id("foot")).getText()
.contains("搜索帮助");
}
});


Assert.assertTrue(driver.getPageSource().contains(SearchResult));

}

@BeforeMethod
public void beforeMethod() {
System.setProperty("webdriver.ie.driver", "D:\\IEDriverServer.exe");
driver = new InternetExplorerDriver();
}

@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+1;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[i]);
}
return results;

}

}

转载于:https://www.cnblogs.com/Treasa/p/5061515.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值