0

Here is my attempted implementation understanding of PageObjects/Pagefactory as applied to Webdriver:

1. Create the following structure in eclipse

--> com.example.qa.pageobjects 
     --> LoginPage.java

Every class in this package has something like: 

@FindBy(how = How.NAME, using = "logonName")
    private WebElement logonNameField;

and the Methods, call Webelement, and call methods on them, like:

logonNameField.sendKeys("username");

Which are called from ScenrioTests.

     --> HomePage.java (i go there after i login)
     --> Page.java (abstract)

--> com.example.qa.setup
    --> Browser.java
    --> FirefoxBrowser.java (Code specific to FFox)
    --> ChromeBrowser.java (Code Specific to Chrome)

--> com.example.qa.test
    --> Scenario1234.java 
    --> Scenario2345.java 

These Scenario Classes instantiate the PageObjects, and Call methods in them, while the Browser setup is only called Once per test run.

Now the question is:

  1. Should i declare a method like below and call Pagefactory ?

public MyPage method() { Call the Methods like Login() etc return PageFactory.initElements(driver, MDNSLoginPage.class); }

  1. Or, Should i Call the same PageFactory from default Constructor

  2. Is my understanding / implementation correct ?

kamal
  • 9,637
  • 30
  • 101
  • 168

1 Answers1

0

I think I kind of follow your no 1 approach . I will try to answer by illustrating my implementation but I am not sure whether it would be useful for your purposes. I have a baseTest class that I load with common methods that can be used across my test classes . (eg. instantiate browser, open login page etc)

abstract class TestBase {
//somewhere
protected static LoginPage goToLoginPage(){
   driver.get(loginPage);
    return  PageFactory.initElements(driver, LoginPage.class);
} 

protected static void startBrowser(Browser browser){}
   //implemntation
  }

Then, in my test classes that inherit the abstract class I use it in the following manner

@BeforeClass
public static void setup(){
  //use a common method to start browser    
  startBrowser(Browser.FIREFOX);
    }

@Test
public void canLogInToHomePage(){
//start my test like this
LoginPage loginPage =goToLoginPage();

}
StatusQuo
  • 1,436
  • 1
  • 13
  • 13