2012-07-20 45 views
0

我在Eclipse中使用Selenium Web Driver和IUnit。我有從Excel文件讀取數據的代碼。每列都以數組的形式呈現。這是代碼:如何從另一個數組中調用變量?

class ReadExcel { 
ArrayList path_name = new ArrayList(); 
     ArrayList field_key = new ArrayList(); 
     ArrayList field_name = new ArrayList(); 
     ArrayList window_new = new ArrayList(); 
     ArrayList link = new ArrayList(); 
     lov_name = new ArrayList(); 
    public void mai() { 
     int i = 0; 


     String path_namel = "", field_keyl = "", field_namel = "", window_newl = "", linkl = "", lov_namel = ""; 
     String filename = "E:/data.xls"; 
     if (filename != null && !filename.equals("")) { 
      FileInputStream fs = new FileInputStream(filename); 
      HSSFWorkbook wb = new HSSFWorkbook(fs); 
      for (int k = 0; k < wb.getNumberOfSheets(); k++) { 
       int j = i + 1; 
       HSSFSheet sheet = wb.getSheetAt(k); 
       int rows = sheet.getPhysicalNumberOfRows(); 
       for (int r = 1; r < rows; r++) { 
        HSSFRow row = sheet.getRow(r); 
        int cells = row.getPhysicalNumberOfCells(); 
        HSSFCell cell1 = row.getCell(0); 
        path_namel = cell1.getStringCellValue(); 
        HSSFCell cell2 = row.getCell(1); 
        field_keyl = cell2.getStringCellValue(); 
        HSSFCell cell3 = row.getCell(2); 
        field_namel = cell3.getStringCellValue(); 
        HSSFCell cell4 = row.getCell(3); 
        window_newl = cell4.getStringCellValue(); 
        HSSFCell cell5 = row.getCell(4); 
        linkl = cell5.getStringCellValue(); 
        HSSFCell cell6 = row.getCell(5); 
        lov_namel = cell6.getStringCellValue(); 

        path_name.add(path_namel); 
        field_key.add(field_keyl); 
        field_name.add(linkl); 
        window_new.add(window_newl); 
        link.add(linkl); 
        lov_name.add(lov_namel); 
       } 
       i++; 
      } 
     } 
    } 
} 

以我硒測試我有這樣循環:

for (int i=0; i<path_name.length; i++){ 
     driver.findElement(By.xpath(path_name[i])).click(); 
} 

這裏我使用可變path_name其是陣列,並且必須是從類READEXCEL等於path_name。其實我想用excel中的這個值作爲數組。我應該如何從ReadExcel調用變量?

編輯 我嘗試使用getter和setter方法。

int q; 
String g; 
public String getG() { 
    return g;} 
public void setG(String g) { 
    this.g = g;} 
public int getQ() { 
    return q;} 
public void setQ(int q) { 
    this.q = q;} 

q=path_name.size(); 
g=path_name.get(i).toString(); 

我我的測試我打電話變量這樣的方式

ReadExcel h = new ReadExcel(); 
String k= h.getG(); 
ReadExcel p = new ReadExcel(); 
int n= p.getQ(); 

for (int j=0; j<n; j++){ 
driver.findElement(By.xpath(k)).click();} 

有編輯器沒有錯誤,但週期不工作。它應該點擊鏈接(k),但不起作用。 我也試試這個(在第一個答案的建議)

ReadExcel readExcel = new ReadExcel(); 
    ArrayList<String> path_name = readExcel.getPath_name(); 

    for(String pathName: path_name){ 
     driver.findElement(By.xpath(pathName)).click(); 
    } 

同樣的效果。它不點擊鏈接

回答

0

一種方法是在您的ReadExcel類中爲要訪問的變量實施setter()getter()方法。他們顯然是公共的方法。

編輯:

從你已經什麼試圖更新和我的理解猜測,你正在做的很多事情是錯誤的。假設你是從其他類調用你的最後一段代碼,這裏就是你確實應該這樣做

另外,我假設你已經修改了ReadExcel類是這個樣子

public class ReadExcel { 

    ArrayList<String> pathName  = new ArrayList<String>(); 
    ArrayList<String> fieldKey  = new ArrayList<String>(); 
    ArrayList<String> fieldName  = new ArrayList<String>(); 
    ArrayList<String> windowNew  = new ArrayList<String>(); 
    ArrayList<String> link   = new ArrayList<String>(); 

    public ReadExcel() { 
     pathName = new ArrayList<String>(); 
     fieldKey = new ArrayList<String>(); 
     fieldName = new ArrayList<String>(); 
     windowNew = new ArrayList<String>(); 
     link  = new ArrayList<String>(); 
    } 

