NullPointerException uising PageFactory

100 views Asked by At

I just started using page factory but I can seem to figure out what is causing the null point exception in my code. Its most likely an issue with how I'm calling the driver but don't know how to resolve it

Managing my webdriver here

    public class Browser {

    public static WebDriver driver;


    public static void setup() {
        ChromeOptions handlingSSL = new ChromeOptions();
        handlingSSL.setAcceptInsecureCerts(true);

        WebDriverManager.chromedriver().setup();
        driver = new ChromeDriver(handlingSSL);
        driver.manage().window().maximize();
        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
    }

    public static void quit() {
        driver.quit();
    }
}

The prep for my TC

    public class Base {

    @BeforeMethod
    public void setup() {
        Browser.setup();
        Browser.driver.get("https://example.com");
    }


    @AfterMethod
    public void quit() {
        Browser.quit();
    }
}

The page class with all the elements

    public  class LoginPage {

    @FindBy (id = "username")
    WebElement username;
    @FindBy (id = "password")
    WebElement password;
    @FindBy (xpath = "//button[contains(text(), 'Sign')]")
    WebElement sign;


        public void logIn(String userName, String passWord) {
            username.sendKeys(userName);
            password.sendKeys(passWord);
            sign.click();
        }
    }

I've tried adding the constructor method in the page call as well but it was a no go as well

public class LoginPageTest extends Base {

    LoginPage loginPage = PageFactory.initElements(Browser.driver, LoginPage.class);

    @Test
    public void verifyLoginTest(){
        loginPage.logIn("user","pass");
    }
}
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.$Proxy10.sendKeys(Unknown Source)
    at pages.LoginPage.logIn(LoginPage.java:18)
    at login.LoginPageTest.verifyLoginTest(LoginPageTest.java:16)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:564)
    at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:80)
    at org.testng.internal.Invoker.invokeMethod(Invoker.java:714)
    at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:901)
    at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1231)
    at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:127)
    at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:111)
    at org.testng.TestRunner.privateRun(TestRunner.java:767)
    at org.testng.TestRunner.run(TestRunner.java:617)
    at org.testng.SuiteRunner.runTest(SuiteRunner.java:334)
    at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:329)
    at org.testng.SuiteRunner.privateRun(SuiteRunner.java:291)
    at org.testng.SuiteRunner.run(SuiteRunner.java:240)
    at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
    at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
    at org.testng.TestNG.runSuitesSequentially(TestNG.java:1198)
    at org.testng.TestNG.runSuitesLocally(TestNG.java:1123)
    at org.testng.TestNG.run(TestNG.java:1031)
    at com.intellij.rt.testng.IDEARemoteTestNG.run(IDEARemoteTestNG.java:66)
    at com.intellij.rt.testng.RemoteTestNGStarter.main(RemoteTestNGStarter.java:109)
1

There are 1 answers

2
DesertPride On

You probably need to have a constructor in your LoginPage.java and define a private object for driver in that class something like this :

private WebDriver driver;

public LoginPage(WebDriver driver) {
     this.driver = driver;
}