您的位置:軟件測試 > 開源軟件測試 > 開源功能測試工具 > Selenium
Selenium Webdriver讀取excel進(jìn)行數(shù)據(jù)驅(qū)動(dòng)測試
作者:網(wǎng)絡(luò)轉(zhuǎn)載 發(fā)布時(shí)間:[ 2015/3/3 17:48:57 ] 推薦標(biāo)簽:Selenium 單元測試 測試工具

  近做自動(dòng)化需要從文件讀取數(shù)據(jù)做參數(shù)化,網(wǎng)上發(fā)現(xiàn)一個(gè)不錯(cuò)的解決方案。
  準(zhǔn)備:新建一個(gè)excel文件,文件名為測試類名,sheet名為測試方法名
  excel第一行為標(biāo)題,從第二行開始為測試數(shù)據(jù)
  build path:jxl.jar
  code:
1 import java.io.FileInputStream;
2 import java.io.InputStream;
3 import java.util.HashMap;
4 import java.util.Iterator;
5 import java.util.Map;
6
7 import org.testng.Assert;
8
9 import jxl.*;
10
11 /**
12  * Excel放在Data文件夾下</p>
13  * Excel命名方式:測試類名.xls</p>
14  * Excel的sheet命名方式:測試方法名</p>
15  * Excel第一行為Map鍵值</p>
16  * 代碼參考鄭鴻志的Blog
17  * {@link www.zhenghongzhi.cn/post/42.html}
18  * @ClassName: ExcelDataProvider
19  * @Description: TODO(讀取Excel數(shù)據(jù))
20  */
21 public class ExcelDataProvider implements Iterator<Object[]> {
22
23     private Workbook book         = null;
24     private Sheet    sheet        = null;
25     private int      rowNum       = 0;
26     private int      currentRowNo = 0;
27     private int      columnNum    = 0;
28     private String[] columnnName;
29
30     public ExcelDataProvider(String classname, String methodname) {
31
32         try {
33
34             int dotNum = classname.indexOf(".");
35
36             if (dotNum > 0) {
37                 classname = classname.substring(classname.lastIndexOf(".") + 1,
38                         classname.length());
39             }
40             //從/data文件夾下讀取以類名命名的excel文件
41             String path = "data/" + classname + ".xls";
42             InputStream inputStream = new FileInputStream(path);
43
44             book = Workbook.getWorkbook(inputStream);
45             //取sheet
46             sheet = book.getSheet(methodname);
47             rowNum = sheet.getRows();
48             Cell[] cell = sheet.getRow(0);
49             columnNum = cell.length;
50             columnnName = new String[cell.length];
51
52             for (int i = 0; i < cell.length; i++) {
53                 columnnName[i] = cell[i].getContents().toString();
54             }
55             this.currentRowNo++;
56
57         } catch (Exception e) {
58             e.printStackTrace();
59             Assert.fail("unable to read Excel data");
60         }
61     }
62
63     public boolean hasNext() {
64
65         if (this.rowNum == 0 || this.currentRowNo >= this.rowNum) {
66
67             try {
68                 book.close();
69             } catch (Exception e) {
70                 e.printStackTrace();
71             }
72             return false;
73         } else {
74             // sheet下一行內(nèi)容為空判定結(jié)束
75             if ((sheet.getRow(currentRowNo))[0].getContents().equals(""))
76                 return false;
77             return true;
78         }
79     }
80
81     public Object[] next() {
82
83         Cell[] c = sheet.getRow(this.currentRowNo);
84         Map<String, String> data = new HashMap<String, String>();
85         // List<String> list = new ArrayList<String>();
86
87         for (int i = 0; i < this.columnNum; i++) {
88
89             String temp = "";
90
91             try {
92                 temp = c[i].getContents().toString();
93             } catch (ArrayIndexOutOfBoundsException ex) {
94                 temp = "";
95             }
96
97             // if(temp != null&& !temp.equals(""))
98             // list.add(temp);
99             data.put(this.columnnName[i], temp);
100         }
101         Object object[] = new Object[1];
102         object[0] = data;
103         this.currentRowNo++;
104         return object;
105     }
106
107     public void remove() {
108         throw new UnsupportedOperationException("remove unsupported.");
109     }
110 }

軟件測試工具 | 聯(lián)系我們 | 投訴建議 | 誠聘英才 | 申請使用列表 | 網(wǎng)站地圖
滬ICP備07036474 2003-2017 版權(quán)所有 上海澤眾軟件科技有限公司 Shanghai ZeZhong Software Co.,Ltd