使用poi进行数据驱动测试

package selenium;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.Assert;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

public class TestDataDrivenByExcelFile {
	public WebDriver driver;
	String baseUrl = "http://www.sogou.com/";

	// 使用注解DataProvider,将数据集合命名为"testData"
	@DataProvider(name = "testData")
	public static Object[][] words() throws IOException {
		return getTestData("d:\\", "testData.xlsx", "sheet1");

	}

	@Test(dataProvider = "testData")
	public void testSearch(String searchWord1, String searchWord2, String searchResult) {
		driver.get("url");
		driver.findElement(By.id("query")).sendKeys(searchWord1 + " " + searchWord2);
		// ......
		// 使用显示等待方式,确认页面已经加载完成,页面底部的关键字
		// "搜索帮助"已经显示在页面上
		(new WebDriverWait(driver, 10)).until(new ExpectedCondition<Boolean>() {
			public Boolean apply(WebDriver d) {
				return d.findElement(By.id("s_footer")).getText().contains("搜索帮助");
			}
		});
		// 在Excel文件每行的前两个单元格内容作为搜索词汇的情况下,断言搜索结果页面是否包含Excel
		Assert.assertTrue(driver.getPageSource().contains(searchResult));
	}

	@BeforeMethod
	public void beforeMethod() {
		driver = new ChromeDriver();
	}

	@AfterMethod
	public void afterMethod() {
		driver.quit();
	}

	// 从Excel文件获取测试数据的静态方法
	public static Object[][] getTestData(String filePath, String fileName, String sheetName) throws IOException {
		// 根据参数传入的数据文件路径和文件名称,组合出Excel数据文件的绝对路径
		// 声明一个File文件对象
		File file = new File(filePath + "\\" + fileName);
		// 创建FileInputStream对象用于读取Excel文件
		FileInputStream inputStream = new FileInputStream(file);
		// 声明Workbook对象
		Workbook Workbook = null;
		// 获取文件名称参数的扩展名,判断是xlsx还是xls文件
		String fileExtensionName = fileName.substring(fileName.indexOf("."));
		// 判断如果是xlsx使用XSSFWorkbook对象进行实例化
		// 判断如果是xls使用SSFWorkbook对象进行实例化
		if (fileExtensionName.equals(".xlsx")) {
			Workbook = new XSSFWorkbook(inputStream);
		} else if (fileExtensionName.equals(".xls")) {
			Workbook = new HSSFWorkbook(inputStream);
		}
		// 通过sheetName 参数,生成Sheet对象
		Sheet sheet = Workbook.getSheet(sheetName);
		// 获取Excel数据文件Sheet1中数据的行数,getLastRowNum方法获取数据的最后一行行号
		// getFirstRowNum方法获取数据的第一行行号,相减之后算出数据的行数
		// 注意:Excel文件的行号和列号都是从0开始的
		int rowCount = sheet.getLastRowNum() - sheet.getFirstRowNum();
		// 创建名为records的list对象来存储从Excel数据文件读取的数据
		List<Object[]> records = new ArrayList<Object[]>();
		// 使用两个for遍历Excel数据文件的所有数据(除了第1行,第1行是数据列名称)
		// 所以i从1开始,而不是从0开始
		for (int i = 1; i < rowCount + i; i++) {
			// 使用getRow方法获取行对象
			Row row = sheet.getRow(i);
			// 声明一个数组,用来存储Excel数据文件每行中的3个数据,数组的大小用
			// getLastCellNum办法来进行动态声明,实现测试数据个数和数组大小相一致
			String fields[] = new String[row.getLastCellNum()];
			for (int j = 0; j < row.getLastCellNum(); j++) {
				// 调用getCell和getStringCellValue方法获取Excel文件中的单元格数据
				fields[j] = row.getCell(j).getStringCellValue();
			}
			// 将fileds的数据对象存储到records的list中
			records.add(fields);
		}

		// 定义函数返回值,即Object[][]
		// 将存储测试数据的list转换为一个Object的二维数组
		Object[][] results = new Object[records.size()][];
		// 设置二维数组每行的值,每行是一个Object对象
		for (int i = 0; i < records.size(); i++) {
			results[i] = records.get(i);

		}
		return results;

	}
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值