Even experienced engineers make these mistakes. Avoid them from the start and your framework will be rock solid.
// BAD — assertion inside page object
public class LoginPage extends BasePage {
public void loginAs(String user, String pass) {
type(usernameField, user);
type(passwordField, pass);
click(loginButton);
// DO NOT do this!
Assert.assertTrue(
driver.findElement(By.id("dashboard")).isDisplayed(),
"Login failed!");
}
}
// GOOD — page provides data, test asserts
public class LoginPage extends BasePage {
public DashboardPage loginAs(String user, String pass) {
type(usernameField, user);
type(passwordField, pass);
click(loginButton);
return new DashboardPage(driver);
}
}
@Test
public void testLogin() {
DashboardPage dashboard = loginPage.loginAs("user", "pass");
Assert.assertTrue(dashboard.isLoaded()); // Test does the assertion
}One giant class with 200 locators for the entire application. It becomes unmaintainable fast. Split into one class per page.
// BAD — test manipulates raw elements
public WebElement getUsernameField() {
return driver.findElement(By.id("username"));
}
// Test does this:
loginPage.getUsernameField().clear();
loginPage.getUsernameField().sendKeys("user");
// GOOD — expose actions, not elements
public void enterUsername(String username) {
type(usernameField, username);
}The golden rule: Page objects provide services (what a user can do), not implementation details (how the UI works). Tests should not need to know that the login button is a CSS selector — they just call loginAs().
Q: Should you put assertions in page objects?
A: No. Assertions belong in test classes only. Page objects should expose the page state — methods like isLoaded(), getErrorMessage(), getAccountBalance(). The test decides what to assert. This way, the same page object works for both positive and negative tests. If LoginPage had an assertion that login must succeed, you could not use it to test invalid login scenarios.
Key Point: No assertions in page objects. No God page objects. No exposed WebElements. No hardcoded data or waits.