Bird
Raised Fist0
Selenium Javatesting~10 mins

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

Choose your learning style10 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.

Practice

(1/5)
1. What is the main purpose of handling an auto-complete field in Selenium testing?
easy
A. To disable the auto-complete feature in the browser
B. To refresh the page automatically after typing
C. To submit the form without entering any data
D. To select the correct suggestion from the dynamic list based on user input

Solution

  1. Step 1: Understand auto-complete behavior

    Auto-complete fields show suggestions dynamically as the user types.
  2. Step 2: Purpose of handling in Selenium

    We need to select the correct suggestion from the list to simulate user interaction accurately.
  3. Final Answer:

    To select the correct suggestion from the dynamic list based on user input -> Option D
  4. Quick Check:

    Auto-complete handling = selecting suggestion [OK]
Hint: Focus on selecting suggestions, not disabling features [OK]
Common Mistakes:
  • Trying to disable auto-complete instead of selecting
  • Ignoring dynamic suggestion list
  • Submitting form without input
2. Which Selenium Java code snippet correctly waits for the auto-complete suggestion list to appear before selecting an option?
easy
A. new WebDriverWait(driver, Duration.ofSeconds(10)).until(ExpectedConditions.visibilityOfElementLocated(By.id("suggestions")));
B. driver.findElement(By.id("suggestions")).click();
C. Thread.sleep(10000); driver.findElement(By.id("suggestions")).click();
D. driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10)); driver.findElement(By.id("suggestions")).click();

Solution

  1. Step 1: Identify correct wait usage

    Explicit wait with WebDriverWait and ExpectedConditions waits for element visibility properly.
  2. Step 2: Compare with other options

    driver.findElement(By.id("suggestions")).click(); clicks without waiting, C uses Thread.sleep which is not best practice, D uses implicit wait which is less reliable for dynamic elements.
  3. Final Answer:

    new WebDriverWait(driver, Duration.ofSeconds(10)).until(ExpectedConditions.visibilityOfElementLocated(By.id("suggestions"))); -> Option A
  4. Quick Check:

    Explicit wait for visibility = new WebDriverWait(driver, Duration.ofSeconds(10)).until(ExpectedConditions.visibilityOfElementLocated(By.id("suggestions"))); [OK]
Hint: Use explicit waits with ExpectedConditions for dynamic elements [OK]
Common Mistakes:
  • Using Thread.sleep instead of explicit wait
  • Relying only on implicit waits
  • Clicking before element is visible
3. Given the following code snippet, what will be the value of the input field after execution?
driver.findElement(By.id("searchBox")).sendKeys("App");
new WebDriverWait(driver, Duration.ofSeconds(5))
  .until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("ul.suggestions li")));
List<WebElement> options = driver.findElements(By.cssSelector("ul.suggestions li"));
for (WebElement option : options) {
  if (option.getText().equals("Apple")) {
    option.click();
    break;
  }
}
String value = driver.findElement(By.id("searchBox")).getAttribute("value");
medium
A. "App"
B. "Apple"
C. "" (empty string)
D. Throws NoSuchElementException

Solution

  1. Step 1: Analyze input and wait

    The code types "App" and waits for suggestions to appear.
  2. Step 2: Select matching suggestion

    It loops through suggestions and clicks the one with text "Apple".
  3. Step 3: Check input value after click

    Clicking "Apple" updates the input field value to "Apple".
  4. Final Answer:

    "Apple" -> Option B
  5. Quick Check:

    Selected suggestion text = input value [OK]
Hint: Click matching suggestion updates input value [OK]
Common Mistakes:
  • Assuming input stays as typed text
  • Ignoring wait for suggestions
  • Expecting empty input after click
4. Identify the error in the following Selenium Java code for selecting an auto-complete option:
driver.findElement(By.id("searchBox")).sendKeys("Ban");
List<WebElement> options = driver.findElements(By.cssSelector("ul.suggestions li"));
for (WebElement option : options) {
  if (option.getText().equals("Banana")) {
    option.click();
  }
}
medium
A. Missing explicit wait for suggestions to appear before finding elements
B. Using sendKeys instead of setText method
C. Incorrect CSS selector syntax for suggestions list
D. Not clearing the input field before typing

Solution

  1. Step 1: Check for wait usage

    The code finds suggestions immediately after typing without waiting for them to appear.
  2. Step 2: Consequence of missing wait

    Without explicit wait, options list may be empty causing no click action.
  3. Final Answer:

    Missing explicit wait for suggestions to appear before finding elements -> Option A
  4. Quick Check:

    Wait before findElements = Missing explicit wait for suggestions to appear before finding elements [OK]
Hint: Always wait for suggestions before accessing them [OK]
Common Mistakes:
  • Skipping explicit wait causing empty list
  • Assuming suggestions load instantly
  • Not breaking loop after click
5. You need to write a Selenium Java test to select the suggestion "Cherry" from an auto-complete field with id "fruitInput". The suggestions appear in a ul with class "autocomplete-list" and each suggestion is a li. Which approach correctly handles this scenario including waiting and verification?
hard
A. Send keys "Che", use implicit wait, find li with text "Cherry" and click, no assertion needed
B. Send keys "Cherry" to input, immediately click first li in ul.autocomplete-list, no wait needed
C. Send keys "Che" to input, wait explicitly for visibility of ul.autocomplete-list, find all li, click the one with text "Cherry", then assert input value equals "Cherry"
D. Clear input, send keys "Ch", use Thread.sleep(5000), click last li in ul.autocomplete-list, assert input value contains "Ch"

Solution

  1. Step 1: Send partial keys and wait explicitly

    Typing "Che" triggers suggestions; explicit wait ensures suggestions are visible before interaction.
  2. Step 2: Find and click correct suggestion, then verify

    Find all li elements, click the one with text "Cherry", then assert input value matches to confirm selection.
  3. Final Answer:

    Send keys "Che" to input, wait explicitly for visibility of ul.autocomplete-list, find all li, click the one with text "Cherry", then assert input value equals "Cherry" -> Option C
  4. Quick Check:

    Explicit wait + correct click + assert input value = Send keys "Che" to input, wait explicitly for visibility of ul.autocomplete-list, find all li, click the one with text "Cherry", then assert input value equals "Cherry" [OK]
Hint: Wait explicitly, click matching text, then verify input value [OK]
Common Mistakes:
  • Not waiting explicitly for suggestions
  • Clicking wrong suggestion or without verification
  • Using Thread.sleep instead of explicit wait
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard