Automate login functionality using Page Object pattern
Preconditions (2)
✅ Expected Result: User successfully logs in and dashboard page is displayed
import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; import org.openqa.selenium.support.ui.ExpectedConditions; import org.openqa.selenium.support.ui.WebDriverWait; import static org.junit.jupiter.api.Assertions.*; import java.time.Duration; // Page Object for Login Page 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 open() { driver.get("http://example.com/login"); } public void enterEmail(String email) { WebElement emailInput = wait.until(ExpectedConditions.visibilityOfElementLocated(emailField)); emailInput.clear(); emailInput.sendKeys(email); } public void enterPassword(String password) { WebElement passwordInput = wait.until(ExpectedConditions.visibilityOfElementLocated(passwordField)); passwordInput.clear(); passwordInput.sendKeys(password); } public void clickLogin() { WebElement loginBtn = wait.until(ExpectedConditions.elementToBeClickable(loginButton)); loginBtn.click(); } } public class LoginTest { private WebDriver driver; private WebDriverWait wait; private LoginPage loginPage; @BeforeEach public void setUp() { driver = new ChromeDriver(); wait = new WebDriverWait(driver, Duration.ofSeconds(10)); loginPage = new LoginPage(driver); } @Test public void testSuccessfulLogin() { loginPage.open(); loginPage.enterEmail("user@example.com"); loginPage.enterPassword("Password123"); loginPage.clickLogin(); // Wait for URL to contain /dashboard boolean urlContainsDashboard = wait.until(ExpectedConditions.urlContains("/dashboard")); assertTrue(urlContainsDashboard, "URL should contain '/dashboard' after login"); } @AfterEach public void tearDown() { if (driver != null) { driver.quit(); } } }
This test uses the Page Object Model pattern to separate the login page details from the test logic. The LoginPage class contains methods to interact with the login page elements. This makes the test easier to read and maintain.
Explicit waits ensure elements are ready before interacting, avoiding flaky tests. Assertions check that after login, the URL contains '/dashboard', confirming successful login.
The setup and teardown methods initialize and close the browser cleanly for each test run.
Now add data-driven testing with 3 different sets of login credentials (valid and invalid)
int result = calculator.add(2, 3);
assertEquals(5, result);
add returns 4 instead of 5?@Test
public void testDivide() {
int result = calculator.divide(10, 0);
assertEquals(0, result);
}
Pass the test and this pattern moves unknown