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自动化测试数据驱动 一.数据驱动测试概念 数据驱动测试是相同的测试脚本使用不同的 ...
随机推荐
- Eclipse Git提交代码,多了一个“工程同名的文件夹”,找不到解决办法!!!
提交代码到Git仓库,有2种方式. 第1种,先在OSChina等平台创建git项目,本地clone,再在本地修改代码提交.√ 这种方式,没任何问题. 不过,我平时不喜欢这么干. 第2种,本地已经有了项 ...
- JavaEE笔记(四)
sql的完整顺序完整的sql语句,由6个子句组成1. from2. where3. group by4. select5. having6. order by # having 和 where的区别w ...
- django学习笔记(2)
Part 2: The admin site ====> Creating an admin user$ python manage.py createsuperuser Username: ...
- /usr/bin/python: can't decompress data; zlib not available 的异常处理
1. 问题背景 使用Pycharm连接远程服务器端pipenv虚拟环境的python解释器,运行python spark脚本时报错如下错误: 2018-09-12 23:56:00 ERROR Exe ...
- SQL Server 查询请求
当SQL Server 引擎接收到用户发出的查询请求时,SQL Server执行优化器将查询请求(Request)和Task绑定,并为Task分配一个Workder,SQL Server申请操作系统的 ...
- 动态加载与插件系统的初步实现(一):反射与MEF解决方案
涉及内容: 反射与MEF解决方案 AppDomain卸载与代理 WinForm.WcfRestService示 PRRT1: 反射实现 插件系统的基本目的是实现宿主与组件的隔离,核心是作为接驳约定的接 ...
- javaweb学习5——JSP
声明:本文只是自学过程中,记录自己不会的知识点的摘要,如果想详细学习JavaWeb,请到孤傲苍狼博客学习,JavaWeb学习点此跳转 本文链接:https://www.cnblogs.com/xdp- ...
- SQL Server Management Studio 键盘快捷键
光标移动键盘快捷键 操作 SQL Server 2012 SQL Server 2008 R2 左移光标 向左键 向左键 右移光标 向右键 向右键 上移光标 向上键 向上键 下移光标 向下键 向下键 ...
- 1.21 贪心入门上午PAT例题题解
1.B1023 #include<cstdio> int a[10]; int main() { for(int i=0;i<=9;i++) { scanf("%d&quo ...
- linux常用命令总结(含选项参数)
• 用户切换 su 切换到root用户并不切换环境 su - root 切换到root用户并切换环境 su redhat 切换到redhat不切换环境 • cd切换目 ...