[英]Unable to call function from another java class
我正在开发一些硒代码,并尝试使用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);
}
}
第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);
}
}
}
除非要初始化变量,否则您不能在其他方法/函数之外调用方法/函数。 此外, 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;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.