Quantcast
Channel: Active questions tagged selenium - Stack Overflow
Viewing all articles
Browse latest Browse all 98814

NullPointerException when using pagefactory

$
0
0

Code:

BASECLASS.JAVA

public class baseclass {
       public static WebDriver driver;
    public baseclass(WebDriver driver) {
        // TODO Auto-generated constructor stub   
        this.driver=driver;
        PageFactory.initElements(driver, this);
    }

    public homepage intializedriver() throws IOException {

        Properties pr=new Properties();
        FileInputStream fs=new FileInputStream("C:\\Users\\Admin\\eclipse - 
        workspace\\learning\\src\\main\\java\\baselearning\\config.properties");
        pr.load(fs);
        String browsername=pr.getProperty("browser");
        if(browsername.equals("chrome")) {
            System.setProperty("webdriver.chrome.driver","D:\\chromedriver.exe");
            driver=new ChromeDriver();
        }
        else if(browsername.equals("firefox")) {
            //firefox code
        }
        driver.get("https://www.flipkart.com/");
        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);


        return PageFactory.initElements(driver, homepage.class);
    }

}

HOMEPAGE.JAVA

public class homepage extends baseclass {
    public WebDriver driver;
    public homepage(WebDriver driver) throws IOException {
        super(driver);
    }


      @FindBy(xpath="//a[contains(text(),'Login & Signup')]")  
      public WebElement clickLogin;

      @FindBy(xpath="//button[@class='_2AkmmA _29YdH8']") 
      public WebElement buttonclose;

      public void clicklogin() {
           clickLogin.click();

          }
      public void buttonclose() { 
          buttonclose.click();

}

TESTCLASS.JAVA

public class firstTest {

        public WebDriver driver;
        baseclass bs=null;
        homepage hp=null;

        @Test
        public void homepagenaviagation() throws IOException, InterruptedException{
            bs=new baseclass(driver);
            bs.intializedriver();

            hp=new homepage(driver);
            Thread.sleep(3000);
            hp.buttonclose();
            Thread.sleep(3000);
            hp.clicklogin();
    }

}

Error:

java.lang.NullPointerException
at org.openqa.selenium.support.pagefactory.DefaultElementLocator.findElement(DefaultElementLocator.java:69)
at org.openqa.selenium.support.pagefactory.internal.LocatingElementHandler.invoke(LocatingElementHandler.java:38)
at com.sun.proxy.$Proxy7.click(Unknown Source)

Viewing all articles
Browse latest Browse all 98814

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>