selenium Excel数据驱动测试(基于 TestNG Apache POI)

阅读: 评论:0

selenium Excel数据驱动测试(基于 TestNG Apache POI)

selenium Excel数据驱动测试(基于 TestNG Apache POI)

玩成功能与上一篇类似,数据源由 CSV 文件改为 Excel 文件。

输入数据如下:

自动化测试selenium
蝙蝠侠主演迈克尔
超人导演圆谷英二
生化危机编剧安德森

另存为TestData1.xlsx至D盘

需要通过.html下载最新的poi-bin-3.16-beta2-20170202.zip文件。

解压后将所有.jar文件导入当前Dynamic Web Project 中。

新建TestByExcel.java

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import org.junit.Assert;
stng.annotations.Test;
stng.annotations.DataProvider;
stng.annotations.BeforeMethod;
stng.annotations.AfterMethod;
import urrent.TimeUnit;
import org.openqa.selenium.*;
import org.openqa.selenium.firefox.*;
import org.openqa.selenium.support.ui.ExpectedCondition;
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 TestByExcel {public WebDriver driver;String url="";@DataProvider(name="TestData")public static Object[][] words() throws IOException{return getTestData("d:\","TestData1.xlsx","Sheet1");}@Test(dataProvider="TestData")public void testSearch(String words1, String words2, String result){driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);("");driver.findElement(By.id("kw")).sendKeys(words1+ " "+words2);driver.findElement(By.id("su")).click();try {Thread.sleep(10000);} catch (InterruptedException e) {e.printStackTrace();}Assert.PageSource().contains(result));}@BeforeMethodpublic void BeforeMethod(){driver =new FirefoxDriver();}@AfterMethodpublic void AfterMethod(){driver.quit();}//从Excel文件获取测试数据的静态方法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("."));//判断是.xlsx还是.xls的文件并进行实例化if(fileExtensionName.equals(".xlsx")){Workbook = new XSSFWorkbook(inputStream);}else if(fileExtensionName.equals(".xls")){Workbook = new HSSFWorkbook(inputStream);}//通过sheetName生成Sheet对象Sheet Sheet = Sheet(SheetName);int rowCount = LastRowNum()-FirstRowNum();List<Object[]> records = new ArrayList<Object[]>();for(int i=0 ; i<rowCount+1; i++){Row row = Row(i);String fields[] = new LastCellNum()];for(int j=0;j&LastCellNum();j++){fields[j] = Cell(j).getStringCellValue();}records.add(fields);}Object[][] results = new Object[records.size()][];for (int i = 0 ; i < records.size();i++){results[i] = (i);}return results;}
}

 (Results of running class TestByCVS如下图所示)



(TestNG Report如下图所示:)


TestNG reports:



本文发布于:2024-02-02 06:01:30,感谢您对本站的认可!

本文链接:https://www.4u4v.net/it/170682489141838.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:测试   数据   Excel   selenium   Apache
留言与评论(共有 0 条评论)
   
验证码:

Copyright ©2019-2022 Comsenz Inc.Powered by ©

网站地图1 网站地图2 网站地图3 网站地图4 网站地图5 网站地图6 网站地图7 网站地图8 网站地图9 网站地图10 网站地图11 网站地图12 网站地图13 网站地图14 网站地图15 网站地图16 网站地图17 网站地图18 网站地图19 网站地图20 网站地图21 网站地图22/a> 网站地图23