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