selenium使用execl实现数据驱动测试
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
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.junit.Assert;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
public class TestDriverExcel2007 {
WebDriver driver;
String baseurl="http://www.sogou.com/";
static String path = "d:/selenium/";
static String fileName = "testData";
static String fileType = "xls";
static int sheetIndex=0;
@BeforeMethod
public void beforeMethod(){
driver=new FirefoxDriver();
driver.get(baseurl);
}
@AfterMethod
public void afterMethod(){
driver.quit();
}
@DataProvider(name="testData")
public static Object[][]words()throws Exception{
return getTestData(path,fileName,fileType,sheetIndex);
}
@Test(dataProvider="testData")
public void searchTest(String searchWord1,String searchWord2){
driver.findElement(By.id("query")).sendKeys(searchWord1+""+searchWord2);
driver.findElement(By.id("stb")).click();
(new WebDriverWait(driver,10)).until(new ExpectedCondition<Boolean>(){
@Override
public Boolean apply(WebDriver d) {
// TODO Auto-generated method stub
return d.findElement(By.id("s_footer")).getText().contains("搜索帮助");
}
});
Assert.assertTrue(driver.getPageSource().contains(searchWord1));
}
public static Object[][] getTestData(String path,String fileName,String fileType,int sheetIndex) throws IOException
{
InputStream stream = new FileInputStream(path+fileName+"."+fileType);
Workbook wb = null;
if (fileType.equals("xls")) {
wb = new HSSFWorkbook(stream);
}
else if (fileType.equals("xlsx")) {
wb = new XSSFWorkbook(stream);
}
else {
System.out.println("您输入的excel格式不正确");
}
Sheet sheet1 = wb.getSheetAt(sheetIndex);
// for (Row row : sheet1) {
// for (Cell cell : row) {
// row.getCell(1).setCellType(Cell.CELL_TYPE_STRING);
// System.out.print(cell.getStringCellValue()+" ");
//
// }
// System.out.println();
// }
// return null;
int rowCount=sheet1.getLastRowNum()-sheet1.getFirstRowNum();
List<Object[]>records=new ArrayList<Object[]>();
for (Row row : sheet1) {
String fields[]=new String[row.getLastCellNum()];
// for (Cell cell : row) {
// row.getCell(1).setCellType(Cell.CELL_TYPE_STRING);
// System.out.print(cell.getStringCellValue()+" ");
// }
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);
System.out.println();
}
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;
}
}
selenium使用execl实现数据驱动测试的更多相关文章
- selenium自动化测试之【数据驱动测试】
数据驱动测试是自动化测试的主流设计模式之一,相同的测试脚本使用不同的测试数据来执行,测试数据和测试行为进行了完全的分离,这样的测试脚本设计模式称为数据驱动.实施数据驱动测试的步骤:1.编写测试脚本,脚 ...
- python for selenium 数据驱动测试
# -*- coding:utf-8 -*- """ 数据驱动测试,从 csv 文件中读取数据 """ from selenium impo ...
- Python Selenium 之数据驱动测试
数据驱动模式的测试好处相比普通模式的测试就显而易见了吧!使用数据驱动的模式,可以根据业务分解测试数据,只需定义变量,使用外部或者自定义的数据使其参数化,从而避免了使用之前测试脚本中固定的数据.可以将测 ...
- Python+Selenium笔记(十二):数据驱动测试
(一) 前言 通过使用数据驱动测试,实现对输入值和预期结果的参数化.(例如:输入数据和预期结果可以直接读取Excel文档的数据) (二) ddt 使用ddt执行数据驱动测试,ddt库可以将测试 ...
- Selenium(十四):自动化测试模型介绍、模块化驱动测试案例、数据驱动测试案例
1. 自动化测试模型介绍 随着自动化测试技术的发展,演化为了集中模型:线性测试.模块化驱动测试.数据驱动测试和关键字驱动测试. 下面分别介绍这几种自动化测试模型的特点. 1.1 线性测试 通过录制或编 ...
- Selenium WebDriver 数据驱动测试框架
Selenium WebDriver 数据驱动测试框架,以QQ邮箱添加联系人为示例,测试框架结构如下图,详细内容请阅读吴晓华编著<Selenium WebDiver 实战宝典>: Obje ...
- Python Selenium 之数据驱动测试的实现
数据驱动模式的测试好处相比普通模式的测试就显而易见了吧!使用数据驱动的模式,可以根据业务分解测试数据,只需定义变量,使用外部或者自定义的数据使其参数化,从而避免了使用之前测试脚本中固定的数据.可以将测 ...
- 如何快速掌握DDT数据驱动测试?
1.前言 (网盗概念^-^)相同的测试脚本使用不同的测试数据来执行,测试数据和测试行为完全分离, 这样的测试脚本设计模式称为数据驱动.(网盗结束)当我们测试某个网站的登录功能时,我们往往会使用不同的用 ...
- selenium webdriver testng自动化测试数据驱动
selenium webdriver testng自动化测试数据驱动 selenium webdriver testng自动化测试数据驱动 一.数据驱动测试概念 数据驱动测试是相同的测试脚本使用不同的 ...
随机推荐
- JavaEE笔记(三)
缓存是通过map储存的 hibernate中一对一关系配置 // 如果A中有B 或者B中有A,那么为单项关联 // 如果A和B互有,那么为双向关联(最常用) class A{ private ...
- 【LG3721】[HNOI2017]单旋
[LG3721][HNOI2017]单旋 题面 洛谷 题解 20pts 直接模拟\(spaly\)的过程即可. 100pts 可以发现单旋最大.最小值到根,手玩是有显然规律的,发现只需要几次\(lin ...
- 4710: [Jsoi2011]分特产
4710: [Jsoi2011]分特产 链接 分析: 容斥原理+隔板法. 代码: #include<cstdio> #include<algorithm> #include&l ...
- 为什么volatile不能保证原子性?
为什么volatile能替代简单的锁,却不能保证原子性?这里面涉及volatile,是java中的一个我觉得这个词在Java规范中从未被解释清楚的神奇关键词,在Sun的JDK官方文档是这样形容vola ...
- 洛咕 P2155 [SDOI2008]沙拉公主的困惑
洛咕 P2155 [SDOI2008]沙拉公主的困惑 有个结论,就是如果\(gcd(a,b)=1\),那么\(gcd(a+kb,b)=1\).证明比较显然. 所以这个题目要问的\(n!\)就可以分成\ ...
- 推荐11个实用Python库
1.delorea 非常酷的日期/时间库 from delorean import Delorean EST = "US/Eastern"d = Delorean(timezone ...
- Micro:bit篮球小游戏
尝试用Micro:bit制作一款篮球游戏,材料是利用一些纸箱跟生活周遭可以取得的加上Micro;bit,打造出一个好玩的篮球游戏,制作过程也十分简单. 材料清单 先制作篮板. 接着制作篮球架体. 制作 ...
- Win10家庭版无法打开策略组问题
Win10家庭版无法打开策略组问题 • 复制以下代码至记事本中 @echo off pushd "%~dp0" *.mum >List.txt *.mum >>L ...
- Qt-网易云音乐界面实现-8 主导航的实现-QtabWidget
哎呀,堕落了,快有小两周没哟更新了,是在是没有动力了,浏览量连三位数都没有,是在是没有写下去的信心. 还有就是这个网易云音乐的代码量绝对是不可小视的,完全低估了这个软件的能量.昨天仔细想了一下,写不下 ...
- Proe/Creo 零件库mnu文件制作批处理
proe零件库自定义时需要菜单文件mnu,百度了下网上还没有人制作,偶然间Google时在PTC论坛上看到一德国人分享了自己制作的bat文件用于对文件夹(及子文件夹)产生mnu文件,我在将他的文件翻译 ...