webdriver 的CSV数据驱动

时间:2014-07-22 23:16:34   收藏:0   阅读:467

1.获取CSV数据内容

public static Object[][] getFromCSV(String filename) {
  
    if (!(new File(filename)).exists()){
     return null;
    }
    Object[][] content;
    CSVReader reader;
    try {
     reader = new CSVReader(new FileReader(filename));
     Vector<String[]> vec = new Vector<String[]>();
     String[] nextLine;
     reader.readNext(); //skip title
     while ((nextLine = reader.readNext()) != null) {   
      vec.add(nextLine);   
     }
     content = new Object[vec.size()][];
     for (int i = 0; i < vec.size(); i++){
      content[i] = vec.get(i);
     }
     return content;
    } catch (Exception e) {
     System.out.println("Read CSV File:" + filename + "failed.");
     e.printStackTrace();  
    }
    return null;
  }

 

2.定义数据驱动

String file = "src/test/java/data/page/TaxIssueAnswerTest.csv";

  @DataProvider(name="CsvDataProvider")
  public Object[][] inputData(){
   return getFromCSV(file);
  }

 

3.使用数据驱动

@Test(dataProvider = "CsvDataProvider",description ="正常业务场景测试")
 public void answerIssue(String caseId, String description,String userName, String pwd){

//执行用例

}

 

4.图例文件路径

mamicode.com,码迷

webdriver 的CSV数据驱动,码迷,mamicode.com

评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!