2016-03-04 39 views
0

我有兩個腳本;一個使用Apache POI從Excel工作表中讀取數據,另一個使用硒從Apache腳本中獲取接收到的數據並將數據用於輸入字段。如何設置變量爲ApachePOI迭代數據值

這裏是我的Apache的Excel閱讀器腳本:

public class readExcelFinal{ 
static ArrayList<Double> priceList; 
static ArrayList<String> titleList; 
static ArrayList<String> descriptionList; 
static ArrayList<String> imageLocationList; 

public static void processExcelFile(String fileName) throws IOException{ 
    priceList = new ArrayList<Double>(); 
    titleList = new ArrayList<String>();   
    descriptionList = new ArrayList<String>(); 
    imageLocationList = new ArrayList<String>(); 

    try{ 
     FileInputStream myInput = new FileInputStream(fileName); 
     POIFSFileSystem myFileSystem = new POIFSFileSystem(myInput); 
     HSSFWorkbook myWorkBook = new HSSFWorkbook(myFileSystem); 
     HSSFSheet mySheet = myWorkBook.getSheetAt(2); 
     Iterator rowIter = mySheet.rowIterator(); 

     // For each row, 
     while(rowIter.hasNext()){ 
      HSSFRow row = (HSSFRow) rowIter.next(); 

      // If there's a cell at index 0, it's a price 
      if(row.getCell(0) != null) 
      priceList.add(row.getCell(0).getNumericCellValue()); 

      // If there's a cell at index 1, it's a title 
      if(row.getCell(1) != null) 
      titleList.add(row.getCell(1).getStringCellValue()); 

      // If there's a cell at index 2, it's a description 
      if(row.getCell(2) != null) 
      descriptionList.add(row.getCell(2).getStringCellValue()); 

      // If there's a cell at index 3, it's an image location 
      if(row.getCell(3) != null) 
      imageLocationList.add(row.getCell(3).getStringCellValue()); 
     } 
    }catch(Exception e){ 
     e.printStackTrace(); 
    } 
} 

public static void createTests(){ 
    // Create an RNG to re-use 
    Random randomGenerator = new Random(); 


    // Iterate through the price list and create tests 
    for (Double price : priceList){   

     // For each of title, description, and image location, get a random index into the list and pull the value 
     int titleIndex = randomGenerator.nextInt(titleList.size()); 
     String title = titleList.get(titleIndex);   

     int descriptionIndex = randomGenerator.nextInt(descriptionList.size()); 
     String description = descriptionList.get(descriptionIndex); 

     int imageLocationListIndex = randomGenerator.nextInt(imageLocationList.size()); 
     String imageLocation = imageLocationList.get(imageLocationListIndex); 


     System.out.println("Creating test for Price " + price + "\n\tTitle:\t" + title + "\n\tDesc:\t" + description + "\n\tImg:\t" + imageLocation); 

     } 
    } 

public static String price; 
public static String title; 
public static String description; 
public static String imageLocation; 

public void setName(String price) { 
    this.price = price; 
} 

public static String getPrice() { 
    return price; 
} 

public void setDescription(String description) { 
    this.description = description; 
} 

public static String getDescription() { 
    return description; 
} 

public void setTitle(String title) { 
    this.title = title; 
} 

public static String getTitle() { 
    return title; 
} 

public void setImageLocation(String imageLocation) { 
    this.imageLocation = imageLocation; 
} 

public static String getImageLocation() { 
    return imageLocation; 
} 

} 

,這裏是我的(部分)我的硒腳本:

public class webDriver { 


public static void main(String[] args) throws Exception { 

    //THIS IS WHERE IM PULLING THE METHOD INTO MY SELENIUM SCRIPT 
    readExcelFinal.main(args); 



      //EXCELL DATA 
      String price = readExcelFinal.getPrice(); 
      String title = readExcelFinal.getTitle(); 
      String description = readExcelFinal.getDescription(); 
      String imageLocation = readExcelFinal.getTitle(); 

      System.out.println("Creating test for Price " + price + "\n\tTitle:\t" + title + "\n\tDesc:\t" + description + "\n\tImg:\t" + imageLocation); 

    // The Firefox driver supports javascript 
    WebDriver driver = new FirefoxDriver(); 

正如你可以在我的硒腳本看,我使用:

System.out.println("Creating test for Price " + price + "\n\tTitle:\t" + title + "\n\tDesc:\t" + description + "\n\tImg:\t" + imageLocation); 

快速查看我的控制檯中的數據,所以我不必等待整個腳本運行,看看whe無論變量是否得到值。我回到了控制檯:

Creating test for Price null 
Title: null 
Desc: null 
Img: null 

我沒有從我的腳本中獲取任何測試用例數據。

+1

閱讀[mcve]並消除所有不相關的變量。 – SiKing

+0

謝謝,我看看 – scgc

回答

1

背後null值的原因是,你要打印變量pricetitledescription通過調用getXXX方法,但是,他們沒有設置/分配的值中的任何位置代碼(例如我看不到setXXX()是稱爲任何地方),因此,default值被分配和打印。

順便說一句,我建議你通讀Java Programming Basics並區別static and non static變量/成員。

+0

謝謝你的回覆..我有這個部分之前,我被告知這是不必要的..它現在備份 – scgc

+0

現在,它的備份,你認爲這是怎麼回事?我在那裏有我的setter,但值仍然沒有被應用到變量 – scgc

+0

我看不到任何設置器從任何地方被調用。 –