0

I'm trying to get a hang of PageFactory POM, however something is not working and I can not understand what is wrong.

This is my first POM class for Home Page:

package PageFactory;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.PageFactory;

public class Home_Page_POF {

public WebDriver driver;

@FindBy(css = "div#header-profile a#header-profile-toggle")
public WebElement profileToggleButton;

@FindBy(css = "form#loginUserdataForm div.footer div.add-footer a.btn.btn-link.linkicon")
public WebElement newRegistrationButton;

public Home_Page_POF(WebDriver driver) {
this.driver = driver;

PageFactory.initElements(driver, this);
}

}

This is the second POM class for the Reg. page

package PageFactory;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.PageFactory;
import org.openqa.selenium.support.ui.Select;


public class Registration_Page_POF {

public WebDriver driver;

//Personal details WebElements
@FindBy(css = "form#personalDetailsForm div.profile.pe div.profile-block.simple.first fieldset#pefield-title select#pesalutation")
public WebElement titleDropdown;
public Select titleSelect = new Select(titleDropdown);

public Registration_Page_POF(WebDriver driver) {
this.driver = driver;

PageFactory.initElements(driver, this);
}

}

And this is the test case:

package Tests;

import PageFactory.Home_Page_POF;
import PageFactory.Registration_Page_POF;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.io.TemporaryFilesystem;
import org.openqa.selenium.support.ui.Select;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
import java.util.concurrent.TimeUnit;

public class Test_POF {

public WebDriver driver;

Home_Page_POF objHomePage;
Registration_Page_POF objRegPage;

@BeforeClass
public void browserSetUp() {
    System.setProperty("webdriver.chrome.driver", "D:/Install/selenium-2.53.0/drivers/chromedriver.exe");
    driver = new ChromeDriver();

    driver.manage().window().maximize();
    driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);

    driver.navigate().to("http://www.lufthansa.com/");        
}

@AfterClass
public void broserCleanUp() {
    if (driver != null) {
        TemporaryFilesystem.getDefaultTmpFS().deleteTemporaryFiles();
        driver.close();
        driver.quit();
    }
}

@Test
public void Test0001() {

    objHomePage = new Home_Page_POF(driver);

    objHomePage.profileToggleButton.click();

    objHomePage.newRegistrationButton.click();

    objRegPage = new Registration_Page_POF(driver);

    Select titleSelect = new Select(objRegPage.titleDropdown);

    titleSelect.selectByVisibleText("Mr.");

}
}

So the HomePage objects are working fine, I click on two buttons and proceed to registration page. After that I want to select a value from dropdown, but it gives me NullPointerException:

java.lang.NullPointerException
at org.openqa.selenium.support.ui.Select.<init>(Select.java:44)
at PageFactory.Registration_Page_POF.<init>(Registration_Page_POF.java:17)
at Tests.Test_POF.Test0001(Test_POF.java:56)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:85)
at org.testng.internal.Invoker.invokeMethod(Invoker.java:639)
at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:816)
at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1124)
at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:125)
at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:108)
at org.testng.TestRunner.privateRun(TestRunner.java:774)
at org.testng.TestRunner.run(TestRunner.java:624)
at org.testng.SuiteRunner.runTest(SuiteRunner.java:359)
at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:354)
at org.testng.SuiteRunner.privateRun(SuiteRunner.java:312)
at org.testng.SuiteRunner.run(SuiteRunner.java:261)
at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
at org.testng.TestNG.runSuitesSequentially(TestNG.java:1215)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1140)
at org.testng.TestNG.run(TestNG.java:1048)
at org.testng.IDEARemoteTestNG.run(IDEARemoteTestNG.java:74)
at org.testng.RemoteTestNGStarter.main(RemoteTestNGStarter.java:121)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144)

1 Answer 1

1

The Problem is you are initializing the varaible titleSelect in class itself. Just Initialize inside constructor or after finding the element;

public class Registration_Page_POF {

public WebDriver driver;

//Personal details WebElements
@FindBy(css = "form#personalDetailsForm div.profile.pe div.profile-block.simple.first fieldset#pefield-title select#pesalutation")
public WebElement titleDropdown;
public Select titleSelect; //Dont initialize here

public Registration_Page_POF(WebDriver driver) {
this.driver = driver;

PageFactory.initElements(driver, this);
titleSelect = new Select(titleDropdown);//initialize here
}

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

1 Comment

Thanks, this solved it. I have seen two ways to use .initElements, one inside the PageFactory class, as I have in my code, another inside the test itself. Is any one of them preferable to the other?

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.