    /** 
    * Not so sure of this method name. But make sure that this method is called before 
    * you try to call getXX() methods 
    */ 
    public void mai() { 
     String filename = "E:/data.xls"; 

     if(fileName == null || "".equals(fileName)) 
      return; 

     HSSFWorkbook workBook = null; 
     FileInputStream fileInputStream = null; 
     HSSFSheet sheet; 
     HSSFRow row; 

     int rows; 

     try{ 
      fileInputStream = new FileInputStream(new File(fileName)); 
      workBook = new HSSFWorkbook(fileInputStream); 

      for(int sheetIndex = 0; sheetIndex < workBook.getNumberOfSheets(); sheetIndex++){ 
       sheet = workBook.getSheetAt(sheetIndex); 

       rows = sheet.getPhysicalNumberOfRows(); 

       for(int rowIndex = 0; rowIndex < rows; rowIndex++){ 
        /** 
        * Update with your own logic for retrieval 
        */ 
        row = sheet.getRow(rowIndex); 
        if(row.getPhysicalNumberOfCells() < 6) 
         continue; 

        pathName.add(row.getCell(0).getStringCellValue()); 
        fieldKey.add(row.getCell(0).getStringCellValue()); 
        fieldName.add(row.getCell(0).getStringCellValue()); 
        windowNew.add(row.getCell(0).getStringCellValue()); 
        link.add(row.getCell(0).getStringCellValue()); 
       } 
      } 

     } catch (FileNotFoundException fileNotFoundException) { 
      fileNotFoundException.printStackTrace(); 
     } catch (IOException ioException) { 
      ioException.printStackTrace(); 
     }finally{ 
      if(fileInputStream != null){ 
       try { 
        fileInputStream.close(); 
       } catch (IOException e) { 
        e.printStackTrace(); 
       } 
      } 
      workBook = null; 
     } 
     } 
    } 

    /** 
    * The getter/setter methods for the variables 
    */ 
    public ArrayList<String> getPathName() { 
     return pathName; 
    } 

    public void setPathName(ArrayList<String> pathName) { 
     this.pathName = pathName; 
    } 

    public ArrayList<String> getFieldKey() { 
     return fieldKey; 
    } 

    public void setFieldKey(ArrayList<String> fieldKey) { 
     this.fieldKey = fieldKey; 
    } 

    public ArrayList<String> getFieldName() { 
     return fieldName; 
    } 

    public void setFieldName(ArrayList<String> fieldName) { 
     this.fieldName = fieldName; 
    } 

    public ArrayList<String> getWindowNew() { 
     return windowNew; 
    } 

    public void setWindowNew(ArrayList<String> windowNew) { 
     this.windowNew = windowNew; 
    } 

    public ArrayList<String> getLink() { 
     return link; 
    } 

    public void setLink(ArrayList<String> link) { 
     this.link = link; 
    } 
} 

我希望你在什麼地方調用你mai()方法(這個名字聽起來很奇怪,雖然)從Excel中檢索數據,並將其存儲在ArrayList你想調用下面的一塊前的代碼:

ReadExcel readExcel = new ReadExcel(); 
ArrayList<String> path_name = readExcel.getPath_name(); 

for(String pathName: path_name){ 
    driver.findElement(By.xpath(pathName).click(); 
} 

一些指針代碼:

  • 使用泛型。代替定義ArrayList pathNameList請考慮使用ArrayList<String> pathNameList

  • 如果您在編碼時使用Java命名約定,它看起來不錯。其中之一是使用混合大小寫字母來編寫方法名稱,以小寫字母開頭並以大寫字母開頭。因此,不要考慮getPath_name(),而應考慮使用類似getPathName()或甚至getPath_Name(),儘管我們大多數人更喜歡第一個)。 Here's a link that can help you with that.

+0

我已更新問題 – khris 2012-07-23 07:50:19

+0

謝謝,但這部分代碼path_name.getPath_name(); 給出錯誤 - path_name無法解析 – khris 2012-07-24 07:58:32

+0

更新的代碼:) – Sujay 2012-07-24 13:43:47

0

您可以將您的變量轉換爲該類的字段嗎?然後在那裏添加一個方法來將該字段的值返回給任何感興趣的人。

+0

我認爲第一個答案就像你指出的那樣。我更新了問題 – khris 2012-07-23 07:51:44

+0

我明白了,現在您的問題並未與原始問題相關。看看有什麼問題需要excel文件和訪問裏面的鏈接。爲了解決這個問題,我會在你的測試中打開特定的頁面,並檢查你的Excel中的xPatch實際上是否匹配正確的鏈接。您可以使用chrome中的開發工具或其他瀏覽器中的替代方法來完成此任務。 – 2012-07-24 21:26:44

+0

鏈接是正確的,我嘗試直接在我的測試中使用它們,它的工作原理 – khris 2012-07-25 07:36:56

相關問題