0
0
Selenium Javatesting~10 mins

Why advanced skills handle complex scenarios in Selenium Java - Test Execution Impact

Choose your learning style9 modes available
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.

Test Code - JUnit with Selenium WebDriver
Selenium Java
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();
        }
    }
}
Execution Trace - 8 Steps
StepActionSystem StateAssertionResult
1Test starts and Chrome browser opensBrowser window is open and ready-PASS
2Navigates to https://example.com/loginLogin page loads with username, password fields and login button-PASS
3Waits until username field is visible and enters 'testuser'Username input field is visible and filled with 'testuser'Username field is visible before typingPASS
4Waits until password field is visible and enters 'password123'Password input field is visible and filled with 'password123'Password field is visible before typingPASS
5Waits until login button is clickable and clicks itLogin button is clicked, page processes loginLogin button is clickable before clickingPASS
6Waits until welcome message is visibleWelcome message appears on pageWelcome message is visiblePASS
7Verifies welcome message text equals 'Welcome, testuser!'Welcome message text matches expectedAssert welcome message text correctnessPASS
8Test ends and browser closesBrowser window closed-PASS
Failure Scenario
Failing Condition: If the welcome message does not appear within 10 seconds after login click
Execution Trace Quiz - 3 Questions
Test your understanding
What does the test wait for before entering the username?
AThe username field to be visible
BThe login button to be clickable
CThe welcome message to appear
DThe password field to be visible
Key Result
Using explicit waits and exception handling in Selenium helps tests handle dynamic page elements and timing issues, making tests reliable for complex scenarios.