gpt4 book ai didi

java - 无法从另一个java类调用函数

转载 作者:行者123 更新时间:2023-12-01 19:50:12 25 4
gpt4 key购买 nike

我正在开发一些 selenium 代码,并尝试使用 apache POI 从 Excel 工作表获取输入。到目前为止,我已经设法获得输入,但无法将其从一个类(class)转移到另一个类(class)。请看下面的代码:

要调用的函数:

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

最佳答案

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

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

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

关于java - 无法从另一个java类调用函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51559277/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com