0
0
Selenium Javatesting~10 mins

Why POM creates maintainable test code in Selenium Java - Test Execution Impact

Choose your learning style9 modes available
Test Overview

This test verifies that the Page Object Model (POM) helps keep test code maintainable by separating page structure from test logic. It checks that changes in the page only require updates in one place, making tests easier to maintain.

Test Code - JUnit with Selenium WebDriver
Selenium Java
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;

// Page Object for Login Page
class LoginPage {
    private WebDriver driver;
    private By usernameField = By.id("username");
    private By passwordField = By.id("password");
    private By loginButton = By.id("loginBtn");

    public LoginPage(WebDriver driver) {
        this.driver = driver;
    }

    public void enterUsername(String username) {
        driver.findElement(usernameField).sendKeys(username);
    }

    public void enterPassword(String password) {
        driver.findElement(passwordField).sendKeys(password);
    }

    public void clickLogin() {
        driver.findElement(loginButton).click();
    }
}

// Test class
public class LoginTest {
    private WebDriver driver;
    private LoginPage loginPage;

    @BeforeEach
    public void setUp() {
        driver = new ChromeDriver();
        driver.get("https://example.com/login");
        loginPage = new LoginPage(driver);
    }

    @Test
    public void testValidLogin() {
        loginPage.enterUsername("user1");
        loginPage.enterPassword("pass123");
        loginPage.clickLogin();

        String expectedUrl = "https://example.com/dashboard";
        String actualUrl = driver.getCurrentUrl();
        assertEquals(expectedUrl, actualUrl, "User should be redirected to dashboard after login");
    }

    @AfterEach
    public void tearDown() {
        driver.quit();
    }
}
Execution Trace - 7 Steps
StepActionSystem StateAssertionResult
1Test starts and Chrome browser opensBrowser is open at https://example.com/login page-PASS
2LoginPage object is created with driverPage Object ready to interact with login page elements-PASS
3Enter username 'user1' in username field using LoginPage methodUsername field filled with 'user1'-PASS
4Enter password 'pass123' in password field using LoginPage methodPassword field filled with 'pass123'-PASS
5Click login button using LoginPage methodLogin button clicked, browser navigates to dashboard-PASS
6Verify current URL is https://example.com/dashboardBrowser URL is https://example.com/dashboardAssert current URL equals expected dashboard URLPASS
7Test ends and browser closesBrowser closed-PASS
Failure Scenario
Failing Condition: If the locator in LoginPage is incorrect or page structure changes without updating LoginPage
Execution Trace Quiz - 3 Questions
Test your understanding
Why does using the LoginPage class help maintain test code?
ABecause all locators and actions are in one place, so changes affect only this class
BBecause it runs tests faster
CBecause it removes the need for assertions
DBecause it automatically fixes broken locators
Key Result
Using Page Object Model keeps locators and page actions in one place, so when the web page changes, only the page object needs updating, making tests easier to maintain and less error-prone.