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实现数据驱动测试的更多相关文章

  1. selenium自动化测试之【数据驱动测试】

    数据驱动测试是自动化测试的主流设计模式之一,相同的测试脚本使用不同的测试数据来执行,测试数据和测试行为进行了完全的分离,这样的测试脚本设计模式称为数据驱动.实施数据驱动测试的步骤:1.编写测试脚本,脚 ...

  2. python for selenium 数据驱动测试

    # -*- coding:utf-8 -*- """ 数据驱动测试,从 csv 文件中读取数据 """ from selenium impo ...

  3. Python Selenium 之数据驱动测试

    数据驱动模式的测试好处相比普通模式的测试就显而易见了吧!使用数据驱动的模式,可以根据业务分解测试数据,只需定义变量,使用外部或者自定义的数据使其参数化,从而避免了使用之前测试脚本中固定的数据.可以将测 ...

  4. Python+Selenium笔记(十二):数据驱动测试

    (一)   前言 通过使用数据驱动测试,实现对输入值和预期结果的参数化.(例如:输入数据和预期结果可以直接读取Excel文档的数据) (二)   ddt 使用ddt执行数据驱动测试,ddt库可以将测试 ...

  5. Selenium(十四):自动化测试模型介绍、模块化驱动测试案例、数据驱动测试案例

    1. 自动化测试模型介绍 随着自动化测试技术的发展,演化为了集中模型:线性测试.模块化驱动测试.数据驱动测试和关键字驱动测试. 下面分别介绍这几种自动化测试模型的特点. 1.1 线性测试 通过录制或编 ...

  6. Selenium WebDriver 数据驱动测试框架

    Selenium WebDriver 数据驱动测试框架,以QQ邮箱添加联系人为示例,测试框架结构如下图,详细内容请阅读吴晓华编著<Selenium WebDiver 实战宝典>: Obje ...

  7. Python Selenium 之数据驱动测试的实现

    数据驱动模式的测试好处相比普通模式的测试就显而易见了吧!使用数据驱动的模式,可以根据业务分解测试数据,只需定义变量,使用外部或者自定义的数据使其参数化,从而避免了使用之前测试脚本中固定的数据.可以将测 ...

  8. 如何快速掌握DDT数据驱动测试?

    1.前言 (网盗概念^-^)相同的测试脚本使用不同的测试数据来执行,测试数据和测试行为完全分离, 这样的测试脚本设计模式称为数据驱动.(网盗结束)当我们测试某个网站的登录功能时,我们往往会使用不同的用 ...

  9. selenium webdriver testng自动化测试数据驱动

    selenium webdriver testng自动化测试数据驱动 selenium webdriver testng自动化测试数据驱动 一.数据驱动测试概念 数据驱动测试是相同的测试脚本使用不同的 ...

随机推荐

  1. Autoanalyze 的注意事项

    磨砺技术珠矶,践行数据之道,追求卓越价值 回到上一级页面:PostgreSQL内部结构与源代码研究索引页    回到顶级页面:PostgreSQL索引页 根据官方文档的说明  http://www.p ...

  2. 【转载】COM 组件设计与应用(一)——起源及复合文件

    原文:http://vckbase.com/index.php/wv/1201.html COM 组件设计与应用 系列文章:http://vckbase.com/index.php/piwz?& ...

  3. 23-[模块]-subprocess模块

    1.调用系统命令 我们经常需要通过Python去执行一条系统命令或脚本,系统的shell命令是独立于你的python进程之外的,每执行一条命令,就是发起一个新进程,通过python调用系统命令或脚本的 ...

  4. Java Swing:JPanel中添加JPanel

    1. JPanel默认布局是FlowLayout,如果不设置父JPanel的布局,则子JPanel自动填满父JPanel. JPanel jpanel = new JPanel(); jpanel.s ...

  5. [HNOI2016]网络 树链剖分,堆

    [HNOI2016]网络 LG传送门 表示乱搞比正解难想. 整体二分很好想吧. 但是为了好写快乐,我们选择三个\(\log\)的乱搞. 先树剖,线段树套堆维护区间最大值.对于一次修改,如果是插入,就把 ...

  6. Sqlite数据库sqlite3命令小记

    SQLite库包含一个名字叫做sqlite3的命令行,它可以让用户手工输入并执行面向SQLite数据库的SQL命令.本文档提供一个样使用sqlite3的简要说明. 开始 启动sqlite3程序,仅仅需 ...

  7. java高并发之锁的使用以及原理浅析

    锁像synchronized同步块一样,是一种线程同步机制.让自Java 5开始,java.util.concurrent.locks包提供了另一种方式实现线程同步机制——Lock.那么问题来了既然都 ...

  8. halcon 手眼标定的坐标转换原理讲解

    原文链接:https://blog.csdn.net/opencv_learner/article/details/82113323 一直以来,对于手眼标定所涉及到的坐标系及坐标系之间的转换关系都没能 ...

  9. AssetBundle一些问题

    AssetBundle划分过细的问题,比如每个资源都是AssetBundle. 加载IO次数过多,从而增大了硬件设备耗能和发热的压力: Unity 5.3 ~ 5.5 版本中,Android平台上在不 ...

  10. python多线程创建与使用(转)

    原文:http://codingpy.com/article/python-201-a-tutorial-on-threads/ 创建多线程 创建多线程主要有2种方式. 使用threading.Thr ...