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)
Guy
  • 46,488
  • 10
  • 44
  • 88

1 Answers1

0

welcome to Stackoverflow!

Let's start with baseclass. The Java convention is to start class names with an uppercase letter, so change the name to BaseClass.

public class BaseClass {
    public static WebDriver driver;

    public BaseClass(WebDriver driver) { 
        this.driver=driver;
        PageFactory.initElements(driver, this);
    }

The line PageFactory.initElements is ok, only if this class is inherited by others. You don't want to explictly create an instance of this class, so let's make it abstract.

Additionally, creating a static WebDriver will cause you a lot of problems in the future. Check out my answer on THIS question where I explain why.

public abstract class BaseClass {
    protected WebDriver driver;

    public BaseClass(WebDriver driver) { 
        this.driver=driver;
        PageFactory.initElements(driver, this);
    }

With the BaseClass like this, we just need to use inheritance on Page Object classes for them to be initialized.

The method initalizedriver should be renamed to initializeDriver to keep up with the Java naming convention. Camel case is used for method names somethingLikeThis.

    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);
    }

The method above returns homepage class which should also be renamed to HomePage.

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();

}

Let's fix the homepage class. Change its name. Remove the public WebDriver driver because you don't need it anymore. HomePage class extends BaseClass which already contains protected WebDriver driver to which you have access to because of the inheritance.

public class HomePage extends BaseClass {

    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();

}

Let's get back for a moment to initializeDriver() method from BaseClass. The problem here is, that you return HomePage class with the last line of that method:

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

This line will cause multiple problems but you need a deeper knowledge of Object-Oriented programming to fully understand the issue. To quick fix it, just change the method declaration to static like this:

public static HomePage intializedriver() throws IOException {

Now, the test class.

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();
    }

}

Change the name, as I mentioned before. Next thing is to utilize @Before annotation (JUnit) to initialize WebDriver, since it's not the part of the test. It's a prerequisite.

Then, we can either use HomePage class which is returned by initializeDriver() method, or initialize it in test class. Like this:

public class FirstTest {
        HomePage hp =null;

        @Before
        public void setUp() {
            hp = HomePage.initializeDriver(); //static method invocation
        }

        @Test
        public void homepagenaviagation() throws IOException, InterruptedException{
            Thread.sleep(3000);
            hp.buttonclose();
            Thread.sleep(3000);
            hp.clicklogin();
    }

}

That's it!

I hope it helps!

Fenio
  • 3,528
  • 1
  • 13
  • 27