Test Overview
This test automates entering text into an auto-complete input field, selects a suggested option, and verifies the correct selection is made.
This test automates entering text into an auto-complete input field, selects a suggested option, and verifies the correct selection is made.
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(); } } }
| Step | Action | System State | Assertion | Result |
|---|---|---|---|---|
| 1 | Test starts and Chrome browser opens | Browser window is open at https://example.com/autocomplete | — | PASS |
| 2 | Finds the auto-complete input field by id 'auto-complete-input' | Input field is located and ready for input | — | PASS |
| 3 | Types 'Uni' into the input field | Input field shows text 'Uni', suggestions dropdown starts loading | — | PASS |
| 4 | Waits until suggestion list items are visible using WebDriverWait and ExpectedConditions | Suggestions dropdown is visible with multiple options including 'United States' | Checks visibility of suggestions list items | PASS |
| 5 | Finds the suggestion 'United States' by XPath and clicks it | 'United States' suggestion is clicked and input field updates | — | PASS |
| 6 | Gets the input field value and asserts it equals 'United States' | Input field value is 'United States' | Assert input value equals 'United States' | PASS |
| 7 | Test ends and browser closes | Browser window is closed | — | PASS |
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");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();
}
}ul with class "autocomplete-list" and each suggestion is a li. Which approach correctly handles this scenario including waiting and verification?li elements, click the one with text "Cherry", then assert input value matches to confirm selection.ul.autocomplete-list, find all li, click the one with text "Cherry", then assert input value equals "Cherry" -> Option Cul.autocomplete-list, find all li, click the one with text "Cherry", then assert input value equals "Cherry" [OK]Pass the test and this pattern moves unknown