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.
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.
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(); } }
| Step | Action | System State | Assertion | Result |
|---|---|---|---|---|
| 1 | Test starts and Chrome browser opens | Browser is open at https://example.com/login page | - | PASS |
| 2 | LoginPage object is created with driver | Page Object ready to interact with login page elements | - | PASS |
| 3 | Enter username 'user1' in username field using LoginPage method | Username field filled with 'user1' | - | PASS |
| 4 | Enter password 'pass123' in password field using LoginPage method | Password field filled with 'pass123' | - | PASS |
| 5 | Click login button using LoginPage method | Login button clicked, browser navigates to dashboard | - | PASS |
| 6 | Verify current URL is https://example.com/dashboard | Browser URL is https://example.com/dashboard | Assert current URL equals expected dashboard URL | PASS |
| 7 | Test ends and browser closes | Browser closed | - | PASS |