0
0
Selenium Javatesting~10 mins

Auto-complete field handling in Selenium Java - Test Execution Trace

Choose your learning style9 modes available
Test Overview

This test automates entering text into an auto-complete input field, selects a suggested option, and verifies the correct selection is made.

Test Code - Selenium
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.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
import java.time.Duration;

public class AutoCompleteTest {
    WebDriver driver;
    WebDriverWait wait;

    @BeforeEach
    public void setUp() {
        driver = new ChromeDriver();
        wait = new WebDriverWait(driver, Duration.ofSeconds(10));
        driver.get("https://example.com/autocomplete");
    }

    @Test
    public void testAutoCompleteSelection() {
        WebElement input = driver.findElement(By.id("auto-complete-input"));
        input.sendKeys("Uni");

        // Wait until suggestions are visible
        wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("ul.suggestions li")));

        // Select the suggestion "United States"
        WebElement suggestion = driver.findElement(By.xpath("//ul[@class='suggestions']/li[text()='United States']"));
        suggestion.click();

        // Verify the input value is updated
        String selectedValue = input.getAttribute("value");
        assertEquals("United States", selectedValue);
    }

    @AfterEach
    public void tearDown() {
        if (driver != null) {
            driver.quit();
        }
    }
}
Execution Trace - 7 Steps
StepActionSystem StateAssertionResult
1Test starts and Chrome browser opensBrowser window is open at https://example.com/autocomplete-PASS
2Finds the auto-complete input field by id 'auto-complete-input'Input field is located and ready for input-PASS
3Types 'Uni' into the input fieldInput field shows text 'Uni', suggestions dropdown starts loading-PASS
4Waits until suggestion list items are visible using WebDriverWait and ExpectedConditionsSuggestions dropdown is visible with multiple options including 'United States'Checks visibility of suggestions list itemsPASS
5Finds the suggestion 'United States' by XPath and clicks it'United States' suggestion is clicked and input field updates-PASS
6Gets the input field value and asserts it equals 'United States'Input field value is 'United States'Assert input value equals 'United States'PASS
7Test ends and browser closesBrowser window is closed-PASS
Failure Scenario
Failing Condition: Suggestion 'United States' is not found or not clickable
Execution Trace Quiz - 3 Questions
Test your understanding
What does the test wait for before clicking a suggestion?
APage title to change
BInput field to be empty
CVisibility of suggestion list items
DBrowser URL to change
Key Result
Always wait explicitly for dynamic elements like auto-complete suggestions to appear before interacting, to avoid flaky tests.