博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
testNG数据驱动Excel(java)
阅读量:4595 次
发布时间:2019-06-09

本文共 2863 字,大约阅读时间需要 9 分钟。

eclipse环境下搭建的数据驱动框架,需要下载Apache POI 并将其内所有jar包导入Build Path

package china;import org.testng.annotations.Test;import org.testng.Assert;import org.testng.annotations.AfterMethod;import org.testng.annotations.BeforeMethod;import org.testng.annotations.DataProvider;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.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.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;public class TestDataDriverByExcelFile {    public WebDriver driver;    String baseUrl="http://www.sogou.com";  @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(baseUrl);      driver.findElement(By.id("query")).sendKeys(searchWord1+""+searchWord2);      driver.findElement(By.id("stb")).click();      (new WebDriverWait(driver, 10)).until(new ExpectedCondition
() { @Override public Boolean apply(WebDriver d){ return d.findElement(By.id("s_footer")).getText().contains("搜索帮助"); } }); Assert.assertTrue(driver.getPageSource().contains(searchresult)); } @BeforeMethod public void beforeMethod() { System.setProperty("webdriver.chrome.driver", "C:\\chromedriver\\chromedriver.exe"); driver=new ChromeDriver(); } @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
records=new ArrayList
(); for(int i=1;i
records=new ArrayList
();// for(int i=1;i

Excel文档放在D盘中 名字:testData

数据如下

 

转载于:https://www.cnblogs.com/wangyinxu/p/6439280.html

你可能感兴趣的文章
POJ 2356 Find a multiple
查看>>
iptables详解
查看>>
HRBUST 1376 能量项链
查看>>
Thread类的使用
查看>>
Unity-NGUI UILabel换行
查看>>
L1-027 出租
查看>>
刷题总结——蚯蚓(NOIP2016DAY2T2)
查看>>
idea @Override is not allowed when implementing interface method
查看>>
javaScript常用知识点有哪些
查看>>
OpenCV调用摄像头 , 人脸检测demo
查看>>
大数据的本质
查看>>
你真的无聊透顶么?不见得
查看>>
软工每日总结30
查看>>
策略模式
查看>>
负载均衡中使用 Redis 实现共享 Session
查看>>
[转载]用纯css改变下拉列表select框的默认样式
查看>>
Content-Type boundary 问题
查看>>
Filestream/Windows Share导致Alwayson Failover失败
查看>>
What is State of Art?
查看>>
实验二
查看>>