1

I am trying to automate using selenium webdriver, testng and page factory. But I am facing null pointer exception.

public class BrowserOpenClose extends Common {
    public WebDriver driver;

    @BeforeTest
    @Parameters("browser")
    public void initBrowser(String browser) {
        if (browser.equalsIgnoreCase("FF")) {
            System.setProperty("webdriver.firefox.marionette", "./gecko/geckodriver.exe");
            driver = new FirefoxDriver();
            driver.manage().window().maximize();
            driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
            driver.get("https://www.flipkart.com/");
        } else if (browser.equalsIgnoreCase("CH")) {
            System.setProperty("webdriver.chrome.driver", "./chrome/chromedriver.exe");
            driver = new ChromeDriver();
            driver.manage().window().maximize();
            driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
            driver.get("https://www.flipkart.com/");
        }
    }

    @AfterTest
    public void closeBrowser() {
        waitUntil(10);
        driver.quit();
    }
}

    public class Login extends BrowserOpenClose {

    @FindBy(xpath = "//a[text()='Log In']")
    private WebElement loginLink;

    @FindBy(xpath = "//input[@class='_2zrpKA']")
    private WebElement username;

    @FindBy(xpath = "//input[@class='_2zrpKA _3v41xv']")
    private WebElement password;

    @FindBy(xpath = "//button[@class='_3zLR9i _1LctnI _36SmAs']")
    private WebElement loginButton;

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

    public void openLoginDialog() {
        loginLink.click();
    }

    public void enterUsername(String name) {
        username.sendKeys(name);
    }

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

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

}

    public class SwitchTShirtPage extends BrowserOpenClose {
    WebDriver driver;
    Login login;

    @BeforeMethod
    public void initElements() {
        login = new Login(driver);
    }

    @Test
    public void switchToTShirt() {
        login.openLoginDialog();
        login.enterUsername("abc");
        login.enterPassword("xyz");
        login.clickLogin();
    }

}

Error trace log: 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.$Proxy9.click(Unknown Source) at com.login.test.Login.openLoginDialog(Login.java:50) at com.homepage.test.SwitchTShirtPage.switchToTShirt(SwitchTShirtPage.java:28) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) at java.lang.reflect.Method.invoke(Unknown Source) at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:100) at org.testng.internal.Invoker.invokeMethod(Invoker.java:646) at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:811) at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1129) at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:129) at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:112) at org.testng.TestRunner.privateRun(TestRunner.java:746) at org.testng.TestRunner.run(TestRunner.java:600) at org.testng.SuiteRunner.runTest(SuiteRunner.java:366) at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:361) at org.testng.SuiteRunner.privateRun(SuiteRunner.java:319) at org.testng.SuiteRunner.run(SuiteRunner.java:268) at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52) at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86) at org.testng.TestNG.runSuitesSequentially(TestNG.java:1264) at org.testng.TestNG.runSuitesLocally(TestNG.java:1189) at org.testng.TestNG.runSuites(TestNG.java:1104) at org.testng.TestNG.run(TestNG.java:1076) at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:126) at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:152) at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:57)

2
  • You need to pass super class's (i.e. BrowserOpenClose) driver reference while instantiating Login in initElements(), currently you are passing sub class's (i.e. SwitchTShirtPage) driver reference. Which is not initialized. As as result java.lang.NullPointerException. Commented Aug 25, 2016 at 13:31
  • thank you:) @AtulDwivedi Commented Aug 25, 2016 at 16:10

1 Answer 1

0

WebDriver driver from SwitchTShirtPage is never initialized and is always null.

What you want is the one from BrowserOpenClose, so just remove the declaration from SwitchTShirtPage.

Then, be careful because driver is initialized with a @BeforeTest which is only run once by test (group of classes). So only the first instance of BrowserOpenClose will have driver initialized. Consider to replace @BeforeTest by @BeforeClass which should fix the issue.

Sign up to request clarification or add additional context in comments.

1 Comment

Julien I am in initial stage so used @BeforeTest, later will change to respective annotation. Thanks for your help.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.