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自动化测试数据驱动 一.数据驱动测试概念 数据驱动测试是相同的测试脚本使用不同的 ...
随机推荐
- Noip前的大抱佛脚----图论
目录 图论 知识点 二分图相关 DFS找环 并查集维护二分图 二分图匹配的不可行边 最小生成树相关 最短路树 最短路相关 负环 多源最短路 差分约束系统 01最短路 k短路 网络流 zkw费用流 做题 ...
- selenium-登录C语言中文网
from selenium import webdriver from selenium.webdriver.support.wait import WebDriverWait from seleni ...
- Mac下 Windows 7 虚拟机搭建SVN服务器的详细步骤(此方法同样适用于单纯的Windows系统搭建SVN)
内容中包含 base64string 图片造成字符过多,拒绝显示
- noip2017普及题解
https://www.luogu.org/problemnew/show/3954 https://www.luogu.org/problemnew/show/3955 https://www.lu ...
- TensorFlow API 汉化
TensorFlow API 汉化 模块:tf 定义于tensorflow/__init__.py. 将所有公共TensorFlow接口引入此模块. 模块 app module:通用入口点脚本. ...
- Redis之数据类型大全
一:String类型 1.set方法:设置key对应的值为string类型的value,如果该key已经存在,则覆盖key对应的value值.所以在redis中key只能有一个. 127.0.0.1: ...
- 如何实现Canvas图像的拖拽、点击等操作
上一篇Canvas的博文写完后,有位朋友希望能对Canvas绘制出来的图像进行点击.拖拽等操作,因为Canvas绘制出的图像能很好的美化.好像是想做炉石什么的游戏,我也没玩过. Canvas在我的理解 ...
- MySQL ZIP Archive 5.7.17 安装方法
1.下载 2.解压缩 3.创建/修改配置文件 在MySQL安装目录下,新建my.ini,内容如下参考 [mysql] # 设置mysql客户端默认字符集 default-character-set=u ...
- JAVA图书管理系统汇总共27个[转]
java图书馆管理系统[优秀毕业设计论文+源码]http://down.51cto.com/data/68350java+sql server图书管理系统 http://down.51cto.com/ ...
- Docker Manager for Docker Swarm deploy
一.Swarm概述 Swarm是Docker公司在2014年12月初发布的一套较为简单的工具,用来管理Docker集群,它将一群Docker宿主机变成一个单一的,虚拟的主机.Swarm使用标准的Doc ...