1

I am getting java.lang.NullPointerException while executing my test case , I am using Page Object Modle with PageFactory ,following are the code snippets

LoginPageObjec.java

public class LoginPageObjects {

    @FindBy(xpath=".//*[@id='userName']")
    private WebElement email;

    @FindBy(xpath=".//*[@id='password']")
    private WebElement password;

    @FindBy(xpath=".//*[@id='loginFormSubmit']")
    private WebElement loginBtn;

    public void EnterUserName(String userName)
    {
        email.sendKeys(userName);
    }

    public void EnterPassword(String pass)
    {
        password.sendKeys(pass);
    }

    public void clickLogin()
    {
        loginBtn.click();
    }


}

LoginTC.java

public class LoginTC extends BasePage{
    private WebDriver driver;

    @Test
    public void login()
    {
        LoginPageObjects loginPage = PageFactory.initElements(driver, LoginPageObjects.class);
        loginPage.EnterUserName("test.hr@domin.com.pk");
        loginPage.EnterPassword("test");
        loginPage.clickLogin();
    }
public void login1()
        {
            LoginPageObjects loginPage = PageFactory.initElements(driver, LoginPageObjects.class);
            loginPage.EnterUserName("test.hr@domin.com.pk");
            loginPage.EnterPassword("test");
            loginPage.clickLogin();
        }

}

BasePage.java

public class BasePage {
    private WebDriver driver;

    @BeforeMethod
      public void initializeBrowser() {
     driver=new FirefoxDriver();
     driver.navigate().to("https://nexthrm-dev.vteamslabs.com/");
     driver.manage().window().maximize();
     }
    @AfterMethod
    public void closeBrowser()
    {
        driver.close();
    }

}

I've extended BasePage class to utilize the (initialBrowser & closeBrowser) functions in my test case but its producing nullpointer exception if I copy both of the functions (initialBrowser & closeBrowser) into my LoginTC (testcase class) it works like charm but I dont want this redundant code i.e((initialBrowser & closeBrowser) for all my testcases class could anyone explain why I am getting nullpointer exception

1

The driver field in LoginTC hides the driver field from BasePage. The BasePage class fills in its own driver field, but LoginTC uses its own, which is never initialized.

Solution: Remove the driver field from LoginTC, then either:

  • Make the driver field in BasePage public or protected.
  • Add a "getter" method (driver() or getDriver() or similar) to BasePage and use that to access the driver in derived classes.
  • Thanks, Null Poiner exception removed I've another question and for this I've updated my LoginTC.java section i.e if I've multiple testcases in a file do I need to use the following statement for every testcase to initialize pageobject LoginPageObjects loginPage = PageFactory.initElements(driver, LoginPageObjects.class); loginPage.EnterUserName("test.hr@domin.com.pk"); – Hassan May 12 '16 at 11:33
  • You can put those two lines in a setup method in LoginTC and annotate the method with @BeforeMethod. Just make sure to name the method something other than initializeBrowser() so that it doesn't hide the one in the base class. – Dale Emery May 12 '16 at 16:04
1

In your BasePage.Java instead of creating a separate WebDriver object, you can use the WebDriver object of BasePage class directly by using (BasePage.driver) as below:

public class LoginTC extends BasePage{

@Test
public void login()
{
    LoginPageObjects loginPage = PageFactory.initElements(BasePage.driver, LoginPageObjects.class);
    loginPage.EnterUserName("test.hr@domin.com.pk");
    loginPage.EnterPassword("test");
    loginPage.clickLogin();
}
public void login1()
        {
            LoginPageObjects loginPage = PageFactory.initElements(BasePage.driver, LoginPageObjects.class);
            loginPage.EnterUserName("test.hr@domin.com.pk");
            loginPage.EnterPassword("test");
            loginPage.clickLogin();
        }

}

It will help you further so now if you extend this BasePage to your multiple test classes, you need not to initialize the driver again and again. So you can perform multiple test cases on the same browser session.

  • 1
    is this necessary to call initElements for every testcase can't it be done globally in same class file and used in every subsequent test case? – Hassan May 12 '16 at 12:00
  • initElements can be used with composition and inheritance? Or, each POM which uses PageFactory needs to be created from the App "driver" main class? – Thufir Dec 28 '18 at 17:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.