想要从excel文件中获取数据

时间:2014-07-01 04:38:21

标签: selenium-webdriver

数据驱动框架,其中每个案例的值都在变化

 public static void main(String[] args) throws BiffException, IOException {       
            Sheet s;
            WebDriver driver = new FirefoxDriver();
            FileInputStream fi = new FileInputStream("D:\\Nikhil\\FGX\\DataDriven.xlsx");
            Workbook W = Workbook.getWorkbook(fi);

            s = W.getSheet(0);

            for(int row = 0;row <= s.getRows();row++)
            {
                String Username = s.getCell(0,row).getContents();
                System.out.println("Username" +Username);
                driver.get("http://********");
                driver.findElement(By.xpath("//*[@id='LoginName']")).sendKeys(Username);

                String password= s.getCell(1, row).getContents();
                System.out.println("Password "+password);

                driver.findElement(By.xpath("//*[@id='Password']")).sendKeys(password);

                driver.findElement(By.xpath("html/body/form/div/div/div/div/fieldset/button")).click();
        }

1 个答案:

答案 0 :(得分:0)

有两个理由让BiffException看Biff exception in Java

确保指向正确的文件,工作表,单元格等。您需要获取并迭代Excel数据。我可能没有直接回答您的回答,但以下代码可能会对您有所帮助,

List getData() { **获取数据

      String path = "filepath";
      List dataList = new ArrayList();
      FileInputStream fis = null;
      try { 
          fis = new FileInputStream(new File(path));
          XSSFWorkbook workbook = new XSSFWorkbook(fis);
          XSSFSheet sheet = workbook.getSheet("TestData");
          java.util.Iterator rows = sheet.rowIterator();

          while (rows.hasNext()) {
              XSSFRow row = ((XSSFRow) rows.next());
              // int r=row.getRowNum();
              java.util.Iterator cells = row.cellIterator();
              int i = 0;
              String[] testData= new String[3];
              while (cells.hasNext()) {

                  XSSFCell cell = (XSSFCell) cells.next();
                  String value = cell.getStringCellValue();
                  if (!value.equals(null)) {
                       testData [i] = value;
                       i++;
                  }
              }
              dataList.add(testData);
          }
      }
      catch (Exception e) {
          e.printStackTrace();
      }
      return dataList;
}



public Object[][] data() { **Store the data in desired format

    @SuppressWarnings("rawtypes")
    List dataList= getData();
    Object a[][]=new Object[dataList.size()][2];
    for(int i=1;i<dataList.size();i++){
        String[] test=(String[]) dataList.get(i);
        String username = test[0];
        String password=test[1];
        a[i][0]=username;
        a[i][1]=password;
    }
    return a;