1

When triggering my test I seem to be seeing a null pointer exception when trying to interact with my Page Factory WebElement.

Code contained within my DriverFactory:

public class DriverFactory {
    private static DriverFactory instance = null;
    public static ThreadLocal<WebDriver> webDriver = new ThreadLocal<WebDriver>();

    public static DriverFactory getInstance() {
        if ( instance == null ) {
            instance = new DriverFactory();
        }
        return instance;
    }

    public static final void setDriver(String browser) {
        switch (browser) {

        case "firefox":
                System.setProperty("webdriver.gecko.driver", 
                Global_VARS.FIREFOX_DRIVER_DIRECTORY);
                webDriver.set(new FirefoxDriver());
            break;

        case "chrome":
                System.setProperty("webdriver.chrome.driver", 
                Global_VARS.CHROME_DRIVER_DIRECTORY);
                webDriver.set(new ChromeDriver());
            break;
        }
        getDriver().manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
        getDriver().manage().window().maximize();
    }

    public static WebDriver getDriver() {
        return webDriver.get();
    }

    public static void tearDown() {
        getDriver().quit();
    }
}

Code contained within my base page:

public abstract class BasePO<T>  {
    private @FindBy(xpath = "//a[text()='Log in']") WebElement logIn_button;
    protected WebDriver driver;

    public BasePO() {
        this.driver = DriverFactory.getDriver();
        PageFactory.initElements(this.driver, this);
    }

    public void openHomepage() {
        driver.get("https://stackoverflow.com/");
    }

    public void baseClickOnLoginButton() {
        logIn_button.click();
    }
}

Code contained within my BaseTest class:

public class BaseTest {
    public SubPage subPage;
    public BasePO<?> basePage;

    @BeforeClass
    public void pomSetup() {
        subPage = PageFactory.initElements(DriverFactory.getDriver(), SubPage.class);
        basePage = PageFactory.initElements(DriverFactory.getDriver(), BasePO.class);
    }

    @BeforeMethod
    public void setup() {
        DriverFactory.setDriver("chrome");

        //works
        //subPage.openHomepage();
    }

    @AfterMethod
    public void tearDown() {
        if (DriverFactory.getDriver() != null) {
            DriverFactory.tearDown();
        }
    }

The code which form's my Test case:

public class Test1 extends BaseTest {
    @Test
    public void exampleTest1() throws InterruptedException {
        subPage.openHomepage(); //works as expected

        subPage.clickOnLoginButton(); //Exception here, null pointer 
    }
}

When trigger my TestNg test its the openHomePage method works, in turn opening the specified url; which uses DriverFactory.getDriver().get() however when attempting to click on a Page Factory element such as calling: logIn_button.click(); within my test I seem to be receiving a null pointer exception even though I have initialised the class?

3
  • You're doing a PageFactory.InitElements in both the BasePO class and the BaseTest class. Commented Dec 18, 2018 at 18:58
  • 2
    PageFactory for abstract class ? i'am getting confused Commented Dec 18, 2018 at 19:42
  • You cannot call PageFactory.initElements(DriverFactory.getDriver(), BasePO.class), because BasePO is abstract class. Commented Dec 18, 2018 at 20:18

2 Answers 2

2

In the @BeforeClass, you have initialized the Page factory before you created the driver instance. If you move the code in pomSetup() to the setup() method after the DriverFactory.setDriver("chrome"); the test code should work. Also, in the BasePO class you have initialized the page factory in the constructor, so calling new in the BaseTest Class will be enough.

@BeforeClass
public void pomSetup() {

}

@BeforeMethod
public void setup() {
    DriverFactory.setDriver("chrome");
    // Page factory initialized the constructor of BasePO class
    subPage = new SubPage();
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you @Sighil!
@Sighil, do you by any chance know the answer to this question? stackoverflow.com/questions/67067486/…
0

On the code part, a couple of suggestions.

  • The implementation of the DriveFactory is wrong. You are using a singleton initialization which is never used. Instead change the code to below.

    public class DriverFactory {
        private static DriverFactory instance = null;
        // Singleton initialization
        public static DriverFactory getInstance() {
            if ( instance == null ) 
                instance = new DriverFactory();
            return instance;
        }
    
        public ThreadLocal<WebDriver> webDriver = new ThreadLocal<WebDriver>();
    
        public final void setDriver(String browser) {
            switch (browser) {
                case "firefox":
                    System.setProperty("webdriver.gecko.driver", Global_VARS.FIREFOX_DRIVER_DIRECTORY);
                    webDriver.set(new FirefoxDriver());
                    break;
                case "chrome":
                    System.setProperty("webdriver.chrome.driver", Global_VARS.CHROME_DRIVER_DIRECTORY);
                    webDriver.set(new ChromeDriver());
                    break;
            }
            getDriver().manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
            getDriver().manage().window().maximize();
        }
    
        public WebDriver getDriver() {
            return webDriver.get();
        }
    
        public void tearDown() {
            getDriver().quit();
        }
    }
    

1 Comment

@Sigil thank you again for your help, really appreciate it

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.