0

我在带有 IUnit 的 Eclipse 中使用 Selenium Web 驱动程序。我有从 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,它必须path_name与 ReadExcel 类相等。实际上,我想将 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();
    }

同样的效果。它不会点击链接

4

2 回答 2

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()尽管我们大多数人更喜欢第一个)。这是一个可以帮助您的链接。

于 2012-07-20T16:15:33.257 回答
0

你能把你的变量转换成那个类的字段吗?然后在那里添加一个方法来将该字段的值返回给任何感兴趣的人。

于 2012-07-20T16:19:37.407 回答