【问题标题】:How to set variables to ApachePOI iterated data values如何将变量设置为 Apache POI 迭代数据值
【发布时间】:2016-03-04 18:53:55
【问题描述】:

我有两个脚本;一个使用 Apache POI 从 Excel 表中读取数据,另一个使用 selenium 从 apache 脚本获取接收到的数据并将数据用于输入字段。

这是我的 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();

正如您在我的 selenium 脚本中看到的,我正在使用:

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

在我的控制台中快速查看数据,这样我就不必等待整个脚本运行来查看变量是否正在获取值。我在控制台中得到了这个:

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

我没有从我的脚本中获得任何测试用例数据。

【问题讨论】:

标签: java excel selenium apache-poi


【解决方案1】:

null 值背后的原因是,您试图通过调用 getXXX 方法打印变量 pricetitledescription,但是,它们不是在代码中的任何位置设置/分配的值(例如,我看不到 setXXX() 在任何地方被调用),因此,default 值被分配和打印。

顺便说一句,我建议您通读 Java Programming Basicsstatic and non static 变量/成员之间的区别。

【讨论】:

  • 感谢您的回复。在我被告知没有必要之前,我已经把那个部分放在了上面。现在它备份了
  • 现在它已备份,您认为这里发生了什么?我有我的设置器,但这些值仍未应用于变量
  • 我看不到任何设置者从任何地方被调用。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-01-11
  • 2021-09-16
  • 2021-04-03
相关资源
最近更新 更多