From 5976c978f3aba4b8e7d6875bcdfd209caedcce22 Mon Sep 17 00:00:00 2001 From: Manfred Ng Date: Fri, 5 Dec 2025 12:53:39 +0000 Subject: [PATCH] BAEL-6863: Select Text From the Autocomplete Input using Selenium --- testing-modules/selenium-3/pom.xml | 4 +- .../autocomplete/AutoCompleteLiveTest.java | 60 +++++++++++++++++++ 2 files changed, 62 insertions(+), 2 deletions(-) create mode 100644 testing-modules/selenium-3/src/test/java/com/baeldung/selenium/autocomplete/AutoCompleteLiveTest.java diff --git a/testing-modules/selenium-3/pom.xml b/testing-modules/selenium-3/pom.xml index 27a7af60f579..4feb75202dee 100644 --- a/testing-modules/selenium-3/pom.xml +++ b/testing-modules/selenium-3/pom.xml @@ -46,8 +46,8 @@ 7.10.2 - 4.27.0 - 5.9.2 + 4.38.0 + 6.3.3 diff --git a/testing-modules/selenium-3/src/test/java/com/baeldung/selenium/autocomplete/AutoCompleteLiveTest.java b/testing-modules/selenium-3/src/test/java/com/baeldung/selenium/autocomplete/AutoCompleteLiveTest.java new file mode 100644 index 000000000000..d1d94771fa9c --- /dev/null +++ b/testing-modules/selenium-3/src/test/java/com/baeldung/selenium/autocomplete/AutoCompleteLiveTest.java @@ -0,0 +1,60 @@ +package com.baeldung.selenium.autocomplete; + +import static org.assertj.core.api.AssertionsForClassTypes.assertThat; + +import java.time.Duration; +import java.util.List; + +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +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; + +public class AutoCompleteLiveTest { + + private static final String XPATH_INPUT = "//div[@id='gh-search-box']//input"; + private static final String XPATH_AUTOCOMPLETE_ITEMS = "//ul[@id='ebay-autocomplete']/li"; + + private WebDriver driver; + + @BeforeEach + void setup() { + driver = new ChromeDriver(); + } + + @AfterEach + void teardown() { + driver.quit(); + } + + @Test + void whenUserNavigatesToEBays_thenSelectThe2ndAutoCompleteItemFromSearchInput() throws Exception { + driver.navigate().to("https://www.ebay.com"); + + WebElement inputElement = driver.findElement(By.xpath(XPATH_INPUT)); + String text = "iphone"; + for (char c : text.toCharArray()) { + inputElement.sendKeys(Character.toString(c)); + Thread.sleep(50); + } + + WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(3)); + List autoCompleteElements = wait.until( + ExpectedConditions.visibilityOfAllElementsLocatedBy( + By.xpath(XPATH_AUTOCOMPLETE_ITEMS) + ) + ); + + assertThat(autoCompleteElements.size()).isGreaterThanOrEqualTo(2); + + WebElement secondItem = autoCompleteElements.get(1); + secondItem.click(); + } + +} \ No newline at end of file