java - 在 PageFactory (Selenium+Java) 中获取 NPE NullPointerException
问题描述
我正在尝试为我的测试框架实现页面工厂模式,但是在尝试访问元素或与元素交互时我得到了 NPE。顺便说一句,我使用 Spring Dependency Injection 在测试之间共享 WebDriver 实例(而不是构造函数),所以我initElements()
在非静态块中实现了 PageFactory 以便能够访问它。顺便说一句,没有 PageFactory(没有 @FindBy 和评论的项目)它工作得很好。这是我的 PO.class 代码:
@Component
public class HomePage extends BasePage {
@Autowired
private DatabasesPage databasesPage;
@Autowired
private UserAccountsPage userAccountsPage;
public static Logger log = LogManager.getLogger(HomePage.class.getName());
private String themeLabelColorHex = "#235a81";
private Select themeList;
/* private By databasesHeaderButton = By.cssSelector("#topmenu > li:nth-child(1)"),
userAccountsHeaderButton = By.cssSelector("#topmenu > li:nth-child(4)"),
languagesDropdown = By.id("sel-lang"),
themeDropdown = By.name("set_theme"),
themeLabel = By.xpath("//*[@id='li_select_theme']//a");*/
@FindBy(how = How.CSS, css = "#topmenu > li:nth-child(1)") private WebElement databasesHeaderButton;
@FindBy(how = How.CSS, css = "#topmenu > li:nth-child(4)") private WebElement userAccountsHeaderButton;
@FindBy(how = How.ID, id = "sel-lang") private WebElement languagesDropdown;
@FindBy(how = How.NAME, name = "set_theme") private WebElement themeDropdown;
@FindBy(how = How.XPATH, xpath = "//*[@id='li_select_theme']//a") private WebElement themeLabel;
{
PageFactory.initElements(driver,this);
System.out.println("**************** THIS IS A NON-STATIC FIELD REACHED BY DEP INJ ****************");
}
public HomePage openUp() {
String propertiesPath = get(System.getProperty("user.dir"),
"src", "main", "java", "base", "configuration", "config.properties").normalize().toString();
try {
Properties properties = new Properties();
properties.load(new FileInputStream(propertiesPath));
String url = properties.getProperty("url");
driver.get(url);
}
catch ( IOException e) {
log.error("Properties file is not found");
log.error("\n " + ExceptionUtils.getStackTrace(e));
}
return this;
}
public DatabasesPage goToDatabasesPage() {
databasesHeaderButton.click();
return databasesPage;
}
public UserAccountsPage goToUserAccountsPage() {
userAccountsHeaderButton.click();
return userAccountsPage;
}
public HomePage changeLanguage(String langValue) {
try {
Select langList = new Select(languagesDropdown);
String currentLang = langList.getFirstSelectedOption().getAttribute("value");
if (langValue.equalsIgnoreCase(currentLang)) log.warn("Desired language is already selected!");
else langList.selectByValue(langValue);
}
catch (Exception e) {
log.error("\n" + ExceptionUtils.getStackTrace(e));
}
return this;
}
public HomePage changeTheme(String themeValue) {
try {
themeList = new Select(themeDropdown);
String currentTheme = themeList.getFirstSelectedOption().getAttribute("value");
if (themeValue.equalsIgnoreCase(currentTheme))
log.warn("Desired theme is already selected!");
else {
themeList.selectByValue(themeValue);
driver.navigate().refresh();
themeLabelColorHex = "#0000ff";
}
}
catch (Exception e) {
log.error("\n" + ExceptionUtils.getStackTrace(e));
}
return this;
}
public HomePage setDefaultTheme() {
themeList = new Select(themeDropdown);
themeList.selectByValue("pmahomme");
themeLabelColorHex = "#235a81";
return this;
}
public boolean isLanguageChanged() {
return true; // TO DO
}
public boolean isThemeChanged() {
String RGBColor = themeLabel.getCssValue("color"); // I'm getting NPE here somehow
String HexColor = Color.fromString(RGBColor).asHex();
return HexColor.equalsIgnoreCase(themeLabelColorHex);
}
}
这是我的测试方法:
@Test
@Description("Change website theme")
public void changeWebsiteTheme() {
homePage.openUp().changeTheme("original");
assertThat(true).isEqualTo(homePage.isThemeChanged()); // and it fails here but it didn't perform previous step - changeTheme();
homePage.setDefaultTheme();
assertThat(true).isEqualTo(homePage.isThemeChanged());
}
enter code here
解决方案
to initiate the PageFactory which is written in constructor, it has to be initiated by create the object of the class. this helped me to achieve this.
推荐阅读
- android - 为什么在 Android Studio 3.3 的预览选项卡中显示模糊 xml 视图
- excel - 当有一个 IF 语句时合并多个 then 语句
- clojure - Clojure / Leiningen - 将 jar 资源打包成 clojars
- ios - 如何通过 Swift 使注解出现在 Apple 地图上?
- visual-studio-code - 当我离线时,vscode 无法在我的笔记本电脑上运行
- firebase - 用于 React Native 应用程序的 Firebase Crashlytics 中更好的异常调用堆栈
- php - 未捕获的 mysqli_sql_exception:“where 子句”中的未知列
- drone - 什么是drone.io 0.8.5 插件/gcr 分泌物的可接受值?
- laravel - 未找到 Stripe 类“Stripe\Stripe”的致命错误
- jit - 如何禁用 CoreCLR 分层编译?