【发布时间】:2022-01-24 19:25:00
【问题描述】:
我需要在第二种方法中访问变量“value”。我尝试将其设为实例变量,但它只是返回为 null。
public class RateFellowUnsatisfactoryPage extends PageObject {
@FindBy(xpath = "actual xpath")
private WebElementFacade checkbox;
@FindBy(xpath = "actual xpath")
private WebElementFacade fellowRated;
private String value;
public void selects_first_checkbox() {
value = checkbox.getValue();
System.out.println(value);
checkbox.click();
}
public void verifyFellowDisplayed() {
String fellow = fellowRated.getTextValue();
System.out.println(fellow);
assertThat(fellow.equalsIgnoreCase(value));
System.out.println(value);
}
}
这里是调用这些方法的代码
public class RateFellowUnsatisfactorySteps extends ScenarioSteps {
private RateFellowUnsatisfactoryPage rateFellowUnsatisfactoryPage;
@Step
public void selectsFirstCheckbox(){
rateFellowUnsatisfactoryPage.selects_first_checkbox();
}
@Step
public void verifyFellowDisplayed(){
rateFellowUnsatisfactoryPage.verifyFellowDisplayed();
}
}
【问题讨论】:
-
你在这里所做的看起来不错。在第一个函数中命中此行时会打印什么?: System.out.println(value); (当创建一个像这样的私有实例变量时,您可能需要创建 getvalue()/setvalue() 函数...这样您就可以从类外部访问它...)
-
@pcalkins 在第一种方法中,它会打印同伴的 ID 号。我需要将同一个 ID 号与在第二种方法中检索到的 ID 号进行比较。此时,String 值的值变为 null。
-
也许你有这个类的两个实例......你需要包含调用方法的代码。
-
@pcalkins 刚刚做到了
-
通过调用构造函数初始化...所以 rateFellowUnsatisfactoryPage = new RateFellowUnsatisfactoryPage();现在,您只有对一个类的引用,但实际上并没有该类的任何实例。 (?)
标签: java selenium automation