无法从另一个Java类调用函数

时间:2018-07-27 13:41:26

标签: java selenium-webdriver apache-poi

我正在开发一些硒代码,并试图使用apache POI从Excel工作表中获取输入。到目前为止,我已经设法获得了输入,但是我无法将其从一个班级转移到另一个班级。请参见下面的代码:

要调用的功能:

package Excel;

import java.io.File;
import java.io.FileInputStream;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

public class Read {

    XSSFSheet Names;
    public void read() throws Exception{

        File src = new File("C:\\Users\\dindo\\Documents\\tests\\d2c-lv-int-01_DATA.xlsx");

        FileInputStream fis = new FileInputStream(src);

        XSSFWorkbook wb = new XSSFWorkbook(fis);

        Names = wb.getSheetAt(0);

    }

    public void getcell(int row, int col){
        String stringresult = Names.getRow(row).getCell(col).getStringCellValue();
        String intresult = Names.getRow(row).getCell(col).getStringCellValue();
    }

尝试调用函数:

package Pages;

import Excel.Read;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.How;

public class DetailsPage {
    Read cel = new Read();
    cel.getcell(2,4)

    @FindBy(how = How.XPATH, using = "/*xpath*/")
    public WebElement coveramountelement;

    public String coveramount = cel.intresult;

    public void EnterDetails() {
        coveramountelement.sendKeys(coveramount);
    }
}

ERRORS

第10行的所有错误均针对cel.getcell(2,4);

2 个答案:

答案 0 :(得分:3)

我建议像这样重组您的代码

函数应被重用-因此将特定变量硬编码到其中不是一个好主意。而且您不存储任何状态,因此只需返回所需的对象

public class Read {

    public static XSSFSheet getSheet(String file, int sheetIndex) throws Exception{
        File src = new File(f);
        FileInputStream fis = new FileInputStream(src);
        XSSFWorkbook wb = new XSSFWorkbook(fis);
        return wb.getSheetAt(sheetIndex);
    }

    public static Cell getCell(XSSFSheet s, int row, int col) {
       return s.getRow(row).getCell(col);
    }

现在,使用这些通用函数执行您的特定任务

public class DetailsPage {
    private XSSFSheet names;

    public DetailsPage() {
        try {
           names = Read.getSheet("C:\\Users\\dindo\\Documents\\tests\\d2c-lv-int-01_DATA.xlsx", 0);
        } catch (Exception e) {
            e.printStackTrace();
        } 
    }

    @FindBy(how = How.XPATH, using = "/*xpath*/")
    public WebElement coverAmountElement;

    public void enterDetails() {
        if (names != null) {
            XSSFCell c = Read.getCell(names, 2,4);
            String coveramount = c.getStringCellValue();
            coverAmountElement.sendKeys(coveramount);
        }
    }
}

答案 1 :(得分:0)

除非要初始化变量,否则不能在其他方法/函数之外调用方法/函数。此外,getCell方法返回一个void,这意味着您不能使用它来分配任何内容。此外,您还缺少分号。所以改变这个

Read cel = new Read();
cel.getcell(2,4)

对于这样的事情:

package Pages;

import Excel.Read;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.How;

public class DetailsPage {

    @FindBy(how = How.XPATH, using = "/*xpath*/")
    public WebElement coveramountelement;


    public void EnterDetails() {
        Read cel = new Read();            
        String coveramount = cel.getcell(2,4);
        coveramountelement.sendKeys(coveramount);
    }
}

对于getCell来说,是这样的:

public String getcell(int row, int col){
    String stringresult = Names.getRow(row).getCell(col).getStringCellValue();
    return stringresult;
}