Search code examples
javaselenium-webdriverwebdriver

Selenium Page Object Model Framework


I am learning page object model using selenium webdriver and can someone advise me on these?

1 - Why does each page in the POM have constructors to initialize the webdriver object? (like the one below) What will happen if webdriver is not initialized?

public class New_Register 
{
    WebDriver driver;

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

    @FindBy(xpath=".//input[@id='emailAdd']") WebElement EmailID;
}

2 - Is POM a framework or design pattern in Selenium Webdriver?


Solution

  • If your Page Object inherits from LoadableComponent class forcing you to override load and isloaded methods and you use the PageFactory.initElements to initialize proxies for your webelements(@Find...) I think you are using it as a framework. If you handle all this stuff taking care of checking whether your page is loaded, getting webelements, passing the pageobject etc but you using a class to model a page or part of its functionality it may be regarded as a design pattern.

    If you do not pass in the driver you will have to initialize driver yourself. Else you will get the favorite Null Pointer Exception when you call any method on it. Also when you call PageFactory.initElements it first looks for a constructor on the pageobject with driver as the sole argument. Then only it looks for the default one if it does not get the previous one, then you will have to handle driver initialization yourself.