自动化实施总结(三)----数据驱动实现

使用到里面的一个ExcelDataProvider类,但是需要注意的是这个类中的public Object[] next() {}这个方法有一个小缺陷。
/**
* 取下一条数据
*/
public Object[] next() {
Map<String, ArrayList<String>> s = new LinkedHashMap<String, ArrayList<String>>();
boolean ThisIsRange = false;
 
if (this.getSomeRows) {
this.curlogicRowNo = this.rowidlist.get(0);
this.rowidlist.remove(0);
}
int[] curPhysicalRow = this.logicRows.get(this.curlogicRowNo);
 
ThisIsRange = (curPhysicalRow[1] - curPhysicalRow[0]) > 0;
 
for (int i = 0; i < this.columnNum; i++) {
ArrayList<String> temp = new ArrayList<String>();
if (ThisIsRange) {
if (i == 0) {
temp.add(sheet.getRow(curPhysicalRow[0])[i].getContents()
.toString());
} else {
for (int j = curPhysicalRow[0]; j <= curPhysicalRow[1]; j++) {
temp.add(sheet.getRow(j)[i].getContents().toString());
}
}
} else {
if(i>=sheet.getRow(curPhysicalRow[0]).length){
break;
}else{
temp.add(sheet.getRow(curPhysicalRow[0])[i].getContents()==null?"":sheet.getRow(curPhysicalRow[0])[i].getContents()
.toString());
}
}
s.put(this.columnnName[i], temp);
}
Object r[] = new Object[1];
r[0] = s;
this.curlogicRowNo++;
return r;
}
 
 
需要按上面修改下,否则如果你的Excel中某一数据列中的值不足时会引起越界异常出现。
 
以下是使用方法:
   //数据池读取
@DataProvider(name = "dataFortestMethod")
public Iterator<Object[]> dataFortestMethod(Method method)
throws IOException, BiffException {
return (Iterator<Object[]>) new ExcelDataProvider(
"./res/parameters.xls", this.getClass().getSimpleName(),
method.getName());
}
//依赖数据提供者
@Test(dataProvider = "dataFortestMethod")
public void test(Map<String, ArrayList<String>> data) {
 
driver.get(config.getValue("config.LoginUKURL"));
lc = new Login(data.get("username").get(0).toString(), data
.get("password").get(0).toString(), data.get("status").get(0)
.toString(), true);
assertEquals(driver.getTitle(),
Init.properties.getValue("xxxx"));
home.loginOut.click();
 
}
 

猜你喜欢

转载自1105980952.iteye.com/blog/1918181