Question

With WebDriver and PageFactory, using Java we are implementing a new automation project, and we've been experimenting with various ways of having PageObjects created. We're torn on a few different ideas, and want to make sure we don't work ourselves into a corner.

Is it best to, as documented in the WebDriver documentation, provide an initialized WebDriver to a PageFactory, along with the class template to create a new PageObject?

driver.get(URL);
PageObject page = PageFactory.initElements(driver, PageObject.class);

// elsewhere
class PageObject {
  private WebDriver driver;

  public PageObject(WebDriver driver) {
    this.driver = driver;
    this.validateUrl();
  }

  public void validateUrl() throws Exception {
    if (!driver.getUrl().equals(url)) {
      throw new Exception("URL not valid");
    }
  }
}

However, since the PageObject knows a lot about itself, such as perhaps its URL, can we not have the Page Object do the work?

PageObject page = new PageObject(driver);
page.goToUrl();

// elsewhere
class PageObject {
  private WebDriver driver;
  private String url;

  public PageObject(WebDriver driver) {
    PageFactory.initElements(driver, this);
  }

  public void goToUrl() {
    driver.get(url);
  }
}

I suppose I don't see much of an advantage to having the PageFactory do the instantiation versus just initialization, however I don't want to stray from the standards setup by the architects if there's a reason for it.

Thanks

Was it helpful?

Solution

One of the advantage of Page Factory:

Scenario:

In your application, you are having 100 fields in a page. The same page is called for 50 times.

If this type of scenario is done by using Page Object means, it will find each element again and again. There may be a chance for degradation of the performance.

If the same scenario is done by using Page Factory means, it will find the elements only for the first time, and then it will take from the cache. By this the performance is increased.

OTHER TIPS

The best way is to let the frameworks like Geb and Thucydides abstract out the PageObjects and their initialization. I have been using Geb + Spock BDD combination for this and the results so far has been excellent.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top