Test Overview
This test opens a web page, uses a page object to interact with the login form, submits credentials, and verifies successful login by checking the welcome message.
This test opens a web page, uses a page object to interact with the login form, submits credentials, and verifies successful login by checking the welcome message.
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 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 WebDriverWait wait; public LoginPage(WebDriver driver) { this.driver = driver; this.wait = new WebDriverWait(driver, java.time.Duration.ofSeconds(10)); } public void enterUsername(String username) { WebElement usernameField = wait.until(ExpectedConditions.presenceOfElementLocated(By.id("username"))); usernameField.clear(); usernameField.sendKeys(username); } public void enterPassword(String password) { WebElement passwordField = wait.until(ExpectedConditions.presenceOfElementLocated(By.id("password"))); passwordField.clear(); passwordField.sendKeys(password); } public void clickLogin() { WebElement loginButton = wait.until(ExpectedConditions.elementToBeClickable(By.id("loginBtn"))); loginButton.click(); } public String getWelcomeMessage() { WebElement welcomeMsg = wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("welcomeMsg"))); return welcomeMsg.getText(); } } // Test class consuming the LoginPage object 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 testSuccessfulLogin() { loginPage.enterUsername("testuser"); loginPage.enterPassword("password123"); loginPage.clickLogin(); String welcome = loginPage.getWelcomeMessage(); assertEquals("Welcome, testuser!", welcome); } @AfterEach public void tearDown() { if (driver != null) { driver.quit(); } } }
| Step | Action | System State | Assertion | Result |
|---|---|---|---|---|
| 1 | Test starts and Chrome browser opens | Browser window is open, ready to navigate | - | PASS |
| 2 | Navigates to https://example.com/login | Login page is loaded with username, password fields and login button visible | Page URL is correct and login form elements are present | PASS |
| 3 | Finds username input field and enters 'testuser' | Username field contains 'testuser' | Username field text equals 'testuser' | PASS |
| 4 | Finds password input field and enters 'password123' | Password field contains 'password123' | Password field text equals 'password123' | PASS |
| 5 | Finds and clicks the login button | Login form submitted, page starts loading next view | - | PASS |
| 6 | Waits for welcome message element to appear | Welcome message 'Welcome, testuser!' is visible on page | Welcome message text equals 'Welcome, testuser!' | PASS |
| 7 | Test ends and browser closes | Browser window closed | - | PASS |