简体   繁体   中英

Getting NPE NullPointerException in PageFactory (Selenium+Java)

I'm trying to implement Page Factory pattern for my testing framework, but I'm getting NPE while trying to reach or interact with elements. BTW, I'm using Spring Dependency Injection for sharing WebDriver instance among tests (instead of constructor), so I implemented PageFactory initElements() inside non-static block to be able to reach it. BTW, without PageFactory (without @FindBy and with commented By items) it works perfect. Here is my code of 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);
  }

}

Here is my Test method:

@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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM