In this Example we will focus on Handling Auto Suggestion or
Auto Complete Textbox in Web Based Applications like Yahoo in Selenium WebDriver. This Example helps
to understand the usage of Synchronization – Explicit Wait in Real time Scenario’s.
Also usage of the method
ExpectedConditions.visibilityOfAllElementsLocatedBy
in Selenium WebDriver.
Definition of Auto Complete
Autocomplete or Auto Suggestion allows the browser to predict the value. When a user starts to type in a field, the browser should display options to fill in the field, based on earlier typed values. The auto complete works
with the following input types: text, search, url,email and datepickers.
Scenario
1. Open the browser
2. Enter the url https://in.yahoo.com/?p=us
3. Type Selenium in the Search text box
4. It displays the options for Selenium
5. Select the text ‘Selenium Interview Questions’ from the display options.
Example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
import java.util.List; import org.openqa.selenium.By; import org.openqa.selenium.WebElement; import org.openqa.selenium.firefox.FirefoxDriver; import org.openqa.selenium.support.ui.ExpectedConditions; import org.openqa.selenium.support.ui.WebDriverWait; import org.testng.annotations.Test; public class AutoSuggest { @Test(description="Auto Suggest") public void selectValues() { System.setProperty("webdriver.gecko.driver", "geckodriver.exe"); FirefoxDriver driver = new FirefoxDriver(); driver.get("https://in.yahoo.com/?p=us"); driver.findElement(By.id("uh-search-box")).sendKeys("Selenium"); /** * Example for Visibility of Elements located by */ WebDriverWait wait = new WebDriverWait(driver,30); wait.until(ExpectedConditions.visibilityOfAllElementsLocatedBy(By.xpath("//ul[@role='listbox']//li"))); List<WebElement> list = driver.findElements(By.xpath("//ul[@role='listbox']//li")); System.out.println("Auto Suggest List ::" + list.size()); for(int i = 0 ;i< list.size();i++) { System.out.println(list.get(i).getText()); if(list.get(i).getText().equals("selenium interview questions")) { list.get(i).click(); break; } } /** * Verify the Search Results */ } } |
Console Output
Search Results