Selenium + Java · Lesson 35 of 44
Module 35: Page Object Model
Your task
Implement simple LoginPage with By locators:
Write it in the editor, then press Check answer (Ctrl + Enter).
public class LoginPage {
private final WebDriver driver;
private final By usernameInput = By.id("username");
private final By passwordInput = By.id("password");
private final By submitBtn = By.id("login-btn");
public LoginPage(WebDriver driver) {
this.driver = driver;
}
public void login(String user, String pass) {
driver.findElement(usernameInput).sendKeys(user);
driver.findElement(passwordInput).sendKeys(pass);
driver.findElement(submitBtn).click();
}
}Welcome to Phase 5! The Page Object Model is the cornerstone design pattern of professional test automation. In this lesson, you will architect a BasePage, encapsulate WebElements, and compare classical By locators with Selenium's PageFactory and FindBy annotations. Let's design scalable page objects!
Part of the Selenium + Java course (Selenium with Java). Practice this concept live in the Selenium Online Compiler or explore our free automation practice form.
Many modern enterprise teams prefer standard By locators over PageFactory (@FindBy) because PageFactory hides initialization timing and complicates dynamic waiting.
Ready? Solve the task in the editor
Use Check answer to mark it complete. Hints are there if you need them.
___