Test Overview
This test checks if a user can successfully fill out and submit a login form. It verifies that the form accepts valid input and leads to the expected page, ensuring the user workflow works correctly.
This test checks if a user can successfully fill out and submit a login form. It verifies that the form accepts valid input and leads to the expected page, ensuring the user workflow works correctly.
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; public class LoginFormTest { private WebDriver driver; private WebDriverWait wait; @BeforeEach public void setUp() { driver = new ChromeDriver(); wait = new WebDriverWait(driver, 10); } @Test public void testValidLoginFormSubmission() { driver.get("https://example.com/login"); WebElement usernameInput = wait.until(ExpectedConditions.presenceOfElementLocated(By.id("username"))); WebElement passwordInput = driver.findElement(By.id("password")); WebElement submitButton = driver.findElement(By.id("submit-btn")); usernameInput.sendKeys("validUser"); passwordInput.sendKeys("validPass123"); submitButton.click(); wait.until(ExpectedConditions.urlContains("/dashboard")); String currentUrl = driver.getCurrentUrl(); assertEquals("https://example.com/dashboard", currentUrl, "User should be redirected to dashboard after login"); } @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 and ready | - | PASS |
| 2 | Navigates to https://example.com/login | Login page is loaded with username, password fields and submit button visible | Waits until username input is present | PASS |
| 3 | Finds username, password input fields and submit button | All form elements are located and interactable | - | PASS |
| 4 | Enters 'validUser' into username field | Username field contains 'validUser' | - | PASS |
| 5 | Enters 'validPass123' into password field | Password field contains 'validPass123' | - | PASS |
| 6 | Clicks the submit button | Form is submitted, page starts loading | - | PASS |
| 7 | Waits until URL contains '/dashboard' | Browser navigates to dashboard page | Checks current URL equals 'https://example.com/dashboard' | PASS |
| 8 | Test ends and browser closes | Browser window is closed | - | PASS |