关于TestNG数据读取方法及数据驱动测试浅谈——方法篇

之前在数据篇介绍了Dataprovider,之后自己看了一下,觉得还是有些实例会可读性强一些。关于数据读取,我以Excel为例,介绍一个自己项目中的方法,以做补充:

// CMDNumber和method是读取参数,前者用于定位Excel的sheet,后者用于定位Cell坐标
public String[][] getExcel_Step2(String CMDNumber, String method)
throws BiffException, IOException
{
//读取指定路径的Excel数据文档,得到参数CMDNumber并依此对应到相应的sheet在Excel文件中
Workbook wb1 = Workbook.getWorkbook(new File("E:/JAVA testing/_Testfiles/TestCMD.xls"));
Sheet sheet1=wb1.getSheet(CMDNumber);
//根据method找到起始坐标,并由其实横,纵坐标位置确定终止坐标
Cell Step2start=sheet1.findCell(method);
int startrow = Step2start.getRow();
int startcol = Step2start.getColumn();
Cell Step2end =sheet1.findCell("#",startcol,startrow+4, 100, 64000, false);
int endrow = Step2end.getRow();
int endcol = Step2end.getColumn();
/*
到这里,简单总结一下,我们现在得到了4个坐标:startrow,startcol,endrow,endcol
另注意一点,getCell()也可以定位坐标,但是getCell(j,i)两个参数顺序是纵坐标在前,这点很容易被忽略,具体看下面的for循环的方法应用
*/
//定义tabarray为返回值数组,我们根据4个坐标来得到Cell的值,方法是getContents()
String[][] tabarray= new String[endrow - startrow][endcol-startcol+1];
int cj = 0;
for(int j = startcol;j<=endcol;j++,cj++)
{

int ci = 0;
for(int i = startrow+1 ;i<endrow;i++,ci++)
{
tabarray[ci][cj] = sheet1.getCell(j, i).getContents();
//System.out.println(tabarray[ci][cj]);
}
}

return tabarray;
}
随后,我们就可以写处理数据的方法了:
public void Login(WebDriver driver, String cmd, String method)
{
//首先读取页面值,因为我们的web页面很简单,就几个元素的简单值,所以用Elements装起来
WebElement Login=driver.findElement(By.id("Login"));
WebElement Password=driver.findElement(By.id("Password"));
WebElement Key=driver.findElement(By.id("Key"));
WebElement[] Elements={Login,Password,Key};

//开始读取数据,并把WebElement需要send的key值从数据方法中传过来
try {

String[][] tab1=getExcel(cmd,method);

for(int i=0;i<tab1.length-1;i++)
{
for(int j=0;j<tab1[i].length;j++)
{
System.out.println(tab1[i][j]);
Elements[i].sendKeys(tab1[i][j]);

}
}


} catch (BiffException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}
当然数据读取形式可以很多样化,数据源类型也不一而同。所以方法也是根据实际情况而变化很多。其实此方法除了测试之外,也可以对测试数据的准备上有用武之地,需求是随时而变的,tester的方法也要活学活用才好,自勉,共勉。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值