Search code examples
javapage-factory

How to use Page factory along with Selenium to initialize the functions


I'm a learner and a absolute beginner to Java. I'm learning to create a automation framework. I have started building a frame work by watching a online tutorial. But the tutorial was in C# and I was trying to build that in Java. So, I was stuck and not able to figure out what's wrong with my code.

Here is the C# code which I'm referring to:

 namespace DemoFramework
{
public static class Pages
        {
            private static T GetPage<T>() where T : new()
            {
                var page = new T();
                PageFactory.InitElements(Browser.Driver, page);
                return page;
            }

            public static AboutPage About
            {
                get { return GetPage<AboutPage>(); }
            }

            public static TopNavigationPage TopNavigation
            {
                get { return GetPage<TopNavigationPage>(); }
            }
}

Here is the Java code which I'm writing:

 public class Pages {

        static WebDriver driver;

        private static Pages GetPage() {
            Pages page = new Pages();
            PageFactory.initElements(driver,page);
            return page;
        }

        public static AboutPage About {

            return return GetPage<AboutPage>();

        }
public static TopNavigationPage TopNavigationPage {

            return Pages.GetPage<TopNavigationPage>();

    }

Could you help me with figuring out what's how to fix this. I'm not able to get how to return for ex. AboutPage.

 public static AboutPage About {

        return GetPage<AboutPage>();

    }

Solution

  • Here's an example of a base page object in java:

    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.support.PageFactory;
    import org.openqa.selenium.support.pagefactory.AjaxElementLocatorFactory;
    
    public class BasePageObject {
    
        WebDriver driver;
    
        public BasePageObject(WebDriver driver) {
    
            this.driver = driver;
            PageFactory.initElements(new AjaxElementLocatorFactory(driver, 15), this);
    
        }
    
    }
    

    Here's an example of a specific page object. The "helper" references are a group of helper methods I have and are declared in my original base page object, but I removed above since implementing your selenium wrapper methods is not what I'm trying to show here. Just know that the helper methods help wrap the selenium methods with exception handling and logging.

    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.WebElement;
    import org.openqa.selenium.support.FindBy;
    
    public class MTSignOnPage extends BasePageObject {
    
        public MTSignOnPage(WebDriver driver) {
            super(driver);
        }
    
        @FindBy(xpath = "//img[contains(@src,'mast_signon')]")
        WebElement pageHeader;
        @FindBy(name = "userName")
        WebElement userName;
        @FindBy(name = "password")
        WebElement password;
        @FindBy(name = "login")
        WebElement submit;
    
        public boolean pageLoaded() {
            return helper.isDisplayed(pageHeader);
        }
    
        public void setUserName(String name) {
            helper.sendKeys(userName, name);
        }
    
        public void setPassword(String pword) {
            helper.sendKeys(password, pword);
        }
    
        public void clickSubmit() {
            helper.click(submit);
        }
    
    }
    

    Finally, a snippet of the code that implements the above page object:

    The following code uses JUnit and JBehave (BDD)

    MTHeaderPage mtHeader;
    
    @Given("I am on the Mercury Tours home page")
    public void givenIAmOnTheMercuryToursHomePage() {
        driver.get("http://newtours.demoaut.com");
        helper.waitForPageToLoad();
        mtHeader = new MTHeaderPage(driver);
        if (driver.getTitle().equalsIgnoreCase("Welcome: Mercury Tours")) 
            test.logGivenPass("I am on the Mercury Tours home page");
        else {
            test.logGivenFail("I am on the Mercury Tours home page", 
                              "Current URL: " + driver.getCurrentUrl());
            fail();
        }
    }
    
    @When("I enter User Name <userName> and password <password>")
    public void whenIEnterUserNameuserNameAndPasswordpassword(@Named("userName") String userName,
                                                              @Named("password") String password) {
        helper.waitForPageToLoad();
        mtHeader = new MTHeaderPage(driver);
        mtHeader.setUserName(userName);
        mtHeader.setPassword(password);
        test.logWhenPass("I enter User Name " + userName + " and password " + password);
    }
    
    @When("I click the sign-in link")
    public void whenIClickTheSigninLink() {
        mtHeader.clickSignIn();
        test.logAndPass("I click the sign-in link");
    }