'Selenium WebDriver - Not able to access or click elements using PageObjectModel

I have defined Web Elements using Page Object model in Selenium. From test method, when ever I try to access or perform any action on those web elements my test completely skips it and completes, with no error.

public class HomePage extends Base{

@FindBy(xpath="//button[@id='sparkButton']")
    public WebElement menuDropDown;

public HomePage(){
        PageFactory.initElements(driver, this);
    }

public void clickHomepagemenuDropDown() {
        menuDropDown.click();
        System.out.println("Print HELLO");
   }
}


public class Test1 extends Base{

  HomePage homepage = new HomePage() ;
  @Test(priority=1)
  public void homePage() throws Exception {
    try {
      //do something
        homepage.clickHomepagemenuDropDown();
      //print something
       }catch (Exception e) {
               System.out.println (e);
                  return;
       }

}

If I replace homepage.clickHomepagemenuDropDown(); with below lines, my program will run fine.

WebElement mdd = driver.findElement(By.xpath("//button[@id='sparkButton']"));
mdd.click();

Is there some set up that I'm missing?

Update after correcting catch message-- I get following null exception Cannot invoke "org.openqa.selenium.SearchContext.findElement(org.openqa.selenium.By)" because "this.searchContext" is null



Solution 1:[1]

In the test class, try creating a method with beforetest/beforemethod annotation and place the object for the homepage within these methods

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1