One of the most powerful POM conventions: action methods return the page object you land on after the action. Click login? Return DashboardPage. Click logout? Return LoginPage. Click transfer? Return TransferPage. This creates a fluent API that mirrors the user journey.
// The return types document the navigation flow
public class LoginPage extends BasePage {
public DashboardPage loginAs(String user, String pass) {
type(usernameField, user);
type(passwordField, pass);
click(loginButton);
return new DashboardPage(driver); // Navigate to dashboard
}
}
public class DashboardPage extends BasePage {
public TransferPage clickTransfer() {
click(transferButton);
return new TransferPage(driver); // Navigate to transfer
}
public LoginPage logout() {
click(logoutButton);
return new LoginPage(driver); // Back to login
}
}This makes tests incredibly readable. Each method call returns the next page, so you can chain the entire user journey:
@Test
public void testFullBankingFlow() {
LoginPage loginPage = new LoginPage(driver);
DashboardPage dashboard = loginPage.loginAs("testuser", "pass123");
Assert.assertTrue(dashboard.isLoaded());
TransferPage transfer = dashboard.clickTransfer();
transfer.performTransfer("Savings", "123456", "500", "Rent");
Assert.assertTrue(transfer.isConfirmationDisplayed());
DashboardPage updated = transfer.goBackToDashboard();
LoginPage loggedOut = updated.logout();
Assert.assertTrue(loggedOut.isDisplayed());
}When a method stays on the same page (like sorting or filtering a catalog), return "this". This enables chaining: catalogPage.sortBy("Price").applyFilter("Electronics").addProduct(0);
Q: How do you handle page navigation in POM?
A: Action methods return the page object that the user navigates to. LoginPage.loginAs() returns DashboardPage. DashboardPage.clickTransfer() returns TransferPage. This documents the navigation flow through return types and enables fluent, readable test code. When a method stays on the same page (like filtering), I return "this" to enable method chaining.
Key Point: Return the next page object from action methods. Return "this" when staying on the same page.