TestNG+Selenium Webdriver 数据(Excel)驱动的方法
2014-11-06 10:38
411 查看
1.下载 jxl.jar 复制到测试项目的 lib 下,在项目中新建数据驱动类
ExcelData.java
[java]
view plaincopyprint?
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 输入输出流的错误。
测试类调用:TheExcelTest.java
[java]
view plaincopyprint?
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
路径下报告:
ExcelData.java
[java]
view plaincopyprint?
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 输入输出流的错误。
测试类调用:TheExcelTest.java
[java]
view plaincopyprint?
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
路径下报告:
相关文章推荐
- TestNG+Selenium Webdriver 数据(Excel)驱动的方法
- TestNG+Selenium Webdriver 数据(Excel)驱动的方法
- web自动化_浏览器驱动chromedriver安装方法(适用RF框架/Selenium/Appium)
- selenium webdriver或Appium+testng+ant+jenkins的集成方法
- selenium webdriver 学习总结-数据驱动(六)
- selenium webdriver 学习总结-数据驱动(六)
- selenium webdriver 学习总结-数据驱动(六)
- selenium webdriver 学习总结-数据驱动(六)
- selenium webdriver学习--利用POI实现数据驱动
- selenium webdriver启动不同浏览器驱动的方法
- selenium Webdriver 封装方法
- 用Maven在Eclipse中配置Selenium WebDriver——方法2
- Selenium + Webdriver 学习(四) 元素定位方法
- 转:WebDriver(Selenium2) 判断页面是否刷新的方法
- selenium_webdriver 驱动不了浏览器问题
- Selenium2.0介绍——WebDriver两种驱动浏览器的方式.
- 最简便安装python+selenium-webdriver环境方法
- testng 数据驱动方法
- UI TA: selenium webdriver + junit 鼠标悬停,出现另一个元素,点击这个元素的解决方法
- UI TA: selenium webdriver + junit 鼠标悬停,出现另一个元素,点击这个元素的解决方法