Page class design in Selenium Java - Build an Automation Script
import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.support.ui.ExpectedConditions; import org.openqa.selenium.support.ui.WebDriverWait; import java.time.Duration; // Page class for Login Page public class LoginPage { private WebDriver driver; private WebDriverWait wait; private By emailField = By.id("email"); private By passwordField = By.id("password"); private By loginButton = By.id("loginBtn"); public LoginPage(WebDriver driver) { this.driver = driver; this.wait = new WebDriverWait(driver, Duration.ofSeconds(10)); } public void enterEmail(String email) { wait.until(ExpectedConditions.visibilityOfElementLocated(emailField)); driver.findElement(emailField).clear(); driver.findElement(emailField).sendKeys(email); } public void enterPassword(String password) { wait.until(ExpectedConditions.visibilityOfElementLocated(passwordField)); driver.findElement(passwordField).clear(); driver.findElement(passwordField).sendKeys(password); } public void clickLogin() { wait.until(ExpectedConditions.elementToBeClickable(loginButton)); driver.findElement(loginButton).click(); } } // Test class import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver; import org.testng.Assert; import org.testng.annotations.AfterClass; import org.testng.annotations.BeforeClass; import org.testng.annotations.Test; public class LoginTest { private WebDriver driver; private LoginPage loginPage; @BeforeClass public void setUp() { // Set path to chromedriver executable if needed driver = new ChromeDriver(); driver.manage().window().maximize(); driver.get("https://example.com/login"); loginPage = new LoginPage(driver); } @Test public void testValidLogin() { loginPage.enterEmail("testuser@example.com"); loginPage.enterPassword("Test@1234"); loginPage.clickLogin(); // Wait for URL to contain /dashboard new org.openqa.selenium.support.ui.WebDriverWait(driver, Duration.ofSeconds(10)) .until(d -> d.getCurrentUrl().contains("/dashboard")); Assert.assertTrue(driver.getCurrentUrl().contains("/dashboard"), "Dashboard page should be displayed after login"); } @AfterClass public void tearDown() { if (driver != null) { driver.quit(); } } }
The LoginPage class encapsulates the login page elements and actions. It uses By locators with IDs, which are stable and fast. The constructor initializes the WebDriver and WebDriverWait for explicit waits.
Each method waits explicitly for the element to be visible or clickable before interacting. This avoids flaky tests due to timing issues.
The LoginTest class sets up the browser, navigates to the login page, and uses the page object methods to perform login steps. It then waits for the URL to contain '/dashboard' and asserts this condition to verify successful login.
Finally, the browser is closed in @AfterClass to clean up resources.
This design keeps page details separate from test logic, making tests easier to maintain and read.
Now add data-driven testing with 3 different sets of login credentials (valid and invalid) to verify login behavior.