简体   繁体   English

如何从另一个类(数组)中调用变量?

[英]How call variable from another class which is array?

I use Selenium Web Driver in Eclipse with IUnit. 我在带有IUnit的Eclipse中使用Selenium Web Driver。 I have code which read data from excel file. 我有从excel文件读取数据的代码。 Every column is presented as array. 每列均以数组形式显示。 This is code: 这是代码:

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++;
            }
        }
    }
}

In my selenium test I have such cycle: 在我的硒测试中,我有这样的循环:

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

Here I use variable path_name which is array and must be equal path_name from class ReadExcel. 在这里,我使用的变量path_name是数组,并且必须与类ReadExcel中的path_name相等。 Actually I want use this values from excel as array. 实际上,我想使用excel中的此值作为数组。 How should I call variable from ReadExcel? 我应该如何从ReadExcel调用变量?

Edit I try use getter and setter methods. 编辑我尝试使用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();

I my test I call variables in such way 我在测试中以这种方式调用变量

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();}

Have no errors in editor, but cycle do not work. 编辑器中没有错误,但是循环不起作用。 It should click on links (k), but have no effect. 它应该单击链接(k),但无效。 Also I try this (was suggested in first answer) 我也尝试一下(在第一个答案中建议)

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

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

The same effect. 效果一样。 It does not click on links 它不会点击链接

One way would be to implement setter() and getter() methods in your ReadExcel class for the variables that you want to access. 一种方法是在ReadExcel类中为要访问的变量实现setter()getter()方法。 And they would obviously be public methods. 它们显然是公共方法。

Edit: 编辑:

Guessing from what you've tried to update and my understanding, you're doing a lot of things wrong. 从您尝试更新的内容和我的理解中猜测,您做错了很多事情。 Assuming that you're calling your last piece of code from another class, here's what you really should have done : 假设您正在从另一个类中调用最后一段代码, 那么您实际上应该这样做

Also, I'm assuming that you've modified your ReadExcel class to look something like this 另外,我假设您已经修改了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;
    }
}

And I hope that you're somewhere calling your mai() method ( this name sounds really weird though ) to retrieve the data from the excel and store them in the ArrayList before you're trying to call the following piece of code: 并且我希望您在某个地方调用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();
}

Some pointers to your code: 一些指向您的代码的指针:

  • Use generics. 使用泛型。 Instead of defiing ArrayList pathNameList consider, using ArrayList<String> pathNameList 代替defiing的ArrayList pathNameList考虑,使用ArrayList<String> pathNameList

  • It looks good if you use Java Naming Convention when you code. 如果在编写代码时使用Java命名约定,则看起来不错。 One of them is to compose method names using mixed case letters, beginning with a lower case letter and starting each subsequent word with an upper case letter. 其中一种方法是使用大小写混合的字母组成方法名称,以小写字母开头,随后的每个单词以大写字母开头。 So instead of having a getPath_name() , consider having something like getPathName() or even getPath_Name() ( although most of us would prefer the 1st one ). 因此,与其拥有getPath_name()getPath_name()考虑拥有诸如getPathName()甚至getPath_Name()尽管我们大多数人更喜欢第一个 )。 Here's a link that can help you with that. 这是一个可以帮助您的链接。

Can you convert your variable into field of that class? 您可以将变量转换为该类的字段吗? And then add a method there to return value of that field to anyone interested. 然后在那里添加一个方法,将该字段的值返回给感兴趣的任何人。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM