TestNG+Selenium Webdriver 数据(Excel)驱动的方法
1.下载 jxl.jar 复制到测试项目的 lib 下,在项目中新建数据驱动类
ExcelData.Java
- package com.annie;
- import java.io.File;
- import java.util.Iterator;
- import java.util.Map;
- import java.util.TreeMap;
- import java.util.regex.Matcher;
- import jxl.*;
- public class ExcelData implements Iterator<Object[]> {
- private Workbook book = null;
- private Sheet sheet = null;
- private int rowNum = 0;//行数
- private int curRowNo = 0;//当前行数
- private int columnNum = 0;//列数
- private String[] columnnName;//列名
- /*在TestNG中,由@DataProvider(dataProvider="name")修饰的方法读取Exel时,调用此类的构造方法(此方法会得到列名并将当前行移到下一行)执行完后,
- *转到TestNG自己的方法中去,然后由他们调用此类实现的hasNext()、next() 方法;
- *得到一行数据,然后返回给由@Test(dataProvider="name")修饰的方法,如此反复到数据读完为止。
- * @param filepath Excel文件名
- * @param casename用例名
- */
- public ExcelData(String filepath, String casename) {
- try {
- File directory = new File(".");
- String ss = "open.anniewang.newexcel.";
- book = Workbook.getWorkbook(new File(directory.getCanonicalPath()
- + "\\resources\\"
- + ss.replaceAll("\\.", Matcher.quoteReplacement("\\"))
- + filepath + ".xls"));
- this.sheet = book.getSheet(casename);
- this.rowNum = sheet.getRows();
- Cell[] c = sheet.getRow(0);
- this.columnNum = c.length;
- columnnName = new String[c.length];
- for (int i = 0; i < c.length; i++) {
- columnnName[i] = c[i].getContents().toString();
- }
- this.curRowNo++;
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- @Override
- public boolean hasNext() {
- /**
- *方法功能:是否有下一条数据
- *如果行数为0即空sheet或者 当前行数大于总行数
- *就关闭对excel的操作返回false,否则返回true
- */
- if (this.rowNum == 0 || this.curRowNo >= this.rowNum) {
- try {
- book.close();
- } catch (Exception e) {
- e.printStackTrace();
- }
- return false;
- } else
- return true;
- }
- @Override
- public Object[] next() {
- /* 方法功能:得到并返回下一行数据
- * 使用for将一行的数据放入TreeMap中(TreeMap默认按照Key值升序排列,HashMap没有排序)
- *然后将Map装入Object[]并返回,且将curRowNo当前行下移
- */
- Cell[] c = sheet.getRow(this.curRowNo);
- Map<String, String> s = new TreeMap<String, String>();
- for (int i = 0; i < this.columnNum; i++) {
- String temp = "";
- try {
- temp = c[i].getContents().toString();
- } catch (ArrayIndexOutOfBoundsException ex) {
- temp = "";
- }
- s.put(this.columnnName[i], temp);
- }
- Object r[] = new Object[1];
- r[0] = s;
- this.curRowNo++;
- return r;
- }
- @Override
- public void remove() {
- throw new UnsupportedOperationException("remove unsupported.");
- }
- }
- package com.annie;
- import java.io.File;
- import java.util.Iterator;
- import java.util.Map;
- import java.util.TreeMap;
- import java.util.regex.Matcher;
- import jxl.*;
- public class ExcelData implements Iterator<Object[]> {
- private Workbook book = null;
- private Sheet sheet = null;
- private int rowNum = 0;//行数
- private int curRowNo = 0;//当前行数
- private int columnNum = 0;//列数
- private String[] columnnName;//列名
- /*在TestNG中,由@DataProvider(dataProvider="name")修饰的方法读取Exel时,调用此类的构造方法(此方法会得到列名并将当前行移到下一行)执行完后,
- *转到TestNG自己的方法中去,然后由他们调用此类实现的hasNext()、next() 方法;
- *得到一行数据,然后返回给由@Test(dataProvider="name")修饰的方法,如此反复到数据读完为止。
- * @param filepath Excel文件名
- * @param casename用例名
- */
- public ExcelData(String filepath, String casename) {
- try {
- File directory = new File(".");
- String ss = "open.anniewang.newexcel.";
- book = Workbook.getWorkbook(new File(directory.getCanonicalPath()
- + "\\resources\\"
- + ss.replaceAll("\\.", Matcher.quoteReplacement("\\"))
- + filepath + ".xls"));
- this.sheet = book.getSheet(casename);
- this.rowNum = sheet.getRows();
- Cell[] c = sheet.getRow(0);
- this.columnNum = c.length;
- columnnName = new String[c.length];
- for (int i = 0; i < c.length; i++) {
- columnnName[i] = c[i].getContents().toString();
- }
- this.curRowNo++;
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- @Override
- public boolean hasNext() {
- /**
- *方法功能:是否有下一条数据
- *如果行数为0即空sheet或者 当前行数大于总行数
- *就关闭对excel的操作返回false,否则返回true
- */
- if (this.rowNum == 0 || this.curRowNo >= this.rowNum) {
- try {
- book.close();
- } catch (Exception e) {
- e.printStackTrace();
- }
- return false;
- } else
- return true;
- }
- @Override
- public Object[] next() {
- /* 方法功能:得到并返回下一行数据
- * 使用for将一行的数据放入TreeMap中(TreeMap默认按照Key值升序排列,HashMap没有排序)
- *然后将Map装入Object[]并返回,且将curRowNo当前行下移
- */
- Cell[] c = sheet.getRow(this.curRowNo);
- Map<String, String> s = new TreeMap<String, String>();
- for (int i = 0; i < this.columnNum; i++) {
- String temp = "";
- try {
- temp = c[i].getContents().toString();
- } catch (ArrayIndexOutOfBoundsException ex) {
- temp = "";
- }
- s.put(this.columnnName[i], temp);
- }
- Object r[] = new Object[1];
- r[0] = s;
- this.curRowNo++;
- return r;
- }
- @Override
- public void remove() {
- throw new UnsupportedOperationException("remove unsupported.");
- }
- }
EXECL数据驱动:ExcelTest.xls
注意:此处的要用office2003扩展名为xls(office 2007 的excel 扩展名为xlsx),否则会报I/O 输入输出流的错误。
- package com.annie;
- import java.util.Iterator;
- import java.util.Map;
- import java.util.Set;
- import org.testng.annotations.DataProvider;
- import org.testng.annotations.Test;
- public class TheExcelTest {
- @Test(dataProvider = "db1")
- public void ts(Map<String, String> data) throws Exception{
- this.prmap(data);
- System.out.println("=====over=====");
- System.out.println("");
- }
- @DataProvider(name = "db1")
- public Iterator<Object[]> data() throws Exception{
- return (Iterator<Object[]>)new ExcelData("ExcelTest","testB");
- }
- public void prmap(Map<String,String>arr){
- Set<String> set=arr.keySet();
- Iterator<String> it=set.iterator();
- while(it.hasNext()){
- String s=(String)it.next();
- System.out.println(arr.get(s));
- }
- }
- }
- package com.annie;
- import java.util.Iterator;
- import java.util.Map;
- import java.util.Set;
- import org.testng.annotations.DataProvider;
- import org.testng.annotations.Test;
- public class TheExcelTest {
- @Test(dataProvider = "db1")
- public void ts(Map<String, String> data) throws Exception{
- this.prmap(data);
- System.out.println("=====over=====");
- System.out.println("");
- }
- @DataProvider(name = "db1")
- public Iterator<Object[]> data() throws Exception{
- return (Iterator<Object[]>)new ExcelData("ExcelTest","testB");
- }
- public void prmap(Map<String,String>arr){
- Set<String> set=arr.keySet();
- Iterator<String> it=set.iterator();
- while(it.hasNext()){
- String s=(String)it.next();
- System.out.println(arr.get(s));
- }
- }
- }
右键:RUN-as- TestNG
或者运行 RUN -as-ANT build
路径下报告:
相关推荐
- 【翻译】谷歌基于数据驱动的软件安全:模型和方法
- 【EXCEL】基于wps的vlookup的数据导入实现方法
- 识别最优的数据驱动特征选择方法以提高分类任务的可重复性
- 【Spark Summit East 2017】加速云上Spark基因测序的数据驱动方法以及案例研究
- 腾讯QQ大数据 :从“增长黑客”谈数据驱动的方法
- ASP导入Excel数据提示:外部数据库驱动程序(1)中的意外错误 解决办法
- 多种方法实现从Excel表格的两列数据中提取不重复(唯一)值
- Excel中的使用相同公式的方法与连续数字的方法,以及在每个单元格数据右下角添加逗号,round函数
- Excel 还在重复输入相同的数据?不用那么麻烦(避免重复输入,批量填充 -- 非手动输入与复制粘贴的快捷精准输入的方法)
- 干货实操!人人都能学会的Excel数据分析方法
- SAP WebClient UI的会话重启原理
- selenium+TestNG.xml简单使用