In some languages, like Javascript, you can create a hierarchial structure of Elements within a PageObject, like so:
class HomePage {
loginBox = {
usernameTextBox = findElement(By.cssSelector("input.login"));
passwordTextBox = findElement(By.cssSelector("input.password"));
};
headerTitle = findElement(By.cssSelector("h1.title"));
}
and then in your test you can access them as
function loginTest() {
homePage = new HomePage();
homePage.loginBox.usernameTextBox.sendKeys(username);
homePage.loginBox.passwordTextBox.sendKeys(password);
homePage.loginBox.loginButton.click();
assert homePage.headerTitle.contains("Logged In")
}
In Java, however, it seems that however when you try to build inner classes with PageFactory, it simply doesn't descend into inner classes. If you set the inner classes static, you can instantiate the inner-class page objects directly, but you have to assign them to a new object field (e.g. homePageLoginBox = PageFactory.initElements(HomePage.loginBox.class);
) which means you have to explicitly instantiate each inner class.
Is there any way with Page Factory to create a hierarchical page structure and instantiate and access such an organized Page Object (with all subdivisions) in one fell swoop?