Test Overview
This test automates a login process on a web page with dynamic elements. It verifies that advanced Selenium skills like explicit waits and exception handling help manage complex scenarios where elements load unpredictably.
This test automates a login process on a web page with dynamic elements. It verifies that advanced Selenium skills like explicit waits and exception handling help manage complex scenarios where elements load unpredictably.
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.openqa.selenium.NoSuchElementException; 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 AdvancedLoginTest { WebDriver driver; WebDriverWait wait; @BeforeEach public void setUp() { driver = new ChromeDriver(); wait = new WebDriverWait(driver, java.time.Duration.ofSeconds(10)); } @Test public void testLoginWithDynamicElements() { driver.get("https://example.com/login"); // Wait for username field to be visible WebElement username = wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("username"))); username.sendKeys("testuser"); // Wait for password field to be visible WebElement password = wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("password"))); password.sendKeys("password123"); // Wait for login button to be clickable WebElement loginButton = wait.until(ExpectedConditions.elementToBeClickable(By.id("loginBtn"))); loginButton.click(); // Wait for welcome message to appear WebElement welcomeMsg = wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("welcomeMessage"))); // Verify welcome message text assertEquals("Welcome, testuser!", welcomeMsg.getText()); } @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 loads with username, password fields and login button | - | PASS |
| 3 | Waits until username field is visible and enters 'testuser' | Username input field is visible and filled with 'testuser' | Username field is visible before typing | PASS |
| 4 | Waits until password field is visible and enters 'password123' | Password input field is visible and filled with 'password123' | Password field is visible before typing | PASS |
| 5 | Waits until login button is clickable and clicks it | Login button is clicked, page processes login | Login button is clickable before clicking | PASS |
| 6 | Waits until welcome message is visible | Welcome message appears on page | Welcome message is visible | PASS |
| 7 | Verifies welcome message text equals 'Welcome, testuser!' | Welcome message text matches expected | Assert welcome message text correctness | PASS |
| 8 | Test ends and browser closes | Browser window closed | - | PASS |